Skip to content

Conversation

@michaeljonathanblack
Copy link

Fixes #2825


// record hash change
const hash = window.location.hash.substring(1)
this.set(route, pathname, query, as, { ...routeInfo, hash })

Choose a reason for hiding this comment

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

This call here is the fix. Previously, when onlyAHashChange was called, this.asPath was undefined because it was never set inside this conditional.

routeInfo = this.components[route]
} else {
const { shallow = false } = options

Choose a reason for hiding this comment

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

Kept this block down here to avoid accidentally calling getInitialProps for a hash change (although it should be noted that we won't call this.scrollToHash(as) when we're navigating between pages with an attached hash.

There's an open issue for that, though.

@timneutkens
Copy link
Member

Could you add a test for this?

@michaeljonathanblack
Copy link
Author

Let me see what I can do!

@michaeljonathanblack
Copy link
Author

@timneutkens Added unit tests.

Also added a change that should close out #5161, with accompanying unit test change.

Copy link
Member

@timneutkens timneutkens left a comment

Choose a reason for hiding this comment

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

Sorry for being a pain, but this will help in the long run, could you add integration tests for this change too, include the fix for the other issue

@michaeljonathanblack
Copy link
Author

I was wondering if you'd want integration tests! I'll have to do some more digging.

Initially I ran into integration test issues as it seemed like the entire suite wanted to run locally, despite my protests.

.elementByCss('p').text()

expect(counter).toBe('COUNT: 1')
expect(counter).toBe('COUNT: 0')

Choose a reason for hiding this comment

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

This test wasn't actually testing what it described; if getInitialProps was not called, count should be zero.

As far as I can tell, the Next.js router doesn't handle regular anchor-based hash changes outside of the Link component (e.g. clicking an anchor hash doesn't update the router state, so clicking #page-url in that case WOULD trigger getInitialProps)

@michaeljonathanblack
Copy link
Author

Update: I don't think this resolves #5161 if that bug is related to _app (since there are actually two scrollToHash functions in this project)

@timneutkens
Copy link
Member

So I was thinking, making the router aware of hashes wouldn't be the solution either, since someone might do <a href="#test"> and the router wouldn't know what happened.

@michaeljonathanblack
Copy link
Author

michaeljonathanblack commented Dec 10, 2018

So I was thinking, making the router aware of hashes wouldn't be the solution either, since someone might do <a href="#test"> and the router wouldn't know what happened.

Correct, the router currently doesn't understand non-Link/Router hash navigations (that seems to be by design?)

We could handle that feature in another issue/PR, if that's desirable.

@michaeljonathanblack
Copy link
Author

@timneutkens Is there outstanding work for this PR?

I'd like to get this merged in so we can start targeting canary and using hash-routing without triggering getInitialProps.

@lfades lfades closed this in #7255 May 5, 2019
@lock lock bot locked as resolved and limited conversation to collaborators May 5, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Changing hash in URL causes getInitialProps to be re-fired

2 participants