Skip to content
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

fix: prevent false positive history.pushState warnings #11858

Merged
merged 7 commits into from
Feb 19, 2024

Conversation

eltigerchino
Copy link
Member

@eltigerchino eltigerchino commented Feb 16, 2024

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)

if (stack[0].includes(import.meta.url)) return;

The solution is to simply return the URL without the query parameters if any.

No test because so far it's only reproducible when Vite is loading SvelteKit from node_modules rather than being linked through the workspace (how it's currently setup in the kit repository).

Aside

This addresses the original issue of:

I'm not using history as suggested by the warning. I'm using goto in a couple of places, from $app/navigation. I haven't changed anything. The warning is not present with v2.3.3.

From a quick test, commenting out all the goto calls still resulted in the warning.

but it does not address the possibility that warn checks the wrong stack entry. This can happen when other scripts also monkey patch history.pushState causing our client.js to appear earlier or later in the call stack.

I think we're safe from third-party scripts monkey patching and upsetting the call stack order. I've tested monkey patching it in:

  • the <script> of a svelte file
  • the html template
  • the svelte:head of a svelte file

and they get called in that order, but warn is always first in the call stack, followed by the client.js monkey patch.


Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Changesets

  • If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpm changeset and following the prompts. Changesets that add features should be minor and those that fix bugs should be patch. Please prefix changeset messages with feat:, fix:, or chore:.

Edits

  • Please ensure that 'Allow edits from maintainers' is checked. PRs without this option may be closed.

Copy link

changeset-bot bot commented Feb 16, 2024

🦋 Changeset detected

Latest commit: f6d7017

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@sveltejs/kit Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link
Member

@dummdidumm dummdidumm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for testing all the different cases!

@dummdidumm dummdidumm merged commit 511126b into main Feb 19, 2024
13 checks passed
@dummdidumm dummdidumm deleted the fix-false-positive-pushstate-warnings branch February 19, 2024 09:14
@github-actions github-actions bot mentioned this pull request Feb 19, 2024
@ebeloded
Copy link

I'm still getting the warning when using Sentry integration. As soon as Sentry.init is called, the warning shows up.

Not sure whether it should be reported here or in Sentry repo.

@eltigerchino
Copy link
Member Author

eltigerchino commented Mar 12, 2024

I'm still getting the warning when using Sentry integration. As soon as Sentry.init is called, the warning shows up.

Not sure whether it should be reported here or in Sentry repo.

Thanks for reporting this. Can you open a new issue with a minimal reproduction?

@linsen
Copy link

linsen commented Apr 30, 2024

I'm experiencing the same warning when using Sentry integration. The warning appears on the first navigation after a hard page load (but not on subsequent client-side navigation between pages).

Here's a minimal repo reproducing the issue: https://github.com/linsen/sveltekit-sentry-warning

Should I open a new issue for this? I don't see one that's currently open.

@eltigerchino
Copy link
Member Author

I'm experiencing the same warning when using Sentry integration. The warning appears on the first navigation after a hard page load (but not on subsequent client-side navigation between pages).

Here's a minimal repo reproducing the issue: linsen/sveltekit-sentry-warning

Should I open a new issue for this? I don't see one that's currently open.

Hi Linsen. Thanks for reporting this. Yes, you’re encouraged to open a new issue for this.

@evdama
Copy link

evdama commented Dec 18, 2024

I'm not using Sentry nor do I use history.pushState() or history.replaceState() anywhere in my app. As mentioned above, The warning appears on the first navigation after a hard page load (but not on subsequent client-side navigation between pages).

Screenshot 2024-12-18 at 16 44 45

My svelte/kit versions:

sa@Mac: ~/0/edm  |development ✓| gr @svelte package.json
package.json:25:    "@sveltejs/adapter-vercel": "^5.5.2",
package.json:26:    "@sveltejs/kit": "^2.12.1",
package.json:27:    "@sveltejs/vite-plugin-svelte": "^5.0.3",
sa@Mac: ~/0/edm  |development ✓|

@eltigerchino
Copy link
Member Author

I'm not using Sentry nor do I use history.pushState() or history.replaceState() anywhere in my app. As mentioned above, The warning appears on the first navigation after a hard page load (but not on subsequent client-side navigation between pages).

Screenshot 2024-12-18 at 16 44 45

My svelte/kit versions:

sa@Mac: ~/0/edm  |development ✓| gr @svelte package.json
package.json:25:    "@sveltejs/adapter-vercel": "^5.5.2",
package.json:26:    "@sveltejs/kit": "^2.12.1",
package.json:27:    "@sveltejs/vite-plugin-svelte": "^5.0.3",
sa@Mac: ~/0/edm  |development ✓|

Please open a new issue with a minimal reproduction

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

False positive pushstate warnings
5 participants