Skip to content

Commit

Permalink
fix: strip internal data before passing URL to reroute (#13092)
Browse files Browse the repository at this point in the history
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 <5968653+dummdidumm@users.noreply.github.com>
  • Loading branch information
eltigerchino and dummdidumm authored Jan 17, 2025
1 parent 37f72fb commit 6774ebc
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 17 deletions.
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);
}

// 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

0 comments on commit 6774ebc

Please sign in to comment.