Skip to content

feat: server js fallthrough to page if handler doesn't exist #10121

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

Closed
wants to merge 5 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
5 changes: 5 additions & 0 deletions .changeset/spicy-rockets-ring.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

feat: server js fallthrough to page if method handler doesn't exist
23 changes: 15 additions & 8 deletions packages/kit/src/runtime/server/respond.js
Original file line number Diff line number Diff line change
Expand Up @@ -378,11 +378,8 @@ export async function respond(request, options, manifest, state) {
}

if (route) {
/** @type {Response} */
let response;

if (is_data_request) {
response = await render_data(
return await render_data(
event,
route,
options,
Expand All @@ -392,16 +389,26 @@ export async function respond(request, options, manifest, state) {
trailing_slash ?? 'never'
);
} else if (route.endpoint && (!route.page || is_endpoint_request(event))) {
response = await render_endpoint(event, await route.endpoint(), state);
const endpoint = await route.endpoint();
const response = await render_endpoint(event, endpoint, state);

// If endpoint doesn't have a handler for these methods, fall back to the page
if (
response.status === 405 &&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did you mean for this to be 405? based on the comment I would have expected 404

Copy link
Member

@eltigerchino eltigerchino Oct 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think it's 405. Kit responds with 405 method not allowed when the method handler isn't found.

route.page &&
['HEAD', 'GET', 'POST'].includes(event.request.method)
) {
return await render_page(event, route.page, options, manifest, state, resolve_opts);
} else {
return response;
}
} else if (route.page) {
response = await render_page(event, route.page, options, manifest, state, resolve_opts);
return await render_page(event, route.page, options, manifest, state, resolve_opts);
} else {
// a route will always have a page or an endpoint, but TypeScript
// doesn't know that
throw new Error('This should never happen');
}

return response;
}

if (state.error) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<pre>Hello from the page!</pre>
7 changes: 7 additions & 0 deletions packages/kit/test/apps/basics/test/server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,13 @@ test.describe('Endpoints', () => {
expect(response.status()).toBe(200);
expect(await response.text()).toBe('ok');
});

test('falls back to page if no matching handler found', async ({ request }) => {
const response = await request.get('/routing/endpoint-fallthrough');

expect(response.status()).toBe(200);
expect(await response.text()).toContain('Hello from the page!');
});
});

test.describe('Errors', () => {
Expand Down