1
0
mirror of synced 2025-12-30 12:02:01 -05:00
Files
docs/components/ClientSideRefresh.tsx
Peter Bengtsson 37fa4e871e auto-refresh content (#30689)
Co-authored-by: Sarah Schneider <sarahs@users.noreply.github.com>
2022-09-14 16:58:27 +00:00

34 lines
1.2 KiB
TypeScript

import { useRouter } from 'next/router'
import useSWR from 'swr'
// This component is never mounted when you're in production mode.
// Only when running in `NODE_ENV==='development'`.
// It will reload the content every time the current page is focussed
// (from being not focussed).
export default function ClientSideRefresh() {
const router = useRouter()
useSWR(
router.asPath,
() => {
router.replace(router.asPath)
},
{
// Implied here is that `revalidateOnFocus: true` which the default
// and it means that the `useSWR` hook will make a listener on the
// the Page Visibility API.
// https://developer.mozilla.org/en-US/docs/Web/API/Page_Visibility_API
// It effectly means that the callback of this hook will run every
// time the browser window is put back to being visible.
// The `revalidateOnMount` is crucial because it means that we don't
// bother executing the hook callback when it was first mounted
// because, naturally, the first time you mount it, it will not
// need to refresh because it's as fresh as it gets already.
revalidateOnMount: false,
}
)
return null
}