Skip to content

Commit 7397a20

Browse files
author
tom
committed
fix bug and add test
1 parent a0f5589 commit 7397a20

File tree

9 files changed

+70
-14
lines changed

9 files changed

+70
-14
lines changed

packages/kit/src/runtime/client/renderer.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -724,11 +724,11 @@ export class Renderer {
724724
/**
725725
* @param {import('./types').NavigationCandidate} selected
726726
* @param {boolean} no_cache
727-
* @param {string} [next_route_key]
728-
* @param {Record<string, any>} [next_route_props]
727+
* @param {string} [fallthrough_target]
728+
* @param {Record<string, any>} [fallthrough_props]
729729
* @returns {Promise<import('./types').NavigationResult | undefined>} undefined if fallthrough
730730
*/
731-
async _load({ route, info }, no_cache, next_route_key, next_route_props) {
731+
async _load({ route, info }, no_cache, fallthrough_target, fallthrough_props) {
732732
const { url, path, routes } = info;
733733
const key = url.pathname + url.search;
734734
if (!no_cache) {
@@ -790,8 +790,8 @@ export class Renderer {
790790
const is_shadow_page = shadow_key !== undefined && i === a.length - 1;
791791

792792
if (is_shadow_page) {
793-
if (next_route_key !== undefined && shadow_key === next_route_key) {
794-
if (next_route_props) props = next_route_props;
793+
if (fallthrough_target !== undefined && shadow_key === fallthrough_target) {
794+
if (fallthrough_props) props = fallthrough_props;
795795
} else {
796796
const res = await fetch(
797797
`${url.pathname}${url.pathname.endsWith('/') ? '' : '/'}__data.json${url.search}`,
@@ -804,7 +804,7 @@ export class Renderer {
804804
);
805805
if (res.ok) {
806806
const redirect = res.headers.get('x-sveltekit-location');
807-
const route_key = res.headers.get('x-sveltekit-load');
807+
const route_index = res.headers.get('x-sveltekit-load');
808808
if (redirect) {
809809
return {
810810
redirect,
@@ -813,15 +813,15 @@ export class Renderer {
813813
};
814814
}
815815
// '.' means fallthrough from server-side not match any router
816-
if (route_key === '.') return;
817-
props = await res.json();
818-
if (route_key) {
819-
const next_route = routes.find((r) => r[4] === route_key);
816+
if (route_index === '-1') return;
817+
props = res.status === 204 ? {} : await res.json();
818+
if (route_index) {
819+
const next_route = routes[+route_index];
820820
if (next_route) {
821821
return await this._load(
822822
{ route: next_route, info },
823823
no_cache,
824-
route_key,
824+
next_route[4],
825825
props
826826
);
827827
}

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ export async function respond(request, options, state = {}) {
153153

154154
const shadow_key = request.headers.get('x-sveltekit-load');
155155
let from_fallthrough = false;
156+
let route_index = -1;
156157

157158
for (const route of options.manifest._.routes) {
158159
const match = route.pattern.exec(decoded);
@@ -162,6 +163,7 @@ export async function respond(request, options, state = {}) {
162163
/** @type {Response | undefined} */
163164
let response;
164165
const is_page = route.type === 'page';
166+
if (is_page) route_index++;
165167
if (is_data_request && is_page && route.shadow) {
166168
if (!from_fallthrough && shadow_key && shadow_key !== route.key) continue;
167169
if (route.shadow) {
@@ -184,7 +186,7 @@ export async function respond(request, options, state = {}) {
184186
if (from_fallthrough && response.ok) {
185187
const headers = new Headers(response.headers);
186188
// client-site will fallthrough to the route
187-
headers.set('x-sveltekit-load', route.key);
189+
headers.set('x-sveltekit-load', `${route_index}`);
188190
response = new Response(response.body, {
189191
status: response.status,
190192
headers
@@ -201,7 +203,7 @@ export async function respond(request, options, state = {}) {
201203
if (is_page) {
202204
if (from_fallthrough) {
203205
const headers = new Headers({
204-
'x-sveltekit-load': route.key
206+
'x-sveltekit-load': `${route_index}`
205207
});
206208
// next match not a shadow so fallthrough at client-side
207209
return new Response(undefined, {
@@ -256,7 +258,7 @@ export async function respond(request, options, state = {}) {
256258
// no match route
257259
if (from_fallthrough) {
258260
const headers = new Headers({
259-
'x-sveltekit-load': '.'
261+
'x-sveltekit-load': '-1'
260262
});
261263
// next match not a shadow so fallthrough at client-side
262264
return new Response(undefined, {
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/** @type {import('@sveltejs/kit').RequestHandler} */
2+
export async function get({ params }) {
3+
const param = params.a;
4+
if (param !== 'a') {
5+
return {
6+
fallthrough: true
7+
};
8+
}
9+
10+
return {
11+
status: 200,
12+
body: { param }
13+
};
14+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
/** @type string|undefined */
3+
export let param;
4+
</script>
5+
<h2>a-{param}</h2>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/** @type {import('@sveltejs/kit').RequestHandler} */
2+
export async function get({ params }) {
3+
const param = params.b;
4+
if (param !== 'b') {
5+
return {
6+
fallthrough: true
7+
};
8+
}
9+
10+
return {
11+
status: 200,
12+
body: { param }
13+
};
14+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
/** @type string|undefined */
3+
export let param;
4+
</script>
5+
<h2>b-{param}</h2>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<h2>c</h2>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<a href="/shadowed/fallthrough/a">fallthrough to shadow a</a>
2+
<a id="b" href="/shadowed/fallthrough/b">fallthrough to shadow b</a>
3+
<a href="/shadowed/fallthrough/c">fallthrough to no shadow c</a>

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,18 @@ test.describe.parallel('Shadowed pages', () => {
504504
await clicknav('[href="/shadowed/dynamic/bar"]');
505505
expect(await page.textContent('h1')).toBe('slug: bar');
506506
});
507+
508+
test('Shadow fallthrough shadow', async ({ page, clicknav }) => {
509+
await page.goto('/shadowed/fallthrough');
510+
await clicknav('[href="/shadowed/fallthrough/b"]');
511+
expect(await page.textContent('h2')).toBe('b-b');
512+
});
513+
514+
test('Shadow fallthrough to no_shadow', async ({ page, clicknav }) => {
515+
await page.goto('/shadowed/fallthrough');
516+
await clicknav('[href="/shadowed/fallthrough/c"]');
517+
expect(await page.textContent('h2')).toBe('c');
518+
});
507519
});
508520

509521
test.describe.parallel('Endpoints', () => {

0 commit comments

Comments
 (0)