Skip to content
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

[fix] Lazy load Svelte components to reenable no-ssr use cases #5930

Merged
merged 3 commits into from
Aug 16, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tender-bottles-cheer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Lazy load Svelte components to reenable no-ssr use cases
2 changes: 1 addition & 1 deletion packages/kit/src/runtime/server/page/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export async function render_response({
url: state.prerendering ? new PrerenderingURL(event.url) : event.url,
data: branch.reduce((acc, { data }) => (Object.assign(acc, data), acc), {})
},
components: branch.map(({ node }) => node.component)
components: await Promise.all(branch.map(({ node }) => node.component()))
};

// TODO remove this for 1.0
Expand Down
6 changes: 5 additions & 1 deletion packages/kit/src/vite/build/build_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,8 @@ export async function build_server(options, client) {
/** @type {string[]} */
const imports = [];

// String representation of
/** @type {import('types').SSRNode} */
Comment on lines +242 to +243
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is kind of a confusing way to write the comment. maybe something like this?

Suggested change
// String representation of
/** @type {import('types').SSRNode} */
// String representation of types.SSRNode

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I specifically wrote it like this so that you can do "find usages" on the type and it takes you to this place. This would have been helpful to me to find the correct places I need to change quicker.

/** @type {string[]} */
const exports = [`export const index = ${i};`];

Expand All @@ -255,7 +257,9 @@ export async function build_server(options, client) {
stylesheets.push(...entry.stylesheets);

exports.push(
`export { default as component } from '../${vite_manifest[node.component].file}';`,
`export const component = async () => (await import('../${
vite_manifest[node.component].file
}')).default;`,
`export const file = '${entry.file}';` // TODO what is this?
);
}
Expand Down
30 changes: 17 additions & 13 deletions packages/kit/src/vite/dev/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,19 +82,23 @@ export async function dev(vite, vite_config, svelte_config, illegal_imports) {
result.stylesheets = [];

if (node.component) {
const { module, module_node, url } = await resolve(node.component);

module_nodes.push(module_node);

result.component = module.default;
result.file = url.endsWith('.svelte') ? url : url + '?import'; // TODO what is this for?

prevent_illegal_vite_imports(
module_node,
illegal_imports,
extensions,
svelte_config.kit.outDir
);
result.component = async () => {
const { module_node, module, url } = await resolve(
/** @type {string} */ (node.component)
);

module_nodes.push(module_node);
result.file = url.endsWith('.svelte') ? url : url + '?import'; // TODO what is this for?

prevent_illegal_vite_imports(
module_node,
illegal_imports,
extensions,
svelte_config.kit.outDir
);

return module.default;
};
}

if (node.shared) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script context="module">
document;
</script>

<p>Works</p>
6 changes: 6 additions & 0 deletions packages/kit/test/apps/basics/test/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -614,3 +614,9 @@ test.describe('Page Store', () => {
);
});
});

test('Can use browser-only global on client-only page', async ({ page, read_errors }) => {
await page.goto('/no-ssr/browser-only-global');
await expect(page.locator('p')).toHaveText('Works');
expect(read_errors('/no-ssr/browser-only-global')).toBe(undefined);
});
7 changes: 1 addition & 6 deletions packages/kit/types/internal.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ export interface SSREndpoint {
}

export interface SSRNode {
component: SSRComponent;
component: SSRComponentLoader;
/** index into the `components` array in client-manifest.js */
index: number;
/** client-side module URL for this component */
Expand Down Expand Up @@ -280,11 +280,6 @@ export interface SSRErrorPage {
id: '__error';
}

export interface SSRPagePart {
id: string;
load: SSRComponentLoader;
}

export type SSRRoute = SSREndpoint | SSRPage;

export interface SSRState {
Expand Down