Skip to content

fix(v2): ignore hash changes in useChangeRoute hook - #5023

Merged
slorber merged 2 commits into
masterfrom
lex111/iss5020
Jun 22, 2021
Merged

slorber merged 2 commits into
masterfrom
lex111/iss5020

Conversation

@lex111

@lex111 lex111 commented Jun 20, 2021

Copy link
Copy Markdown
Contributor

Motivation

Fixes #5020

Currently the useChangedRoute hook is mistakenly triggered by hash changes on cross-page, which in particular resets scroll position when clicking on anchor link leading to another internal page.

Have you read the Contributing Guidelines on pull requests?

Yes

Test Plan

See "Reproducible Demo" section from #5020.

Related PRs

(If this PR adds or changes functionality, please take some time to update the docs at https://github.com/facebook/docusaurus, and link to your PR here.)

@lex111 lex111 added the pr: bug fix This PR fixes a bug in a past release. label Jun 20, 2021
@lex111
lex111 requested a review from slorber as a code owner June 20, 2021 19:13
@facebook-github-bot facebook-github-bot added the CLA Signed Signed Facebook CLA label Jun 20, 2021
@lex111 lex111 mentioned this pull request Jun 20, 2021
5 tasks done
@netlify

netlify Bot commented Jun 20, 2021 •

Copy link
Copy Markdown

@github-actions

github-actions Bot commented Jun 20, 2021 •

Copy link
Copy Markdown

⚡️ Lighthouse report for the changes in this PR:

Category Score
🟠 Performance 64
🟢 Accessibility 98
🟢 Best practices 100
🟢 SEO 100
🟢 PWA 95

Lighthouse ran on https://deploy-preview-5023--docusaurus-2.netlify.app/

@github-actions

github-actions Bot commented Jun 20, 2021 •

Copy link
Copy Markdown

Size Change: +133 B (0%)

Total Size: 569 kB

Filename Size Change
website/build/assets/js/main.********.js 385 kB +133 B (0%)
ℹ️ View Unchanged
Filename Size
website/build/assets/css/styles.********.css 87.8 kB
website/build/blog/2017/12/14/introducing-docusaurus/index.html 65.8 kB
website/build/docs/introduction/index.html 235 B
website/build/index.html 30.5 kB

compressed-size-action

@slorber slorber left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I'd rather make this fix outside of the onRouteChange hook

if (pathname !== latestPathnameRef.current) {
if (!hash && pathname !== latestPathnameRef.current) {
latestPathnameRef.current = pathname;
onRouteChange();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

What about using onRouteChange({location}) instead, and preventing the call to programmaticFocus if there's a hash?

onRouteChange should rather fire even if an anchor link is clicked, for example this should be triggered, otherwise the hook does not really do what its name implies.

We also may want this callback to keep being called for anchor links:

  useChangeRoute(() => {
    if (!hideOnScroll) {
      return;
    }

    setIsNavbarVisible(true);
  });

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I thought about this at first, but for the hideable navbar to work properly we also need to ignore hash changes in the according hook, otherwise the navbar will automatically show up when go to on anchor link on cross-page. So in order not to duplicate code, we can slightly improve useChangedRoute hook directly, because at the moment it also triggers when hash has changed, but it shouldn't. After all, this hook is only supposed to keep track of changes in current pathname, right?

@slorber slorber Jun 22, 2021 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Yes but it won't trigger the callback when current pathname change, if a hash is also present, which is confusing.

Agree to merge that for now, but not 100% sure it'll be the best long-term decision. It might become a problem if we decide to use this hook for another use-case. For example it wouldn't work reliably anymore for handling analytics or things like that, because it wouldn't report page changes if user keeps clicking on anchor links.

If we use more this hook in the future we'll have to handle the hash case on the 2 call-sites we currently have

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Actually I'll do that now, because I'm not comfortable at all having the latestPathnameRef not being updated when there is a hash. This might lead to subtle bugs in this hook after the user has navigated to an anchor link

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Alright, although there is more code/complexity, agree, it looks more flexible and reliable 👍

@slorber
slorber merged commit 8bda3b2 into master Jun 22, 2021
@tsirlucas

Copy link
Copy Markdown
Contributor

Hey, any idea on when this is going to be released? We really want to use the new 2.0.0-beta.1 version but this issue is breaking links to headers and that is kinda of a blocker right now

@slorber

slorber commented Jun 24, 2021

Copy link
Copy Markdown
Collaborator

@tsirlucas you can always use the canary release: https://docusaurus.io/community/canary

Going to release very soon!

@indre-idenfy

Copy link
Copy Markdown

@slorber Thank you, where will be possible to see the release for this fix?

@tsirlucas

Copy link
Copy Markdown
Contributor

@indre-idenfy this is already deployed in the beta.2 but unfortunately theres a new bug introduced so im waiting for beta.3

@slorber
slorber deleted the lex111/iss5020 branch August 17, 2021 17:49
mrizwanashiq pushed a commit to mrizwanashiq/docusaurus that referenced this pull request Jun 25, 2026
* fix(v2): ignore hash changes in useChangeRoute hook

* refactor and introduce useLocationChange hook

Co-authored-by: slorber <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed Signed Facebook CLA pr: bug fix This PR fixes a bug in a past release.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Anchor links across pages are failing

5 participants