1- import { createHeadManager , router } from '@inertiajs/core'
1+ import { createHeadManager , PageHandler , router } from '@inertiajs/core'
22import { createElement , useEffect , useMemo , useState } from 'react'
33import HeadContext from './HeadContext'
44import PageContext from './PageContext'
55
6- let isRouterInitialized = false
76let currentIsInitialPage = true
7+ let routerIsInitialized = false
8+ let swapComponent : PageHandler = async ( ) => {
9+ // Dummy function so we can init the router outside of the useEffect hook. This is
10+ // needed so `router.reload()` works right away (on mount) in any of the user's
11+ // components. We swap in the real function in the useEffect hook below.
12+ currentIsInitialPage = false
13+ }
814
915export default function App ( {
1016 children,
@@ -28,20 +34,21 @@ export default function App({
2834 )
2935 } , [ ] )
3036
31- if ( ! isRouterInitialized ) {
37+ if ( ! routerIsInitialized ) {
3238 router . init ( {
3339 initialPage,
3440 resolveComponent,
35- swapComponent : async ( ) => {
36- currentIsInitialPage = false
37- } ,
41+ swapComponent : async ( args ) => swapComponent ( args ) ,
3842 } )
39- isRouterInitialized = true
43+
44+ routerIsInitialized = true
4045 }
4146
4247 useEffect ( ( ) => {
43- router . setSwapComponent ( async ( { component, page, preserveState } ) => {
48+ swapComponent = async ( { component, page, preserveState } ) => {
4449 if ( currentIsInitialPage ) {
50+ // We block setting the current page on the initial page to
51+ // prevent the initial page from being re-rendered again.
4552 currentIsInitialPage = false
4653 return
4754 }
@@ -51,7 +58,7 @@ export default function App({
5158 page,
5259 key : preserveState ? current . key : Date . now ( ) ,
5360 } ) )
54- } )
61+ }
5562
5663 router . on ( 'navigate' , ( ) => headManager . forceUpdate ( ) )
5764 } , [ ] )
0 commit comments