Skip to content

Commit

Permalink
chore: have rendered page load only a single script
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccann committed Apr 17, 2023
1 parent 2b3e68a commit 3e0a7c2
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 25 deletions.
6 changes: 0 additions & 6 deletions packages/kit/src/exports/vite/dev/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,6 @@ export async function dev(vite, vite_config, svelte_config) {
imports: [],
stylesheets: [],
fonts: []
},
app: {
file: `${svelte_config.kit.outDir}/generated/client/app.js`,
imports: [],
stylesheets: [],
fonts: []
}
},
nodes: manifest_data.nodes.map((node, index) => {
Expand Down
11 changes: 4 additions & 7 deletions packages/kit/src/exports/vite/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,10 @@ function kit({ svelte_config }) {
name: 'vite-plugin-sveltekit-virtual-modules',

async resolveId(id) {
// treat $env/static/[public|private] as virtual
if (id === '__sveltekit/APP') {
return `${kit.outDir}/generated/client-optimized/app.js`;
}
// virtual modules
if (id.startsWith('$env/') || id.startsWith('__sveltekit/') || id === '$service-worker') {
return `\0${id}`;
}
Expand Down Expand Up @@ -524,7 +527,6 @@ function kit({ svelte_config }) {
});
} else {
input['entry/start'] = `${runtime_directory}/client/start.js`;
input['entry/app'] = `${kit.outDir}/generated/client-optimized/app.js`;

/**
* @param {string | undefined} file
Expand Down Expand Up @@ -731,11 +733,6 @@ function kit({ svelte_config }) {
client_manifest,
posixify(path.relative('.', `${runtime_directory}/client/start.js`)),
false
),
app: find_deps(
client_manifest,
posixify(path.relative('.', `${kit.outDir}/generated/client-optimized/app.js`)),
false
)
};

Expand Down
5 changes: 3 additions & 2 deletions packages/kit/src/runtime/client/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@ import { create_client } from './client.js';
import { init } from './singletons.js';

/**
* @param {import('./types').SvelteKitApp} app
* @param {HTMLElement} target
* @param {Parameters<import('./types').Client['_hydrate']>[0]} [hydrate]
*/
export async function start(app, target, hydrate) {
export async function start(target, hydrate) {
if (DEV && target === document.body) {
console.warn(
`Placing %sveltekit.body% directly inside <body> is not recommended, as your app may break for users who have certain browser extensions installed.\n\nConsider wrapping it in an element:\n\n<div style="display: contents">\n %sveltekit.body%\n</div>`
);
}

// @ts-expect-error
const app = await import('__sveltekit/APP');
const client = create_client(app, target);

init({ client });
Expand Down
14 changes: 6 additions & 8 deletions packages/kit/src/runtime/server/page/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ export async function render_response({

const { client } = manifest._;

const modulepreloads = new Set([...client.start.imports, ...client.app.imports]);
const stylesheets = new Set(client.app.stylesheets);
const fonts = new Set(client.app.fonts);
const modulepreloads = new Set([...client.start.imports]);
const stylesheets = new Set(client.start.stylesheets);
const fonts = new Set(client.start.fonts);

/** @type {Set<string>} */
const link_header_preloads = new Set();
Expand Down Expand Up @@ -329,7 +329,7 @@ export async function render_response({
${properties.join(',\n\t\t\t\t\t\t')}
};`);

const args = [`app`, `${global}.element`];
const args = [`${global}.element`];

if (page_config.ssr) {
const serialized = { form: 'null', error: 'null' };
Expand Down Expand Up @@ -365,10 +365,8 @@ export async function render_response({
args.push(`{\n\t\t\t\t\t\t\t${hydrate.join(',\n\t\t\t\t\t\t\t')}\n\t\t\t\t\t\t}`);
}

blocks.push(`Promise.all([
import(${s(prefixed(client.start.file))}),
import(${s(prefixed(client.app.file))})
]).then(([kit, app]) => {
blocks.push(`import(${s(prefixed(client.start.file))})
.then((kit) => {
kit.start(${args.join(', ')});
});`);

Expand Down
1 change: 0 additions & 1 deletion packages/kit/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1061,7 +1061,6 @@ export interface SSRManifest {
_: {
client: {
start: AssetDependencies;
app: AssetDependencies;
};
nodes: SSRNodeLoader[];
routes: SSRRoute[];
Expand Down
1 change: 0 additions & 1 deletion packages/kit/types/internal.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ export interface BuildData {
service_worker: string | null;
client: {
start: AssetDependencies;
app: AssetDependencies;
} | null;
server_manifest: import('vite').Manifest;
}
Expand Down

0 comments on commit 3e0a7c2

Please sign in to comment.