Skip to content

fix: cache location.origin on startup #11004

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

Merged
merged 2 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/small-readers-decide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: cache location.origin on startup
7 changes: 4 additions & 3 deletions packages/kit/src/runtime/client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ import {
get_link_info,
get_router_options,
is_external_url,
scroll_state
scroll_state,
origin
} from './utils.js';

import { base } from '__sveltekit/paths';
Expand Down Expand Up @@ -843,7 +844,7 @@ export function create_client(app, target) {
} catch {
// at this point we have no choice but to fall back to the server, if it wouldn't
// bring us right back here, turning this into an endless loop
if (url.origin !== location.origin || url.pathname !== location.pathname || hydrated) {
if (url.origin !== origin || url.pathname !== location.pathname || hydrated) {
await native_navigation(url);
}
}
Expand Down Expand Up @@ -1173,7 +1174,7 @@ export function create_client(app, target) {
* @returns {Promise<import('./types').NavigationFinished>}
*/
async function server_fallback(url, route, error, status) {
if (url.origin === location.origin && url.pathname === location.pathname && !hydrated) {
if (url.origin === origin && url.pathname === location.pathname && !hydrated) {
// We would reload the same page we're currently on, which isn't hydrated,
// which means no SSR, which means we would end up in an endless loop
return await load_root_error_page({
Expand Down
6 changes: 4 additions & 2 deletions packages/kit/src/runtime/client/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { PRELOAD_PRIORITIES } from './constants.js';

/* global __SVELTEKIT_APP_VERSION_FILE__, __SVELTEKIT_APP_VERSION_POLL_INTERVAL__ */

export const origin = BROWSER ? location.origin : '';

/** @param {HTMLDocument} doc */
export function get_base_uri(doc) {
let baseURI = doc.baseURI;
Expand Down Expand Up @@ -135,7 +137,7 @@ export function get_link_info(a, base) {
is_external_url(url, base) ||
(a.getAttribute('rel') || '').split(/\s+/).includes('external');

const download = url?.origin === location.origin && a.hasAttribute('download');
const download = url?.origin === origin && a.hasAttribute('download');

return { url, external, target, download };
}
Expand Down Expand Up @@ -290,5 +292,5 @@ export function create_updated_store() {
* @param {string} base
*/
export function is_external_url(url, base) {
return url.origin !== location.origin || !url.pathname.startsWith(base);
return url.origin !== origin || !url.pathname.startsWith(base);
}