Skip to content

How to force a page refresh in Next.js

FULL-STACK WEB DEVELOPMENT BOOTCAMP

2024 COHORT SIGNUPS END TOMORROW

In a component, you can use the useRouter hook:

import { useRouter } from 'next/router'

//...

const router = useRouter()

router.reload(window.location.pathname)

Sometimes you canโ€™t, for example when youโ€™re not in a React component, maybe in a utility function.

In that case, you can do this:

import Router from 'next/router'

Router.reload(window.location.pathname)
  • Learn modern web development in my BOOTCAMP (SIGNUP END TOMORROW FEB 20, 2024)
  • THE VALLEY OF CODE (+ PRO), your web development manual
  • I wrote 15+ coding BOOKS, all available in THE VALLEY OF CODE PRO
  • Indie solopreneur internet business masterclass SOLO LAB (summer 2024)