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

clicking hash link recorded in history twice #3636

Closed
kyontan opened this issue Jan 30, 2022 · 8 comments
Closed

clicking hash link recorded in history twice #3636

kyontan opened this issue Jan 30, 2022 · 8 comments
Labels
bug Something isn't working p2-nice-to-have SvelteKit cannot be used by a small number of people, quality of life improvements, etc. router
Milestone

Comments

@kyontan
Copy link

kyontan commented Jan 30, 2022

Describe the bug

When opening #anchor-a then click a link to #anchor-b, history newly records entries #anchor-a and #anchor-b though #anchor-a is previous location.

Therefore, hisotry looks like:

#anchor-a (initial view)
#anchor-b
#anchor-b (when clicked #anchor-b)
#anchor-c
#anchor-c (when clicked #anchor-c)
#anchor-d
#anchor-d (when clicked #anchor-d)
...

Disabling router by export const router = false; mitigate this issue, so that I think router has some issue around this.

Reproduction

https://stackblitz.com/edit/sveltekit-ivmzya?devtoolsheight=33&file=src/routes/index.svelte

Easily understand by following steps:

  1. Open in New Window in Stackblitz
  2. Clicking anchor links some times
  3. Check a browser history, looks like:

スクリーンショット 2022-01-31 3 59 07

(Also I notified null is oddly appealing on first clicking #anchor-b)

Logs

No response

System Info

# npx envinfo --system --binaries --browsers --npmPackages "{svelte,@sveltejs/*,vite}"
  System:
    OS: macOS 11.4
    CPU: (8) x64 Apple M1
    Memory: 24.88 MB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 16.13.2 - ~/.nodebrew/current/bin/node
    Yarn: 1.22.17 - ~/.nodebrew/current/bin/yarn
    npm: 8.1.2 - ~/.nodebrew/current/bin/npm
  Browsers:
    Firefox: 95.0
    Safari: 14.1.1
  npmPackages:
    @sveltejs/adapter-auto: next => 1.0.0-next.17
    @sveltejs/kit: next => 1.0.0-next.251
    svelte: ^3.46.3 => 3.46.3


### Severity

blocking an upgrade

### Additional Information

_No response_
@kyontan
Copy link
Author

kyontan commented Jan 30, 2022

I think replacing <a href="#anchor">...</a> with <a on:click={() => goto("#anchor")}>...</a> could workaround this.

@bluwy bluwy added bug Something isn't working p2-nice-to-have SvelteKit cannot be used by a small number of people, quality of life improvements, etc. router labels Jan 31, 2022
@Glench
Copy link

Glench commented Jan 31, 2022

Maybe related to #3621?

@bluwy
Copy link
Member

bluwy commented Feb 1, 2022

Yeah it does look like that this issue causes #3621. cc @vicky1999 in case you're already working on a fix.

@vicky1999
Copy link
Contributor

vicky1999 commented Feb 2, 2022

@bluwy I think the above fix will solve the issue. but, we need to check this once.

aolose referenced this issue Feb 2, 2022
* fix: skip navigation if only url hash change

* docs: update pushState comment

* remove _tests.js

Co-authored-by: Rich Harris <hello@rich-harris.dev>
@aolose
Copy link
Contributor

aolose commented Feb 2, 2022

I think some code change the history ,so I global searched history. then found it at:
kit/src/runtime/client/router.js line:191

// Check if new url only differs by hash
if (url.href.split('#')[0] === location.href.split('#')[0]) {
	// Call `pushState` to add url to history so going back works.
	// Also make a delay, otherwise the browser default behaviour would not kick in
	setTimeout(() => history.pushState({}, '', url.href));
	const info = this.parse(url);
	if (info) {
		return this.renderer.update(info, [], false);
	}
	return;
}

I don't know what does these code do,
the history.pushState here will record history one more time.
I try to delete it and run the Reproduction ,then hash links works fine~

@benmccann
Copy link
Member

I've sent a PR for a failing test that can be uncommented when this issue is fixed: #3880

benmccann added a commit that referenced this issue Feb 13, 2022
benmccann added a commit that referenced this issue Feb 13, 2022
@benmccann
Copy link
Member

This was fixed recently. The test has now been uncommented and passes:

await page.goBack();

@kyontan
Copy link
Author

kyontan commented Feb 17, 2022

Great thanks to @benmccann !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working p2-nice-to-have SvelteKit cannot be used by a small number of people, quality of life improvements, etc. router
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants