0

What is the best way to force a hard reload in Nextjs?

My current use case:

  • The web application asks the user for the geolocation
  • If the user denies the geolocation I want to show him an error message, that the geolocation is needed

To improve the user experience I would like to give the user the option to do a page reload, so that the user will be asked again for his geolocation. It seems that a hard reload is required for this.

If there are better ways to ask again for the user geolocation, please don't hesitate to tell me about it.

If a hard reload is the way to go, those are the steps I tried and failed:

with useRouter

const router = useRouter()
router.reload()

This does not perform a hard reload and thus doesn't ask the user again for the geolocation.

with window.location

window.location.reload();

Same expected behaviour than with useRouter.

Add cache-control

<Head>
  <meta httpEquiv="Cache-Control" content="no-store, max-age=0" />
</Head>

This does not perform a hard reload and thus doesn't ask the user again for the geolocation.

2Up1Down
  • 154
  • 3
  • Does this answer your question: [JavaScript hard refresh of current page](https://stackoverflow.com/questions/2099201/javascript-hard-refresh-of-current-page)? – juliomalves Nov 21 '22 at 06:15
  • I gave it a try with reload(true) and its not working. See the following statement from https://developer.mozilla.org/en-US/docs/Web/API/Location/reload "You may, though, come across instances of location.reload(true) in existing code that was written with the assumption the force-reload effect occurs in all browsers. A GitHub "location.reload(true)" search returns several hundred thousand results. ..... So a boolean parameter is not part of the current specification for location.reload() — and in fact has never been part of any specification for location.reload() ever published." – 2Up1Down Nov 22 '22 at 07:27
  • Yes, that's an outdated answer. Check the other solutions. – juliomalves Nov 22 '22 at 07:37

0 Answers0