Skip to content

Commit

Permalink
fix: prevent false positive history.pushState warnings (#11858)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
eltigerchino authored Feb 19, 2024
1 parent d94a82a commit 511126b
Showing 2 changed files with 9 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/loud-kangaroos-leave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@sveltejs/kit": patch
---

fix: prevent false positive `history.pushState` and `history.replaceState` warnings
5 changes: 4 additions & 1 deletion packages/kit/src/runtime/client/client.js
Original file line number Diff line number Diff line change
@@ -67,6 +67,8 @@ const snapshots = storage.get(SNAPSHOT_KEY) ?? {};
if (DEV && BROWSER) {
let warned = false;

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

const warn = () => {
if (warned) return;

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

warned = true;

0 comments on commit 511126b

Please sign in to comment.