Skip to content

Commit

Permalink
fix: follow whatwg fetch spec for handling redirect loops (#10857)
Browse files Browse the repository at this point in the history
  • Loading branch information
moatra authored Oct 11, 2023
1 parent ff6cd45 commit be2c8a8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
5 changes: 5 additions & 0 deletions .changeset/proud-worms-press.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: follow whatwg fetch spec for handling redirect loops
32 changes: 14 additions & 18 deletions packages/kit/src/runtime/client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ export function create_client(app, target) {

if (navigation_result) {
if (navigation_result.type === 'redirect') {
return goto(new URL(navigation_result.location, url).href, {}, [url.pathname], nav_token);
return goto(new URL(navigation_result.location, url).href, {}, 1, nav_token);
} else {
if (navigation_result.props.page !== undefined) {
page = navigation_result.props.page;
Expand Down Expand Up @@ -208,7 +208,7 @@ export function create_client(app, target) {
/**
* @param {string | URL} url
* @param {{ noScroll?: boolean; replaceState?: boolean; keepFocus?: boolean; state?: any; invalidateAll?: boolean }} opts
* @param {string[]} redirect_chain
* @param {number} redirect_count
* @param {{}} [nav_token]
*/
async function goto(
Expand All @@ -220,7 +220,7 @@ export function create_client(app, target) {
state = {},
invalidateAll = false
},
redirect_chain,
redirect_count,
nav_token
) {
if (typeof url === 'string') {
Expand All @@ -231,7 +231,7 @@ export function create_client(app, target) {
url,
scroll: noScroll ? scroll_state() : null,
keepfocus: keepFocus,
redirect_chain,
redirect_count,
details: {
state,
replaceState
Expand Down Expand Up @@ -941,7 +941,7 @@ export function create_client(app, target) {
* url: URL;
* scroll: { x: number, y: number } | null;
* keepfocus: boolean;
* redirect_chain: string[];
* redirect_count: number;
* details: {
* replaceState: boolean;
* state: any;
Expand All @@ -957,7 +957,7 @@ export function create_client(app, target) {
url,
scroll,
keepfocus,
redirect_chain,
redirect_count,
details,
type,
delta,
Expand Down Expand Up @@ -1014,7 +1014,8 @@ export function create_client(app, target) {
}

if (navigation_result.type === 'redirect') {
if (redirect_chain.length > 10 || redirect_chain.includes(url.pathname)) {
// whatwg fetch spec https://fetch.spec.whatwg.org/#http-redirect-fetch says to error after 20 redirects
if (redirect_count >= 20) {
navigation_result = await load_root_error_page({
status: 500,
error: await handle_error(new Error('Redirect loop'), {
Expand All @@ -1026,12 +1027,7 @@ export function create_client(app, target) {
route: { id: null }
});
} else {
goto(
new URL(navigation_result.location, url).href,
{},
[...redirect_chain, url.pathname],
nav_token
);
goto(new URL(navigation_result.location, url).href, {}, redirect_count + 1, nav_token);
return false;
}
} else if (/** @type {number} */ (navigation_result.props.page?.status) >= 400) {
Expand Down Expand Up @@ -1379,7 +1375,7 @@ export function create_client(app, target) {
},

goto: (href, opts = {}) => {
return goto(href, opts, []);
return goto(href, opts, 0);
},

invalidate: (resource) => {
Expand Down Expand Up @@ -1440,7 +1436,7 @@ export function create_client(app, target) {
tick().then(reset_focus);
}
} else if (result.type === 'redirect') {
goto(result.location, { invalidateAll: true }, []);
goto(result.location, { invalidateAll: true }, 0);
} else {
/** @type {Record<string, any>} */
root.$set({
Expand Down Expand Up @@ -1595,7 +1591,7 @@ export function create_client(app, target) {
url,
scroll: options.noscroll ? scroll_state() : null,
keepfocus: options.keep_focus ?? false,
redirect_chain: [],
redirect_count: 0,
details: {
state: {},
replaceState: options.replace_state ?? url.href === location.href
Expand Down Expand Up @@ -1649,7 +1645,7 @@ export function create_client(app, target) {
url,
scroll: noscroll ? scroll_state() : null,
keepfocus: keep_focus ?? false,
redirect_chain: [],
redirect_count: 0,
details: {
state: {},
replaceState: replace_state ?? url.href === location.href
Expand Down Expand Up @@ -1687,7 +1683,7 @@ export function create_client(app, target) {
url,
scroll,
keepfocus: false,
redirect_chain: [],
redirect_count: 0,
details: null,
accepted: () => {
current_history_index = event.state[INDEX_KEY];
Expand Down

0 comments on commit be2c8a8

Please sign in to comment.