Skip to content

Commit

Permalink
feat(react): add refetchOnWindowFocus option to SessionProvider (#3730)
Browse files Browse the repository at this point in the history
  • Loading branch information
timakin committed Jan 25, 2022
1 parent 1bf56a2 commit af157da
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/react/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -379,16 +379,18 @@ export function SessionProvider(props: SessionProviderProps) {
}, [])

React.useEffect(() => {
const { refetchOnWindowFocus = true } = props
// Listen for when the page is visible, if the user switches tabs
// and makes our tab visible again, re-fetch the session.
// and makes our tab visible again, re-fetch the session, but only if
// this feature is not disabled.
const visibilityHandler = () => {
if (document.visibilityState === "visible")
if (refetchOnWindowFocus && document.visibilityState === "visible")
__NEXTAUTH._getSession({ event: "visibilitychange" })
}
document.addEventListener("visibilitychange", visibilityHandler, false)
return () =>
document.removeEventListener("visibilitychange", visibilityHandler, false)
}, [])
}, [props.refetchOnWindowFocus])

React.useEffect(() => {
const { refetchInterval } = props
Expand Down
5 changes: 5 additions & 0 deletions src/react/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,9 @@ export interface SessionProviderProps {
* If set to `0` (default), the session is not polled.
*/
refetchInterval?: number
/**
* `SessionProvider` automatically refetches the session when the user switches between windows.
* This option activates this behaviour if set to `true` (default).
*/
refetchOnWindowFocus?: boolean
}

0 comments on commit af157da

Please sign in to comment.