-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Allow lazy loading of rendering dependencies on ssr mode #7053
Comments
If I understand correctly, you're proposing to be able to read stuff like I think the better solution overall is to have SvelteKit not actually importing the component at all, but lexes the exports to determine the options. This has its own caveats, like Routify on the other hand tackles this with special comments like |
The template uses this pattern to conditionally add a router and hydrate the page. Here is an example on how I think it would work: <!-- ClientThing.svelte -->
<script>
export let name;
</script>
<span>hello, {name}</span> <!-- index.svelte -->
<script context="module">
export const ssr = Math.floor(Math.random() * 2)
</script>
<script>
import ClientThing from './ClientThing.svelte'
</script>
<ClientThing name="user"/> The compiled code currently looks like: /* index.svelte generated by Svelte v3.44.3 */
import { create_ssr_component, validate_component } from "svelte/internal";
import ClientThing from './ClientThing.svelte';
const ssr = Math.floor(Math.random() * 2);
const Index = create_ssr_component(($$result, $$props, $$bindings, slots) => {
return `${validate_component(ClientThing, "ClientThing").$$render($$result, { name: "user" }, {}, {})}`;
});
export default Index;
export { ssr }; But if the option was enabled, the output could look like this: import { create_ssr_component, validate_component } from "svelte/internal";
const ssr = Math.floor(Math.random() * 2);
async function load_Index() {
const {default: ClientThing} = await import("./ClientThing.svelte")
return create_ssr_component(($$result, $$props, $$bindings, slots) => {
return `${validate_component(ClientThing, "ClientThing").$$render($$result, { name: "user" }, {}, {})}`;
});
}
export default load_Index;
export { ssr }; |
Thanks for the explanation, I see what you mean now. From a glance, a caveat with this approach is that Svelte is re-writing imports to dynamic imports. This feels like it would open a can of worms. For example, if a dependency has side effects. I'm still not quite convinced that we have to change Svelte to accommodate this syntax though. But I think there are ways to support this at the bundler level, like importing only the |
Although the manifest would be easier to implement, it could bring problems because we would be duplicating the module part of each page. And unlike sveltejs/kit#2804 this particular solution would not require additional configuration from the sveltekit users, and would keep the logic scoped inside each page again. Although I'm not sure as to how to tell if a module should be lazy loaded because:
|
What if instead of using a compiler otion, we add a new option to <svelte:options lazy={true}/> |
This was solved in Kit by putting |
Describe the problem
Sveltekit currently has an issue where components that have dependencies using browser features such as the
window
anddocument
objects throw errors during the component module initialization, preventing the framework from checking the component's module exports and deciding whether the component is suitable for server-side rendering.Describe the proposed solution
Adding a compiler option to lazily load the dependencies of a component's regular
<script>
, allowing the framework to read the configuration of a page without loading modules that may be incompatible with the current environment.Alternatives considered
Additional configuration options and special handler parameters
Importance
would make my life easier
The text was updated successfully, but these errors were encountered: