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

Try to fix SPA initial behavior for 404s #4116

Merged
Merged
Show file tree
Hide file tree
Changes from 6 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/blue-parents-invite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fixes SPA mode error handling
Rich-Harris marked this conversation as resolved.
Show resolved Hide resolved
3 changes: 2 additions & 1 deletion packages/adapter-static/test/apps/spa/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
"scripts": {
"dev": "../../../../kit/svelte-kit.js dev",
"build": "../../../../kit/svelte-kit.js build",
"start": "../../../../kit/svelte-kit.js start"
"start": "sirv -s 200.html build"
},
"devDependencies": {
"@sveltejs/adapter-node": "next",
"@sveltejs/kit": "next",
"sirv-cli": "^2.0.2",
"svelte": "^3.43.0"
},
"type": "module"
Expand Down
7 changes: 7 additions & 0 deletions packages/adapter-static/test/apps/spa/src/app.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/// <reference types="@sveltejs/kit" />

declare namespace App {
interface Session {
count: number;
}
}
5 changes: 5 additions & 0 deletions packages/adapter-static/test/apps/spa/src/hooks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export function getSession() {
return {
count: 0
};
}
25 changes: 25 additions & 0 deletions packages/adapter-static/test/apps/spa/src/routes/__error.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<script context="module">
/** @type {import('@sveltejs/kit').Load} */
export function load({ session }) {
return {
props: session
};
}
</script>

<script>
import { browser } from '$app/env';
import { page, session } from '$app/stores';

/** @type {number} */
export let count;

if (browser) {
$session.count += 1;
}
</script>

<h1>{$page.status}</h1>
<h2>count: {count}</h2>

<button on:click={() => ($session.count += 1)}>+1</button>
3 changes: 3 additions & 0 deletions packages/adapter-static/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,8 @@ run('spa', (test) => {
test('renders error page for missing page', async ({ base, page }) => {
await page.goto(`${base}/nosuchpage`);
assert.equal(await page.textContent('h1'), '404');
assert.equal(await page.textContent('h2'), 'count: 1');

await page.waitForLoadState('networkidle', { timeout: 1000 });
});
});
16 changes: 8 additions & 8 deletions packages/kit/src/runtime/client/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,14 @@ export class Renderer {
const token = (this.token = {});
let navigation_result = await this._get_navigation_result(info, no_cache);

if (!navigation_result && info.url.pathname === location.pathname) {
navigation_result = await this._load_error({
status: 404,
error: new Error(`Not found: ${info.url.pathname}`),
url: info.url
});
}

if (!navigation_result) {
location.href = info.url.href;
return;
Expand Down Expand Up @@ -507,14 +515,6 @@ export class Renderer {
);
if (result) return result;
}

if (info.initial) {
return await this._load_error({
status: 404,
error: new Error(`Not found: ${info.url.pathname}`),
url: info.url
});
}
}

/**
Expand Down
5 changes: 2 additions & 3 deletions packages/kit/src/runtime/client/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export class Router {
* base: string;
* routes: import('types').CSRRoute[];
* trailing_slash: import('types').TrailingSlash;
* renderer: import('./renderer').Renderer
* renderer: import('./renderer').Renderer;
* }} opts
*/
constructor({ base, routes, trailing_slash, renderer }) {
Expand Down Expand Up @@ -293,8 +293,7 @@ export class Router {
id: url.pathname + url.search,
routes: this.routes.filter(([pattern]) => pattern.test(path)),
url,
path,
initial: !this.initialized
path
};
}
}
Expand Down
1 change: 0 additions & 1 deletion packages/kit/src/runtime/client/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ export type NavigationInfo = {
routes: CSRRoute[];
url: URL;
path: string;
initial: boolean;
};

export type NavigationCandidate = {
Expand Down
89 changes: 77 additions & 12 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.