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: strip internal data before passing URL to reroute #13092

Merged
merged 11 commits into from
Jan 17, 2025
5 changes: 5 additions & 0 deletions .changeset/curvy-trains-dress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: strip internal data before passing URL to `reroute`
31 changes: 15 additions & 16 deletions packages/kit/src/runtime/server/respond.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,21 @@ export async function respond(request, options, manifest, state) {
return text('Not found', { status: 404 });
}

const is_data_request = has_data_suffix(url.pathname);
/** @type {boolean[] | undefined} */
let invalidated_data_nodes;
if (is_data_request) {
url.pathname =
strip_data_suffix(url.pathname) +
(url.searchParams.get(TRAILING_SLASH_PARAM) === '1' ? '/' : '') || '/';
url.searchParams.delete(TRAILING_SLASH_PARAM);
invalidated_data_nodes = url.searchParams
.get(INVALIDATED_PARAM)
?.split('')
.map((node) => node === '1');
url.searchParams.delete(INVALIDATED_PARAM);
}
dummdidumm marked this conversation as resolved.
Show resolved Hide resolved

// reroute could alter the given URL, so we pass a copy
let rerouted_path;
try {
Expand Down Expand Up @@ -126,22 +141,6 @@ export async function respond(request, options, manifest, state) {
return text('Not found', { status: 404, headers });
}

const is_data_request = has_data_suffix(decoded);
/** @type {boolean[] | undefined} */
let invalidated_data_nodes;
if (is_data_request) {
decoded = strip_data_suffix(decoded) || '/';
url.pathname =
strip_data_suffix(url.pathname) +
(url.searchParams.get(TRAILING_SLASH_PARAM) === '1' ? '/' : '') || '/';
url.searchParams.delete(TRAILING_SLASH_PARAM);
invalidated_data_nodes = url.searchParams
.get(INVALIDATED_PARAM)
?.split('')
.map((node) => node === '1');
url.searchParams.delete(INVALIDATED_PARAM);
}

if (!state.prerendering?.fallback) {
// TODO this could theoretically break — should probably be inside a try-catch
const matchers = await manifest._.matchers();
Expand Down
3 changes: 2 additions & 1 deletion packages/kit/test/apps/basics/src/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { Foo } from './lib';
const mapping = {
'/reroute/basic/a': '/reroute/basic/b',
'/reroute/client-only-redirect/a': '/reroute/client-only-redirect/b',
'/reroute/preload-data/a': '/reroute/preload-data/b'
'/reroute/preload-data/a': '/reroute/preload-data/b',
'/reroute/invalidate/a': '/reroute/invalidate'
};

/** @type {import("@sveltejs/kit").Reroute} */
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export function load({ isDataRequest }) {
return {
request: isDataRequest
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<script>
import { invalidateAll } from '$app/navigation';

export let data;
</script>

<button on:click={async () => await invalidateAll()}>Invalidate</button>

{#if data.request}
<p>data request: {data.request}</p>
{/if}
6 changes: 6 additions & 0 deletions packages/kit/test/apps/basics/test/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1399,6 +1399,12 @@ test.describe('reroute', () => {

expect(await page.textContent('h1')).toContain('Full Navigation');
});

test('reroute works with invalidate', async ({ page }) => {
await page.goto('/reroute/invalidate/a');
await page.click('button');
await expect(page.locator('p')).toHaveText('data request: true');
});
});

test.describe('init', () => {
Expand Down
Loading