diff --git a/packages/kit/src/core/sync/write_client_manifest.js b/packages/kit/src/core/sync/write_client_manifest.js index 06c32d8a48d8..ef0c9f4518df 100644 --- a/packages/kit/src/core/sync/write_client_manifest.js +++ b/packages/kit/src/core/sync/write_client_manifest.js @@ -110,8 +110,6 @@ export function write_client_manifest(kit, manifest_data, output, metadata) { write_if_changed( `${output}/manifest.js`, trim(` - ${hooks_file ? `import * as client_hooks from '${relative_path(output, hooks_file)}';` : ''} - export { matchers } from './matchers.js'; export const nodes = [${nodes}]; @@ -119,7 +117,13 @@ export function write_client_manifest(kit, manifest_data, output, metadata) { export const server_loads = [${[...layouts_with_server_load].join(',')}]; export const dictionary = ${dictionary}; + `) + ); + write_if_changed( + `${output}/hooks.js`, + trim(` + ${hooks_file ? `import * as client_hooks from '${relative_path(output, hooks_file)}';` : ''} export const hooks = { handleError: ${ hooks_file ? 'client_hooks.handleError || ' : '' diff --git a/packages/kit/src/runtime/client/ambient.d.ts b/packages/kit/src/runtime/client/ambient.d.ts index 5f98d9891506..0a55aa3f838e 100644 --- a/packages/kit/src/runtime/client/ambient.d.ts +++ b/packages/kit/src/runtime/client/ambient.d.ts @@ -1,5 +1,5 @@ declare module '__CLIENT__/manifest.js' { - import { CSRPageNodeLoader, ClientHooks, ParamMatcher } from 'types'; + import { CSRPageNodeLoader, ParamMatcher } from 'types'; /** * A list of all the error/layout/page nodes used in the app @@ -21,6 +21,10 @@ declare module '__CLIENT__/manifest.js' { export const dictionary: Record; export const matchers: Record; +} + +declare module '__CLIENT__/hooks.js' { + import { ClientHooks } from 'types'; export const hooks: ClientHooks; } diff --git a/packages/kit/src/runtime/client/client.js b/packages/kit/src/runtime/client/client.js index e74085ab3993..87bd343e3a52 100644 --- a/packages/kit/src/runtime/client/client.js +++ b/packages/kit/src/runtime/client/client.js @@ -25,7 +25,7 @@ import { import { parse } from './parse.js'; import Root from '__GENERATED__/root.svelte'; -import { nodes, server_loads, dictionary, matchers, hooks } from '__CLIENT__/manifest.js'; +import { nodes, server_loads, dictionary, matchers } from '__CLIENT__/manifest.js'; import { HttpError, Redirect } from '../control.js'; import { stores } from './singletons.js'; import { unwrap_promises } from '../../utils/promises.js'; @@ -36,6 +36,12 @@ import { compact } from '../../utils/array.js'; const routes = parse(nodes, server_loads, dictionary, matchers); +/** @type {import('types').ClientHooks} */ +let hooks; +export async function load_client_hooks() { + ({ hooks } = await import('__CLIENT__/hooks.js')); +} + const default_layout_loader = nodes[0]; const default_error_loader = nodes[1]; diff --git a/packages/kit/src/runtime/client/start.js b/packages/kit/src/runtime/client/start.js index 1083898a97c1..f9454f322d50 100644 --- a/packages/kit/src/runtime/client/start.js +++ b/packages/kit/src/runtime/client/start.js @@ -1,5 +1,5 @@ import { DEV } from 'esm-env'; -import { create_client } from './client.js'; +import { create_client, load_client_hooks } from './client.js'; import { init } from './singletons.js'; import { set_paths, set_version, set_public_env } from '../shared.js'; @@ -33,6 +33,8 @@ export async function start({ env, hydrate, paths, target, version }) { init({ client }); + await load_client_hooks(); + if (hydrate) { await client._hydrate(hydrate); } else {