Skip to content

Commit fcfd947

Browse files
aolosetomRich-Harris
authored
[fix] goto self can incorrectly result in 404 #4316 (#4318)
* fix: last match route fallthrough return 404 * fix * make test relate more directly to the missing method * remove renamed test files * lint Co-authored-by: tom <[email protected]> Co-authored-by: Rich Harris <[email protected]>
1 parent 268ee6b commit fcfd947

File tree

6 files changed

+34
-5
lines changed

6 files changed

+34
-5
lines changed

.changeset/spicy-suits-relax.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
Allow page endpoint without GET handler

packages/kit/src/runtime/client/client.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ export function create_client({ target, session, base, trailing_slash }) {
561561
* @param {import('./types').NavigationIntent} intent
562562
* @param {boolean} no_cache
563563
*/
564-
async function load_route(route, { id, url, path }, no_cache) {
564+
async function load_route(route, { id, url, path, routes }, no_cache) {
565565
if (!no_cache) {
566566
const cached = cache.get(id);
567567
if (cached) return cached;
@@ -570,7 +570,7 @@ export function create_client({ target, session, base, trailing_slash }) {
570570
const [pattern, a, b, get_params, shadow_key] = route;
571571
const params = get_params
572572
? // the pattern is for the route which we've already matched to this path
573-
get_params(/** @type {RegExpExecArray} */ (pattern.exec(path)))
573+
get_params(/** @type {RegExpExecArray} */ (pattern.exec(path)))
574574
: {};
575575

576576
const changed = current.url && {
@@ -642,10 +642,14 @@ export function create_client({ target, session, base, trailing_slash }) {
642642
}
643643

644644
if (res.status === 204) {
645-
// fallthrough
646-
return;
645+
if (route !== routes[routes.length - 1]) {
646+
// fallthrough
647+
return;
648+
}
649+
props = {};
650+
} else {
651+
props = await res.json();
647652
}
648-
props = await res.json();
649653
} else {
650654
status = res.status;
651655
error = new Error('Failed to load data');

packages/kit/test/apps/basics/src/routes/shadowed/index.svelte

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<a href="/shadowed/error-get">error-get</a>
55
<a href="/shadowed/no-get">no-get</a>
66
<a href="/shadowed/dynamic/foo">dynamic/foo</a>
7+
<a href="/shadowed/missing-get">missing-get</a>
78

89
<form action="/shadowed/redirect-post" method="post">
910
<button type="submit" id="redirect-post">redirect</button>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const post = () => {};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<h1>post without get</h1>

packages/kit/test/apps/basics/test/test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,23 @@ test.describe.parallel('Shadowed pages', () => {
522522
await clicknav('[href="/shadowed/redirect/a"]');
523523
expect(await page.textContent('h1')).toBe('done');
524524
});
525+
526+
test('Endpoint without GET', async ({ page, clicknav, baseURL, javaScriptEnabled }) => {
527+
await page.goto('/shadowed');
528+
529+
/** @type {string[]} */
530+
const requests = [];
531+
page.on('request', (r) => requests.push(r.url()));
532+
533+
await clicknav('[href="/shadowed/missing-get"]');
534+
535+
expect(await page.textContent('h1')).toBe('post without get');
536+
537+
// check that the router didn't fall back to the server
538+
if (javaScriptEnabled) {
539+
expect(requests).not.toContain(`${baseURL}/shadowed/missing-get`);
540+
}
541+
});
525542
});
526543

527544
test.describe.parallel('Endpoints', () => {

0 commit comments

Comments
 (0)