diff --git a/packages/kit/src/core/build/build_server.js b/packages/kit/src/core/build/build_server.js index 145fb601fe3a..8887315a5bc3 100644 --- a/packages/kit/src/core/build/build_server.js +++ b/packages/kit/src/core/build/build_server.js @@ -41,6 +41,7 @@ const get_hooks = hooks => ({ getSession: hooks.getSession || (() => ({})), handle: hooks.handle || (({ event, resolve }) => resolve(event)), handleError: hooks.handleError || (({ error }) => console.error(error.stack)), + handleResponse: hooks.handleResponse || (() => {}), externalFetch: hooks.externalFetch || fetch }); diff --git a/packages/kit/src/core/dev/plugin.js b/packages/kit/src/core/dev/plugin.js index 100456fbed91..d75f3a9f5517 100644 --- a/packages/kit/src/core/dev/plugin.js +++ b/packages/kit/src/core/dev/plugin.js @@ -107,6 +107,7 @@ export async function create_plugin(config, cwd) { return { type: 'page', pattern: route.pattern, + key: route.key, params: get_params(route.params), shadow: route.shadow ? async () => { @@ -122,6 +123,7 @@ export async function create_plugin(config, cwd) { return { type: 'endpoint', pattern: route.pattern, + key: route.key, params: get_params(route.params), load: async () => { const url = path.resolve(cwd, route.file); @@ -184,6 +186,7 @@ export async function create_plugin(config, cwd) { // @ts-expect-error this picks up types that belong to the tests getSession: user_hooks.getSession || (() => ({})), handle: amp ? sequence(amp, handle) : handle, + handleResponse: user_hooks.handleResponse || (() => {}), handleError: user_hooks.handleError || (({ /** @type {Error & { frame?: string }} */ error }) => { diff --git a/packages/kit/src/core/generate_manifest/index.js b/packages/kit/src/core/generate_manifest/index.js index af7333d24dda..22829005ed8d 100644 --- a/packages/kit/src/core/generate_manifest/index.js +++ b/packages/kit/src/core/generate_manifest/index.js @@ -68,6 +68,7 @@ export function generate_manifest( return `{ type: 'page', pattern: ${route.pattern}, + key: '${route.key}', params: ${get_params(route.params)}, path: ${route.path ? s(route.path) : null}, shadow: ${route.shadow ? importer(`${relative_path}/${build_data.server.vite_manifest[route.shadow].file}`) : null}, @@ -84,6 +85,7 @@ export function generate_manifest( return `{ type: 'endpoint', pattern: ${route.pattern}, + key: '${route.key}', params: ${get_params(route.params)}, load: ${importer(`${relative_path}/${build_data.server.vite_manifest[route.file].file}`)} }`.replace(/^\t\t/gm, ''); diff --git a/packages/kit/src/runtime/server/index.js b/packages/kit/src/runtime/server/index.js index 9e52cde695d3..b2cfe4232e3c 100644 --- a/packages/kit/src/runtime/server/index.js +++ b/packages/kit/src/runtime/server/index.js @@ -165,6 +165,7 @@ export async function respond(request, options, state = {}) { } if (response) { + options.hooks.handleResponse({ event, response, route }); // respond with 304 if etag matches if (response.status === 200 && response.headers.has('etag')) { let if_none_match_value = request.headers.get('if-none-match'); diff --git a/packages/kit/test/apps/basics/src/hooks.js b/packages/kit/test/apps/basics/src/hooks.js index dfb9959d0e66..8c16ecb343d8 100644 --- a/packages/kit/test/apps/basics/src/hooks.js +++ b/packages/kit/test/apps/basics/src/hooks.js @@ -45,6 +45,12 @@ export const handle = sequence( } ); +export const handleResponse = ({ event, response, route }) => { + console.log( + `request to '${event.url.pathname}' gave response '${response.status}' from route '${route.key}'.` + ); +}; + /** @type {import('@sveltejs/kit').ExternalFetch} */ export async function externalFetch(request) { let newRequest = request;