Next.js Discord

Discord Forum

Refresh Website Content After Updating Files in Backend

Answered
Prairie yellowjacket posted this in #help-forum
Open in Discord
Prairie yellowjacketOP
I updated some part of content inside the landing page file in backend. But on the landing page of live website it still shows old content. How do I refresh and show updated content? Do I have to run npm run build everytime after updating some content in backend?
Answered by joulev
technically rebuilding the entire site as you suggested above does work. but a ssh command to revalidate a nextjs page after updating content doesn't exist, because after all this is next.js not next.sh :)

you can do like this
1. make a route handler, say, /api/revalidate/home
2. protect it with a search param only you know (so only requests like /api/revalidate/home?secret=hgiueahjf29u399a are taken, if the secret is missing or mismatches then you simply reject the request)
3. inside the route handler, call revalidatePath("/") (or use revalidateTag if you tagged the fetch inside / - check the revalidateTag documentation for more details)
4. whenever you update some content, you curl https://yourapp.com/api/revalidate/home?secret=hgiueahjf29u399a or something similar <- this can be done in a shell script or with a webhook or basically anything
View full answer

9 Replies

@Prairie yellowjacket in SSH?
check the documentation first
@joulev check the documentation first
Prairie yellowjacketOP
Thanks! It will help.
@joulev use [`revalidatePath`](<https://nextjs.org/docs/app/api-reference/functions/revalidatePath>)
Prairie yellowjacketOP
Is there anything wrong or downside if I just run npm run build and pm2 start everytime after updating content?
@Prairie yellowjacket Is there anything wrong or downside if I just run `npm run build` and `pm2 start` everytime after updating content?
well if it works, then it's good to go. though that would mean you have to rebuilt the whole site right, which is obviously far slower than if you only revalidate one page
@joulev well if it works, then it's good to go. though that would mean you have to rebuilt the whole site right, which is obviously far slower than if you only revalidate one page
Prairie yellowjacketOP
So there is no ssh command prompt to refresh Nextjs app after updating some content?
@Prairie yellowjacket So there is no ssh command prompt to refresh Nextjs app after updating some content?
technically rebuilding the entire site as you suggested above does work. but a ssh command to revalidate a nextjs page after updating content doesn't exist, because after all this is next.js not next.sh :)

you can do like this
1. make a route handler, say, /api/revalidate/home
2. protect it with a search param only you know (so only requests like /api/revalidate/home?secret=hgiueahjf29u399a are taken, if the secret is missing or mismatches then you simply reject the request)
3. inside the route handler, call revalidatePath("/") (or use revalidateTag if you tagged the fetch inside / - check the revalidateTag documentation for more details)
4. whenever you update some content, you curl https://yourapp.com/api/revalidate/home?secret=hgiueahjf29u399a or something similar <- this can be done in a shell script or with a webhook or basically anything
Answer
Prairie yellowjacketOP
Thanks