-
Notifications
You must be signed in to change notification settings - Fork 0
feat(sophora-components): Alternative build setup / clientside-only rendering #343
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
1ed0e97
Log debug information
sjockers f013be0
Refactor getDataFromUrl (less specific to domains, add error logging)
sjockers 81ccbf2
Alternative setup: Svelte only, clientside rendering
sjockers 1a3bb30
Run storybook workflows for components directory only
sjockers 89de4b1
Resolve individual imports from @swr-data-lab/components
sjockers 0450de9
Use ASSETS_PATH env variable in build process
sjockers f5c0bd7
Move JS to body (so that it is picked up by the sophora embedding)
sjockers c8275b4
Mount multiple instances of an embed on a single page.
sjockers 541db3f
Show preview links in index page
sjockers 92acb4b
Refactor project structure
sjockers c7b5f3a
Reduce boilerplate
sjockers 7780413
Remove debugger logging
sjockers 74df49f
Move mounting of embeds to utility function
sjockers 1ee7fcc
Re-mount on pjax:complete
sjockers 136df97
Add popstate handler
sjockers b404424
Expose mounting function via window object
sjockers 2e02957
Use previous sibling as target, no more initialization loop
sjockers bf82451
Update DOM loaded handling
sjockers ec180fc
Move initialization for datawrapper-switcher into module
sjockers 8fb8f1c
Add PJAX fallback for re-mounting embeds upon PJAX navigation
sjockers 4a41d66
Defer initialization after PJAX navigation
sjockers 8301b31
Remove debug logs and redundant styling
sjockers 74e13fc
Add log for validating mount cycle
sjockers 4c91240
Poll for DOM instead of simple setTimeout
sjockers ce7c6ce
Merge main into feat/sophora-components-clientside-only
sjockers 54f5b5d
Simplify polling
sjockers 2cf07d1
Remove logging, adjust delay
sjockers File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| <!doctype html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="utf-8" /> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1" /> | ||
| </head> | ||
| <body> | ||
| <div data-lab-components-embed="datawrapper-switcher"></div> | ||
| <script type="module"> | ||
| import mountEmbeds from '/src/lib/utils/mountEmbeds'; | ||
| import Embed from '/src/components/datawrapper-switcher/embed.svelte'; | ||
|
|
||
| mountEmbeds(Embed, '[data-lab-components-embed="datawrapper-switcher"]'); | ||
| </script> | ||
| </body> | ||
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| <!doctype html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="utf-8" /> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1" /> | ||
| </head> | ||
| <body> | ||
| <div data-lab-components-embed="highlight-cards"></div> | ||
| <script type="module"> | ||
| import mountEmbeds from '/src/lib/utils/mountEmbeds'; | ||
| import Embed from '/src/components/highlight-cards/embed.svelte'; | ||
|
|
||
| mountEmbeds(Embed, '[data-lab-components-embed="highlight-cards"]'); | ||
| </script> | ||
| </body> | ||
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 3 additions & 2 deletions
5
.../routes/datawrapper-switcher/+page.svelte → ...ponents/datawrapper-switcher/embed.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 3 additions & 2 deletions
5
...s/src/routes/highlight-cards/+page.svelte → ...c/components/highlight-cards/embed.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| // Shim for $app/environment used by @swr-data-lab/components | ||
| // Replaces SvelteKit's built-in module in this pure Vite setup | ||
| export const browser = typeof window !== 'undefined'; | ||
| export const dev = import.meta.env.DEV; | ||
| export const building = false; | ||
| export const version = ''; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,21 +1,16 @@ | ||
| export default function getDataFromUrl(target: HTMLElement): Record<string, string> { | ||
| let url: URL; | ||
| if ( | ||
| // SvelteKit DEV mode, preview server, or static hosting: | ||
| import.meta.env.DEV || | ||
| window.location.origin === 'http://localhost:4173' || | ||
| window.location.origin === 'https://static.datenhub.net' || | ||
| window.location.href.includes('apidata.googleusercontent.com') || | ||
| window.location.href.includes('storage.googleapis.com') | ||
| ) { | ||
| const parent = target.parentNode?.parentNode as HTMLElement | null; | ||
|
|
||
| // Default: Embedded mode – use URL used to embed the component | ||
| // `data-url` is the embeds the grandparent element, provided by Sophora | ||
| let embedURL = parent?.dataset.url; | ||
|
|
||
| if (!embedURL) { | ||
| // Preview mode – use URL of current page | ||
| url = new URL(window.location.href); | ||
| } else { | ||
| // Embedded mode – use URL used to embed the component | ||
| // `data-url` is set on the grandparent element, provided by Sophora | ||
| const parent = target.parentNode?.parentNode as HTMLElement | null; | ||
| url = new URL(parent?.dataset.url || ''); | ||
| embedURL = window?.location.href; | ||
| } | ||
| const params: Record<string, string> = Object.fromEntries(url.searchParams); | ||
| return params; | ||
|
|
||
| return URL.canParse(embedURL) | ||
| ? Object.fromEntries(new URL(embedURL).searchParams.entries()) | ||
| : (console.error('Could not parse Embed-URL:', embedURL), {}); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import { mount } from 'svelte'; | ||
| import type { SvelteComponent } from 'svelte'; | ||
|
|
||
| declare global { | ||
| interface Window { | ||
| $?: unknown; | ||
| } | ||
| } | ||
|
|
||
| export default async function mountEmbeds( | ||
| Component: typeof SvelteComponent, | ||
| targetSelector: string | ||
| ): Promise<void> { | ||
| const _mountEmbeds = (embedTargets: NodeListOf<Element>) => { | ||
| embedTargets.forEach((target) => { | ||
| target.innerHTML = ''; // Clear any existing content before mounting | ||
| mount(Component, { target }); | ||
| }); | ||
| }; | ||
|
|
||
| // Mount the embeds on initial page load | ||
| _mountEmbeds(document.querySelectorAll(targetSelector)); | ||
|
|
||
| // SWR.dev uses PJAX and jQuery for client-side navigation, which replaces page content without a full reload. | ||
| // This means we need to re-mount the Svelte components after PJAX updates the DOM during client-side navigation. | ||
| // https://github.com/defunkt/jquery-pjax?tab=readme-ov-file#reinitializing-pluginswidget-on-new-page-content | ||
| if (window.$) { | ||
| $(document).on('pjax:end', () => { | ||
| const maxPolls = 10; | ||
| const pollDelay = 300; // ms | ||
| let pollCount = 0; | ||
|
|
||
| // Delay to ensure new DOM is ready for mounting. | ||
| // PJAX triggers 'pjax:end' before the mounting targets are actually available in the DOM. | ||
| const interval = window.setInterval(() => { | ||
| const embedTargets = document.querySelectorAll(targetSelector); | ||
| if (embedTargets.length > 0) { | ||
| _mountEmbeds(embedTargets); | ||
| window.clearInterval(interval); | ||
| } else if (++pollCount >= maxPolls) { | ||
| window.clearInterval(interval); | ||
| } | ||
| }, pollDelay); | ||
| }); | ||
| } | ||
| } |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,19 @@ | ||
| { | ||
| "extends": "./.svelte-kit/tsconfig.json", | ||
| "compilerOptions": { | ||
| "allowJs": true, | ||
| "checkJs": true, | ||
| "target": "es2022", | ||
| "module": "es2022", | ||
| "moduleResolution": "bundler", | ||
| "esModuleInterop": true, | ||
| "forceConsistentCasingInFileNames": true, | ||
| "resolveJsonModule": true, | ||
| "skipLibCheck": true, | ||
| "sourceMap": true, | ||
| "strict": true | ||
| } | ||
| "strict": true, | ||
| "paths": { | ||
| "$lib/*": ["./src/lib/*"] | ||
| } | ||
| }, | ||
| "include": ["src/**/*", "vite.config.ts"] | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.