Skip to content

Commit 6774ebc

Browse files
fix: strip internal data before passing URL to reroute (#13092)
Decoding the pathname etc is irrelevant for stripping the internal data that shows us whether or not this is a data request, so move that logic before the reroute hook. fixes #11625 --------- Co-authored-by: Simon H <[email protected]>
1 parent 37f72fb commit 6774ebc

File tree

6 files changed

+44
-17
lines changed

6 files changed

+44
-17
lines changed

.changeset/curvy-trains-dress.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
fix: strip internal data before passing URL to `reroute`

packages/kit/src/runtime/server/respond.js

+15-16
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,21 @@ export async function respond(request, options, manifest, state) {
8585
return text('Not found', { status: 404 });
8686
}
8787

88+
const is_data_request = has_data_suffix(url.pathname);
89+
/** @type {boolean[] | undefined} */
90+
let invalidated_data_nodes;
91+
if (is_data_request) {
92+
url.pathname =
93+
strip_data_suffix(url.pathname) +
94+
(url.searchParams.get(TRAILING_SLASH_PARAM) === '1' ? '/' : '') || '/';
95+
url.searchParams.delete(TRAILING_SLASH_PARAM);
96+
invalidated_data_nodes = url.searchParams
97+
.get(INVALIDATED_PARAM)
98+
?.split('')
99+
.map((node) => node === '1');
100+
url.searchParams.delete(INVALIDATED_PARAM);
101+
}
102+
88103
// reroute could alter the given URL, so we pass a copy
89104
let rerouted_path;
90105
try {
@@ -126,22 +141,6 @@ export async function respond(request, options, manifest, state) {
126141
return text('Not found', { status: 404, headers });
127142
}
128143

129-
const is_data_request = has_data_suffix(decoded);
130-
/** @type {boolean[] | undefined} */
131-
let invalidated_data_nodes;
132-
if (is_data_request) {
133-
decoded = strip_data_suffix(decoded) || '/';
134-
url.pathname =
135-
strip_data_suffix(url.pathname) +
136-
(url.searchParams.get(TRAILING_SLASH_PARAM) === '1' ? '/' : '') || '/';
137-
url.searchParams.delete(TRAILING_SLASH_PARAM);
138-
invalidated_data_nodes = url.searchParams
139-
.get(INVALIDATED_PARAM)
140-
?.split('')
141-
.map((node) => node === '1');
142-
url.searchParams.delete(INVALIDATED_PARAM);
143-
}
144-
145144
if (!state.prerendering?.fallback) {
146145
// TODO this could theoretically break — should probably be inside a try-catch
147146
const matchers = await manifest._.matchers();

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import { Foo } from './lib';
44
const mapping = {
55
'/reroute/basic/a': '/reroute/basic/b',
66
'/reroute/client-only-redirect/a': '/reroute/client-only-redirect/b',
7-
'/reroute/preload-data/a': '/reroute/preload-data/b'
7+
'/reroute/preload-data/a': '/reroute/preload-data/b',
8+
'/reroute/invalidate/a': '/reroute/invalidate'
89
};
910

1011
/** @type {import("@sveltejs/kit").Reroute} */
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export function load({ isDataRequest }) {
2+
return {
3+
request: isDataRequest
4+
};
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<script>
2+
import { invalidateAll } from '$app/navigation';
3+
4+
export let data;
5+
</script>
6+
7+
<button on:click={async () => await invalidateAll()}>Invalidate</button>
8+
9+
{#if data.request}
10+
<p>data request: {data.request}</p>
11+
{/if}

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

+6
Original file line numberDiff line numberDiff line change
@@ -1399,6 +1399,12 @@ test.describe('reroute', () => {
13991399

14001400
expect(await page.textContent('h1')).toContain('Full Navigation');
14011401
});
1402+
1403+
test('reroute works with invalidate', async ({ page }) => {
1404+
await page.goto('/reroute/invalidate/a');
1405+
await page.click('button');
1406+
await expect(page.locator('p')).toHaveText('data request: true');
1407+
});
14021408
});
14031409

14041410
test.describe('init', () => {

0 commit comments

Comments
 (0)