From 3bcf87d3100b490e704ba903f7e877ec08258e7d Mon Sep 17 00:00:00 2001 From: gtmnayan Date: Mon, 30 Jan 2023 17:58:46 +0545 Subject: [PATCH 1/3] defer importing client hooks until env is set --- packages/kit/src/core/sync/write_client_manifest.js | 8 ++++++-- packages/kit/src/runtime/client/ambient.d.ts | 6 +++++- packages/kit/src/runtime/client/client.js | 10 +++++++++- packages/kit/src/runtime/client/start.js | 4 +++- 4 files changed, 23 insertions(+), 5 deletions(-) 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..08d8f4f04051 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,14 @@ import { compact } from '../../utils/array.js'; const routes = parse(nodes, server_loads, dictionary, matchers); +/** @type {import('types').ClientHooks} */ +let hooks; +/** @param {typeof hooks} _hooks */ +export function set_hooks(_hooks) { + console.log(_hooks); + hooks = _hooks; +} + 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..e5ecf3747c37 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, set_hooks } from './client.js'; import { init } from './singletons.js'; import { set_paths, set_version, set_public_env } from '../shared.js'; @@ -26,6 +26,8 @@ export async function start({ env, hydrate, paths, target, version }) { ); } + set_hooks((await import('__CLIENT__/hooks.js')).hooks); + const client = create_client({ target, base: paths.base From 82b90516f5f6b09bf09a30ec6c730b32a122a636 Mon Sep 17 00:00:00 2001 From: gtmnayan Date: Mon, 30 Jan 2023 18:40:09 +0545 Subject: [PATCH 2/3] better api? --- packages/kit/src/runtime/client/client.js | 6 ++---- packages/kit/src/runtime/client/start.js | 4 ++-- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/packages/kit/src/runtime/client/client.js b/packages/kit/src/runtime/client/client.js index 08d8f4f04051..87bd343e3a52 100644 --- a/packages/kit/src/runtime/client/client.js +++ b/packages/kit/src/runtime/client/client.js @@ -38,10 +38,8 @@ const routes = parse(nodes, server_loads, dictionary, matchers); /** @type {import('types').ClientHooks} */ let hooks; -/** @param {typeof hooks} _hooks */ -export function set_hooks(_hooks) { - console.log(_hooks); - hooks = _hooks; +export async function load_client_hooks() { + ({ hooks } = await import('__CLIENT__/hooks.js')); } const default_layout_loader = nodes[0]; diff --git a/packages/kit/src/runtime/client/start.js b/packages/kit/src/runtime/client/start.js index e5ecf3747c37..3722de8b76bf 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, set_hooks } 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'; @@ -26,7 +26,7 @@ export async function start({ env, hydrate, paths, target, version }) { ); } - set_hooks((await import('__CLIENT__/hooks.js')).hooks); + await load_client_hooks(); const client = create_client({ target, From 0e1ab812ba531eb39ee9cab237eb975d417334c5 Mon Sep 17 00:00:00 2001 From: gtmnayan Date: Mon, 30 Jan 2023 18:46:36 +0545 Subject: [PATCH 3/3] shuffle execution order to make tests pass... how? --- packages/kit/src/runtime/client/start.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/kit/src/runtime/client/start.js b/packages/kit/src/runtime/client/start.js index 3722de8b76bf..f9454f322d50 100644 --- a/packages/kit/src/runtime/client/start.js +++ b/packages/kit/src/runtime/client/start.js @@ -26,8 +26,6 @@ export async function start({ env, hydrate, paths, target, version }) { ); } - await load_client_hooks(); - const client = create_client({ target, base: paths.base @@ -35,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 {