Next Router Issues

So I have an issues using next router. In my project I have something similar to /blog/[id].tsx This [id].tsx does a getServerSideProps where it fetches the correspoding information from a database and passes that to the page. Now when I do router.push() from anywhere in the application everything works fine. How ever I have some blog posts that reference others. Now when I do router.push("/blog/1234") while being on /blog/1 for example only the url changes. So the url now is /blog/1234, but the content that is displayed is still the one of /blog/1 A work around is just using window.location.replace, but I'm not really happy with that solution und would prefer using next router. Thanks for your help!
3 Replies
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
NotLuksus
NotLuksus2y ago
That still comes with the same issues. https://github.com/vercel/next.js/issues/26270 I found that so I probably just stick with what I got so far
GitHub
router.push doesn't refresh page · Issue #26270 · vercel/next.js
What version of Next.js are you using? 11.0.0 What version of Node.js are you using? 16.3 What browser are you using? chrome What operating system are you using? Windows 8 How are you deploying you...
JulieCezar
JulieCezar2y ago
You could maybe use Router.replace() if you don't mind deleting history Or maybe try something like this ``
const Router = useRouter();
const trpcContext = trpc.useContext()

Router.events.on("routeChangeComplete", ()=> {
trpcContext.invalidateQueries(["example.hello"])
})
const Router = useRouter();
const trpcContext = trpc.useContext()

Router.events.on("routeChangeComplete", ()=> {
trpcContext.invalidateQueries(["example.hello"])
})
Or maybe even try "routeChangeStart" because if you invalidate right at the beginning it might have a weird interaction, but if you invalidate after you leave it should be ok Although it will invalidate whenever you are going to any page from /blog/[id] but it should solve your case