How to redirect/reload using Router
Unanswered
Eastern Phoebe posted this in #help-forum

Eastern PhoebeOP
hey guys I am using NextJS 12.3.1 and I am using the router as:
then I need to perform a redirect/refresh to the website HomePage and I am doing it as follows:
is this correct? It is working fine but I want to be sure I am doing it correctly
const router = useRouter();
then I need to perform a redirect/refresh to the website HomePage and I am doing it as follows:
router.push('/').then(() => {
router.reload();
});
is this correct? It is working fine but I want to be sure I am doing it correctly
16 Replies

router.refresh()
?I don't understand what you want to do. You can use what I mentioned above to fetch the information again while preserving the client state.

Eastern PhoebeOP
@Virtue I want to redirect to the home page aka
/
and then refresh the page content. For some reason the content does not get refreshed which is I added the reload
, that's what I am trying to do
Does the content you want to renew come from the API?

i was also going to ask why you haven't updated to v13? (still can keep pages dir)

@Virtue Does the content you want to renew come from the API?

Eastern PhoebeOP
if you mean I have to make a call to fetch the content, then yes

@risky i was also going to ask why you haven't updated to v13? (still can keep pages dir)

Eastern PhoebeOP
We've an upcoming release next month and do not have time to made the upgrade right now, we will, after this gets released to PRD and everyone is "happy"

@Eastern Phoebe if you mean I have to make a call to fetch the content, then yes

For this, you should use router.refresh() and a new request will be sent to the server side.

@Virtue For this, you should use router.refresh() and a new request will be sent to the server side.

Eastern PhoebeOP
do you mean ...
router.push('/').then(() => {
router.refresh();
});

Eastern PhoebeOP
what I was asking at first was about the syntax being right or wrong or if there is a better way to write the same thing
like chaining calls or something like that

I don't know what kind of structure you work in, so I can't give you information about the best practice.
You have to find it by trying and testing
No one knows what your project needs but you

Eastern PhoebeOP
@Virtue FYI there is no refresh method in NextJS 12.3.1 router