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

[fix] goto self can incorrectly result in 404 #4316 #4318

Merged
merged 5 commits into from
Mar 14, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/spicy-suits-relax.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Allow page endpoint without GET handler
14 changes: 9 additions & 5 deletions packages/kit/src/runtime/client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ export function create_client({ target, session, base, trailing_slash }) {
* @param {import('./types').NavigationIntent} intent
* @param {boolean} no_cache
*/
async function load_route(route, { id, url, path }, no_cache) {
async function load_route(route, { id, url, path, routes }, no_cache) {
if (!no_cache) {
const cached = cache.get(id);
if (cached) return cached;
Expand All @@ -570,7 +570,7 @@ export function create_client({ target, session, base, trailing_slash }) {
const [pattern, a, b, get_params, shadow_key] = route;
const params = get_params
? // the pattern is for the route which we've already matched to this path
get_params(/** @type {RegExpExecArray} */ (pattern.exec(path)))
get_params(/** @type {RegExpExecArray} */ (pattern.exec(path)))
: {};

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

if (res.status === 204) {
// fallthrough
return;
if (route !== routes[routes.length - 1]) {
// fallthrough
return;
}
props = {};
} else {
props = await res.json();
}
props = await res.json();
} else {
status = res.status;
error = new Error('Failed to load data');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<a href="/shadowed/error-get">error-get</a>
<a href="/shadowed/no-get">no-get</a>
<a href="/shadowed/dynamic/foo">dynamic/foo</a>
<a href="/shadowed/missing-get">missing-get</a>

<form action="/shadowed/redirect-post" method="post">
<button type="submit" id="redirect-post">redirect</button>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const post = () => {};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>post without get</h1>
17 changes: 17 additions & 0 deletions packages/kit/test/apps/basics/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,23 @@ test.describe.parallel('Shadowed pages', () => {
await clicknav('[href="/shadowed/redirect/a"]');
expect(await page.textContent('h1')).toBe('done');
});

test('Endpoint without GET', async ({ page, clicknav, baseURL, javaScriptEnabled }) => {
await page.goto('/shadowed');

/** @type {string[]} */
const requests = [];
page.on('request', (r) => requests.push(r.url()));

await clicknav('[href="/shadowed/missing-get"]');

expect(await page.textContent('h1')).toBe(`post without get`);

// check that the router didn't fall back to the server
if (javaScriptEnabled) {
expect(requests).not.toContain(`${baseURL}/shadowed/missing-get`);
}
});
});

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