Skip to content

Commit 2a39564

Browse files
committed
fix: follow whatwg fetch spec for handling redirect loops (#10838)
1 parent c0d128c commit 2a39564

File tree

2 files changed

+19
-18
lines changed

2 files changed

+19
-18
lines changed

.changeset/proud-worms-press.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
fix: follow whatwg fetch spec for handling redirect loops

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

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ export function create_client(app, target) {
173173

174174
if (navigation_result) {
175175
if (navigation_result.type === 'redirect') {
176-
return goto(new URL(navigation_result.location, url).href, {}, [url.pathname], nav_token);
176+
return goto(new URL(navigation_result.location, url).href, {}, 1, nav_token);
177177
} else {
178178
if (navigation_result.props.page !== undefined) {
179179
page = navigation_result.props.page;
@@ -208,7 +208,7 @@ export function create_client(app, target) {
208208
/**
209209
* @param {string | URL} url
210210
* @param {{ noScroll?: boolean; replaceState?: boolean; keepFocus?: boolean; state?: any; invalidateAll?: boolean }} opts
211-
* @param {string[]} redirect_chain
211+
* @param {number} redirect_count
212212
* @param {{}} [nav_token]
213213
*/
214214
async function goto(
@@ -220,7 +220,7 @@ export function create_client(app, target) {
220220
state = {},
221221
invalidateAll = false
222222
},
223-
redirect_chain,
223+
redirect_count,
224224
nav_token
225225
) {
226226
if (typeof url === 'string') {
@@ -231,7 +231,7 @@ export function create_client(app, target) {
231231
url,
232232
scroll: noScroll ? scroll_state() : null,
233233
keepfocus: keepFocus,
234-
redirect_chain,
234+
redirect_count,
235235
details: {
236236
state,
237237
replaceState
@@ -941,7 +941,7 @@ export function create_client(app, target) {
941941
* url: URL;
942942
* scroll: { x: number, y: number } | null;
943943
* keepfocus: boolean;
944-
* redirect_chain: string[];
944+
* redirect_count: number;
945945
* details: {
946946
* replaceState: boolean;
947947
* state: any;
@@ -957,7 +957,7 @@ export function create_client(app, target) {
957957
url,
958958
scroll,
959959
keepfocus,
960-
redirect_chain,
960+
redirect_count,
961961
details,
962962
type,
963963
delta,
@@ -1014,7 +1014,8 @@ export function create_client(app, target) {
10141014
}
10151015

10161016
if (navigation_result.type === 'redirect') {
1017-
if (redirect_chain.length > 10 || redirect_chain.includes(url.pathname)) {
1017+
// whatwg fetch spec https://fetch.spec.whatwg.org/#http-redirect-fetch says to error after 20 redirects
1018+
if (redirect_count >= 20) {
10181019
navigation_result = await load_root_error_page({
10191020
status: 500,
10201021
error: await handle_error(new Error('Redirect loop'), {
@@ -1026,12 +1027,7 @@ export function create_client(app, target) {
10261027
route: { id: null }
10271028
});
10281029
} else {
1029-
goto(
1030-
new URL(navigation_result.location, url).href,
1031-
{},
1032-
[...redirect_chain, url.pathname],
1033-
nav_token
1034-
);
1030+
goto(new URL(navigation_result.location, url).href, {}, redirect_count + 1, nav_token);
10351031
return false;
10361032
}
10371033
} else if (/** @type {number} */ (navigation_result.props.page?.status) >= 400) {
@@ -1379,7 +1375,7 @@ export function create_client(app, target) {
13791375
},
13801376

13811377
goto: (href, opts = {}) => {
1382-
return goto(href, opts, []);
1378+
return goto(href, opts, 0);
13831379
},
13841380

13851381
invalidate: (resource) => {
@@ -1440,7 +1436,7 @@ export function create_client(app, target) {
14401436
tick().then(reset_focus);
14411437
}
14421438
} else if (result.type === 'redirect') {
1443-
goto(result.location, { invalidateAll: true }, []);
1439+
goto(result.location, { invalidateAll: true }, 0);
14441440
} else {
14451441
/** @type {Record<string, any>} */
14461442
root.$set({
@@ -1595,7 +1591,7 @@ export function create_client(app, target) {
15951591
url,
15961592
scroll: options.noscroll ? scroll_state() : null,
15971593
keepfocus: options.keep_focus ?? false,
1598-
redirect_chain: [],
1594+
redirect_count: 0,
15991595
details: {
16001596
state: {},
16011597
replaceState: options.replace_state ?? url.href === location.href
@@ -1649,7 +1645,7 @@ export function create_client(app, target) {
16491645
url,
16501646
scroll: noscroll ? scroll_state() : null,
16511647
keepfocus: keep_focus ?? false,
1652-
redirect_chain: [],
1648+
redirect_count: 0,
16531649
details: {
16541650
state: {},
16551651
replaceState: replace_state ?? url.href === location.href
@@ -1687,7 +1683,7 @@ export function create_client(app, target) {
16871683
url,
16881684
scroll,
16891685
keepfocus: false,
1690-
redirect_chain: [],
1686+
redirect_count: 0,
16911687
details: null,
16921688
accepted: () => {
16931689
current_history_index = event.state[INDEX_KEY];

0 commit comments

Comments
 (0)