Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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/wild-mice-end.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

perf: cache dynamic imports of nodes
24 changes: 20 additions & 4 deletions packages/kit/src/core/generate_manifest/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export function generate_manifest({ build_data, relative_path, routes }) {
);

/** @type {(path: string) => string} */
const loader = (path) => `() => import('${path}')`;
const loader = (path) => `__memo(() => import('${path}'))`;
Copy link
Member

Choose a reason for hiding this comment

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

Small comment maybe why we memo the import

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added a comment near the __memo declaration below. That should suffice.


const assets = build_data.manifest_data.assets.map((asset) => asset.file);
if (build_data.service_worker) {
Expand All @@ -65,8 +65,8 @@ export function generate_manifest({ build_data, relative_path, routes }) {

// prettier-ignore
// String representation of
/** @type {import('@sveltejs/kit').SSRManifest} */
return dedent`
/** @template {import('@sveltejs/kit').SSRManifest} T */
Copy link
Member

Choose a reason for hiding this comment

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

I don't think it's correct to use @template here. That's for type parameters and not template literals: https://www.typescriptlang.org/docs/handbook/jsdoc-supported-types.html#template. Unless maybe I'm missing where the T is?

Copy link
Contributor Author

@gtm-nayan gtm-nayan Jun 7, 2023

Choose a reason for hiding this comment

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

it's a hack to get VSCode to create a jump link for the SSRManifest. setting it as @type gave an error because the string is not of the type SSRManifest

const manifest_expr = dedent`
{
appDir: ${s(build_data.app_dir)},
appPath: ${s(build_data.app_path)},
Expand Down Expand Up @@ -97,10 +97,26 @@ export function generate_manifest({ build_data, relative_path, routes }) {
}).filter(Boolean).join(',\n')}
],
matchers: async () => {
${Array.from(matchers).map(type => `const { match: ${type} } = await import ('${(join_relative(relative_path, `/entries/matchers/${type}.js`))}')`).join('\n')}
${Array.from(
matchers,
type => `const { match: ${type} } = await import ('${(join_relative(relative_path, `/entries/matchers/${type}.js`))}')`
).join('\n')}
return { ${Array.from(matchers).join(', ')} };
}
}
}
`;

// Memoize the loaders to prevent Node from doing unnecessary work
// on every dynamic import call
return dedent`
Copy link
Member

Choose a reason for hiding this comment

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

Small comment maybe why we need the memo

(() => {
function __memo(fn) {
let value;
return () => value ??= (value = fn());
}

return ${manifest_expr}
})()
`;
}
7 changes: 6 additions & 1 deletion packages/kit/src/exports/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,12 @@ const encoder = new TextEncoder();
export function text(body, init) {
const headers = new Headers(init?.headers);
if (!headers.has('content-length')) {
headers.set('content-length', encoder.encode(body).byteLength.toString());
const encoded = encoder.encode(body);
headers.set('content-length', encoded.byteLength.toString());
return new Response(encoded, {
Copy link
Member

Choose a reason for hiding this comment

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

is this a drive-by fix?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Another perf thing to avoid duplicating the encoding work inside the Response constructor.

...init,
headers
});
}

return new Response(body, {
Expand Down
3 changes: 2 additions & 1 deletion packages/kit/src/exports/vite/build/build_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ export function build_server_nodes(out, kit, manifest_data, server_manifest, cli

if (node.component && client_manifest) {
exports.push(
`export const component = async () => (await import('../${
'let component_cache;',
`export const component = async () => component_cache ??= (await import('../${
resolve_symlinks(server_manifest, node.component).chunk.file
}')).default;`
);
Expand Down