Skip to content

Commit 511126b

Browse files
authored
fix: prevent false positive history.pushState warnings (#11858)
fixes #11671 The warning currently incorrectly fires when we use history.pushState() internally such as clicking on a link. You can test this in a stackblitz starter project. This is because one of the early exit conditions in the warning method is failing incorrectly. import.meta.url returns the client.js file with a query parameter when loading it from node_modules. This causes the substring search for the module filename in the error stack to always return false (because of the query params that are not present in the error stack) The solution is to simply return the URL without the query parameters if any.
1 parent d94a82a commit 511126b

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

.changeset/loud-kangaroos-leave.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: prevent false positive `history.pushState` and `history.replaceState` warnings

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ const snapshots = storage.get(SNAPSHOT_KEY) ?? {};
6767
if (DEV && BROWSER) {
6868
let warned = false;
6969

70+
const current_module_url = import.meta.url.split('?')[0]; // remove query params that vite adds to the URL when it is loaded from node_modules
71+
7072
const warn = () => {
7173
if (warned) return;
7274

@@ -76,7 +78,8 @@ if (DEV && BROWSER) {
7678
if (!stack) return;
7779
if (!stack[0].includes('https:') && !stack[0].includes('http:')) stack = stack.slice(1); // Chrome includes the error message in the stack
7880
stack = stack.slice(2); // remove `warn` and the place where `warn` was called
79-
if (stack[0].includes(import.meta.url)) return;
81+
// Can be falsy if was called directly from an anonymous function
82+
if (stack[0]?.includes(current_module_url)) return;
8083

8184
warned = true;
8285

0 commit comments

Comments
 (0)