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 inconsistent rendering of shadow routes on client/server-side #3780

Merged
5 changes: 5 additions & 0 deletions .changeset/curvy-experts-try.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Make shadow endpoint `event.url` consistent between server and client navigation
2 changes: 1 addition & 1 deletion packages/kit/src/runtime/client/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,7 @@ export class Renderer {

if (has_shadow && i === a.length - 1) {
const res = await fetch(
`${url.pathname}${url.pathname.endsWith('/') ? '' : '/'}__data.json`,
`${url.pathname}${url.pathname.endsWith('/') ? '' : '/'}__data.json${url.search}`,
{
headers: {
'x-sveltekit-noredirect': 'true'
Expand Down
12 changes: 11 additions & 1 deletion packages/kit/src/runtime/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,17 @@ export async function respond(request, options, state = {}) {
}

const is_data_request = decoded.endsWith(DATA_SUFFIX);
if (is_data_request) decoded = decoded.slice(0, -DATA_SUFFIX.length) || '/';

if (is_data_request) {
decoded = decoded.slice(0, -DATA_SUFFIX.length) || '/';

const normalized = normalize_path(
url.pathname.slice(0, -DATA_SUFFIX.length),
options.trailing_slash
);

event.url = new URL(event.url.origin + normalized + event.url.search);
}

for (const route of options.manifest._.routes) {
const match = route.pattern.exec(decoded);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<a href="/shadowed/same-render?param1=value1">Click here to navigate</a>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/** @type {import('@sveltejs/kit').RequestHandler} */
export function get({ url }) {
return { body: { url: url.toString() } };
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
/** @type {string} */
export let url;
</script>
<h1>URL: {url}</h1>
6 changes: 6 additions & 0 deletions packages/kit/test/apps/basics/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,12 @@ test.describe.parallel('Shadowed pages', () => {
expect(await response.json()).toEqual({ answer: 42 });
});

test('Endpoint receives consistent URL', async ({ baseURL, page, clicknav }) => {
await page.goto('/shadowed/same-render-entry');
await clicknav('[href="/shadowed/same-render?param1=value1"]');
expect(await page.textContent('h1')).toBe(`URL: ${baseURL}/shadowed/same-render?param1=value1`);
});

test('responds to HEAD requests from endpoint', async ({ request }) => {
const url = '/shadowed/simple';

Expand Down