Skip to content

Ability to push changes to url without re-rendering #18072

Answered by matthewwolfe
matthewwolfe asked this question in Help
Discussion options

You must be logged in to vote

Leaving this comment here in case others have the same question in the future:

I ended up going with

window.history.replaceState({ ...window.history.state, as: newUrl, url: newUrl }, '', newUrl);

for when I wanted to update the url without doing any re-rendering. I also check against Router.pathname and only use history.replaceState when remaining on the same page. Otherwise I use the nextjs Router.

Specifying window.history.state is mandatory because it maintains some nextjs state internals. If you pass {} instead of window.history.state, moving forward/back will potentially be broken. This was the hardest issue for me to fix.

Replies: 8 comments 34 replies

Comment options

You must be logged in to vote
3 replies
@matthewwolfe
Comment options

@jamesmosier
Comment options

@matthewwolfe
Comment options

Comment options

You must be logged in to vote
26 replies
@vissaf
Comment options

@bombillazo
Comment options

@EarthlingDavey
Comment options

@kirandash
Comment options

@JimCarnicelli
Comment options

Answer selected by matthewwolfe
Comment options

You must be logged in to vote
2 replies
@akash-mahmud
Comment options

@bamdadsabbagh
Comment options

Comment options

You must be logged in to vote
1 reply
@HelaGone
Comment options

Comment options

You must be logged in to vote
1 reply
@EarthlingDavey
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@antofa
Comment options

Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
None yet