Why window.history.pushState refetches the data and router.push not?
Answered
Nebelung posted this in #help-forum
NebelungOP
Hello! After mutating data in server action I call
I suspect the
I tried adding adding setTimout before calling
the redirectUrl in both cases looks like this:
Working example
I wish below would work:
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();4 Replies
@Nebelung 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);
I think its better to use
or you have other client side logic?
redirect() in the server action so you don't need to use router.refresh()or you have other client side logic?
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!