Next.js Discord

Discord Forum

Why window.history.pushState refetches the data and router.push not?

Answered
Nebelung posted this in #help-forum
Open in Discord
NebelungOP
Hello! After mutating data in server action I call router.refresh() and then window.history.pushState(null, '', redirectUrl). This works but I also expected the router.push(redirectUrl) to work as well.

I suspect the router.refresh() gets immediately overwritten with router.push so it doesn't have time to refresh the route...

I tried adding adding setTimout before calling router.push(redirectUrl) and then it somehow works-> without setTimout it doesn't...

the redirectUrl in both cases looks like this: http://localhost:3000/account?status=Success!&status_description=...

Working example
const redirectUrl = await requestFunc(
    paymentMethodId,
    subscriptionId,
    currentPath
  );

  if (router) {
    router.refresh();
    window.history.pushState(null, '', redirectUrl);

I wish below would work:
const redirectUrl = await requestFunc(
    paymentMethodId,
    subscriptionId,
    currentPath
  );

  if (router) {
    router.refresh();
    return router.push(redirectUrl);
Answered by Ray
to answer your question, I think you need to call push before refresh
router.push(redirectUrl);
router.refresh();
View full answer

4 Replies

to answer your question, I think you need to call push before refresh
router.push(redirectUrl);
router.refresh();
Answer
NebelungOP
Both solutions work nicely! I actually thought i tried it before...
both solutions: redirect(); router.push then refresh refreshed the fetch and kept the modal open. thanks!