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

Defer client hooks import to allow using env in them #8803

Closed
wants to merge 3 commits into from
Closed
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
8 changes: 6 additions & 2 deletions packages/kit/src/core/sync/write_client_manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,20 @@ 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}];

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 || ' : ''
Expand Down
6 changes: 5 additions & 1 deletion packages/kit/src/runtime/client/ambient.d.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -21,6 +21,10 @@ declare module '__CLIENT__/manifest.js' {
export const dictionary: Record<string, [leaf: number, layouts: number[], errors?: number[]]>;

export const matchers: Record<string, ParamMatcher>;
}

declare module '__CLIENT__/hooks.js' {
import { ClientHooks } from 'types';

export const hooks: ClientHooks;
}
Expand Down
8 changes: 7 additions & 1 deletion packages/kit/src/runtime/client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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];

Expand Down
4 changes: 3 additions & 1 deletion packages/kit/src/runtime/client/start.js
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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 {
Expand Down