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

handleResponse hook for logging/monitoring which route was matched #3907

Closed
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
1 change: 1 addition & 0 deletions packages/kit/src/core/build/build_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
});

Expand Down
3 changes: 3 additions & 0 deletions packages/kit/src/core/dev/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand All @@ -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);
Expand Down Expand Up @@ -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 }) => {
Expand Down
2 changes: 2 additions & 0 deletions packages/kit/src/core/generate_manifest/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -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, '');
Expand Down
1 change: 1 addition & 0 deletions packages/kit/src/runtime/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
6 changes: 6 additions & 0 deletions packages/kit/test/apps/basics/src/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down