sveltejs / kit

web development, streamlined
https://svelte.dev/docs/kit
MIT License
18.68k stars 1.93k forks source link

pushState Not Adding to History in Chrome Browser #12538

Closed rtlqwm324 closed 3 days ago

rtlqwm324 commented 2 months ago

Describe the bug

I’m encountering an issue where the pushState function doesn’t add a new history entry in Chrome, although it works as expected in Firefox.

Here’s the relevant code snippet:

<script>
  import { pushState } from '$app/navigation';
  import { onMount, tick } from 'svelte';

  onMount(async () => {
    await tick();
    pushState("");
  });
</script>

https://github.com/user-attachments/assets/6e2adccc-a409-4e41-b248-7a91c0284a28

Reproduction

To reproduce the issue, please follow these steps:

Create a New Project: Use create-svelte@6.3.5 to generate a new SvelteKit project with the following command: npm create svelte@6.3.5 my-sveltekit-project

Configure the Project: During the setup, select the following options:

Add the Code Snippet: Enter the code snippet provided in 'Describe the bug'

Logs

No response

System Info

System:
  OS: macOS 14.5
  CPU: (8) arm64 Apple M1
  Memory: 36.30 MB / 8.00 GB
  Shell: 5.9 - /bin/zsh
Binaries:
  Node: 20.10.0 - /opt/homebrew/bin/node
  Yarn: 1.22.22 - /opt/homebrew/bin/yarn
  npm: 10.2.5 - /opt/homebrew/bin/npm
  pnpm: 8.5.0 - /opt/homebrew/bin/pnpm
Browsers:
  Chrome: 127.0.6533.89
  Safari: 17.5
  Firefox: 128.0.3
npmPackages:
  @sveltejs/adapter-auto: ^3.0.0 => 3.2.2
  @sveltejs/kit: ^2.0.0 => 2.5.19
  @sveltejs/vite-plugin-svelte: ^3.0.0 => 3.1.1
  svelte: ^4.2.7 => 4.2.18
  vite: ^5.0.3 => 5.3.5

Severity

annoyance

Additional Information

No response

Conduitry commented 2 months ago

This sounds like a duplicate of #11466. Is it only happening for you in onMount?

rtlqwm324 commented 2 months ago

This sounds like a duplicate of #11466. Is it only happening for you in onMount?

Not only onMount, but also afterNavigate produce the same result. However, if tick is not called first in onMount, an error occurs: in Chrome, it’s Uncaught (in promise) TypeError: Cannot read properties of undefined (reading '$set'), and in Firefox, it’s Uncaught (in promise) TypeError: root is undefined. The difference from issue #11466 is that when the code runs without errors, a history entry is not added in Chrome.

eltigerchino commented 3 days ago

Closing as I was unable to reproduce the issue. Note that pushState requires 2 arguments, so I added an empty object so that it was pushState("", {});. Will re-open if another minimal reproduction as a repository is provided.