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

Bad developer practice causes JS exception #2195

Open
ipddev opened this issue Jan 16, 2025 · 7 comments
Open

Bad developer practice causes JS exception #2195

ipddev opened this issue Jan 16, 2025 · 7 comments

Comments

@ipddev
Copy link

ipddev commented Jan 16, 2025

Adding href=# to an A tag and then scrolling after clicking triggers an Exception in FireFox (v134.0) with the following error:

Uncaught (in promise) TypeError: window.history.state is null
    saveScrollPositions history.ts:106
    promise callback*saveScrollPositions/< history.ts:103

Although its bad practice, I think it needs to be handled as it still seems quite a common unfortunately, and it took a lot of head scratching to realise what was causing the site to stop working!

Link to line:

page: window.history.state.page,

@danmatthews
Copy link

I have PR for this, we've had the same issue:

#2189

@davidwessman
Copy link

We are experiencing this when doing $inertia.reload as well, some could be replaced by router.poll and it fixes the error, but we have reloads when we do e.g. pagination as well.

@danmatthews
Copy link

@davidwessman are you using replaceState: true in those reloads? Just wondering if that causes the state to be initialised to null and then causes the error.

@davidwessman
Copy link

davidwessman commented Jan 20, 2025

Right now it looks like:

created() {
    this.poll();
  },

  methods: {
    poll() {
      this.$inertia.reload({
        onSuccess: (event) => {
          if (event.component === "PollingShow") {
            setTimeout(this.poll, 1000);
          }
        },
      });
    },
  },

Context: This is Vue3 🙂

@joetannenbaum
Copy link
Contributor

Does anyone have a clean example of producing this error? Trying to write a test that replicates the behavior and having trouble. Clean meaning: fire up a fresh Inertia app and write the simplest script possible to re-create the error.

@danmatthews
Copy link

danmatthews commented Jan 20, 2025

@ipddev @davidwessman can you both drop your package.json's here please? Trying to see if this might be caused by something in a common dependency or something, or version specific to a certain vite version maybe.

EDIT: and possibly your vite.config.js if you don't mind?

@danmatthews
Copy link

Found it, i think @joetannenbaum.

Seems to happen (for us) when doing a router.reload immediately in the onMount of a Svelte component on the page immediately after returning from an Inertia::location() call.

We're still using Inertia::lazy here, and this would likely be fixed by refactoring to ::defer() but it's a good one to fix for backwards compatibility. My guess is likely something to do with the new async nature of requests.

Simple repro repo below, just npm install and composer install 👍🏻

inertia-svelte-repro.zip

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

No branches or pull requests

4 participants