Skip to content
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/early-otters-enter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: set initial navigation's `from` to null when SSR is disabled
15 changes: 9 additions & 6 deletions packages/kit/src/runtime/client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -3755,12 +3755,15 @@ function create_navigation(

/** @type {(Navigation | AfterNavigate) & { type: T }} */
const navigation = /** @type {any} */ ({
from: {
params: current.params,
route: { id: current.route?.id ?? null },
url: current.url,
scroll: scroll_state()
},
from:
type === 'enter'
? null
: {
params: current.params,
route: { id: current.route?.id ?? null },
url: current.url,
scroll: scroll_state()
},
to: url && {
params: target ? target.params : (intent?.params ?? null),
route: { id: target ? (target.route?.id ?? null) : (intent?.route?.id ?? null) },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
let route_id;
/** @type {string | undefined} */
let params;
let from = '';

afterNavigate((navigation) => {
from = JSON.stringify(navigation.from);
route_id = navigation.to?.route.id;
params = JSON.stringify(navigation.to?.params);
});
</script>

<h1>route: {route_id}</h1>
<p>params: {params}</p>
<pre>{from}</pre>
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@

let count = 0;
let type = '';
let from = '';
let to = '';

afterNavigate((event) => {
count += 1;
type = event.type;
from = JSON.stringify(event.from);
to = JSON.stringify(event.to);
});
</script>

<p>{type.toString()} {count}</p>
<pre class="from">{from}</pre>
<pre class="to">{to}</pre>
24 changes: 24 additions & 0 deletions packages/kit/test/apps/basics/test/playwright/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,30 @@ test.describe('SPA mode / no SSR', () => {
);
});

test('afterNavigate enter navigation has no from target when SSR is disabled', async ({
page,
app
}) => {
const path = '/no-ssr/after-navigate';
await page.goto(path);
await expect(page.locator('pre.from')).toHaveText('null');
const target = {
params: {},
route: { id: path },
url: page.url(),
scroll: { x: 0, y: 0 }
};
expect(JSON.parse(await page.locator('pre.to').innerText())).toEqual(target);

await app.goto(`${path}?next`);
await expect(page.locator('p')).toHaveText('goto 2');
expect(JSON.parse(await page.locator('pre.from').innerText())).toEqual(target);

await page.reload();
await expect(page.locator('p')).toHaveText('enter 1');
await expect(page.locator('pre.from')).toHaveText('null');
});

test('afterNavigate is only called once during start', async ({ page }) => {
await page.goto('/no-ssr/after-navigate');
await expect(page.locator('p')).toHaveText('enter 1');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@
'route: /navigation-lifecycle/after-navigate-enter/[slug]'
);
expect(await page.textContent('p')).toBe('params: {"slug":"x"}');
await expect(page.locator('pre')).toHaveText('null');
});

test('onNavigate calls callback', async ({ page, clicknav }) => {
Expand Down Expand Up @@ -1477,7 +1478,7 @@

test.describe('Load', () => {
if (process.env.DEV) {
test('using window.fetch does not cause false-positive warning', async ({ page }) => {

Check warning on line 1481 in packages/kit/test/apps/basics/test/playwright/cross-platform/client.test.js

View workflow job for this annotation

GitHub Actions / e2e (test:cross-platform:dev, macOS-latest, webkit, 3)

flaky test: using window.fetch does not cause false-positive warning

retries: 2
/** @type {string[]} */
const warnings = [];
page.on('console', (msg) => {
Expand Down
Loading