8
8
import React , {
9
9
useReducer ,
10
10
useEffect ,
11
- unstable_useTransition as useTransition ,
11
+ useTransition ,
12
12
useCallback ,
13
13
useMemo ,
14
14
Suspense ,
15
+ unstable_useCacheRefresh as useCacheRefresh ,
16
+ unstable_Cache as Cache ,
15
17
} from 'react' ;
16
- import { createCache , CacheProvider } from 'react/unstable-cache' ;
17
18
import { RouterProvider } from './client/RouterContext' ;
18
19
// TODO: can't really import a server component on the client.
19
20
import App from './server/App' ;
20
21
21
22
const initialUrl = window . location . pathname ;
22
- const initialState = {
23
- // TODO: use this for invalidation.
24
- cache : createCache ( ) ,
25
- url : initialUrl ,
26
- pendingUrl : initialUrl ,
27
- root : < App route = { initialUrl } /> ,
28
- } ;
29
23
30
24
function reducer ( state , action ) {
31
25
switch ( action . type ) {
@@ -48,10 +42,14 @@ function reducer(state, action) {
48
42
}
49
43
50
44
function Router ( ) {
45
+ const initialState = {
46
+ url : initialUrl ,
47
+ pendingUrl : initialUrl ,
48
+ root : < App route = { initialUrl } /> ,
49
+ } ;
51
50
const [ state , dispatch ] = useReducer ( reducer , initialState ) ;
52
- const [ startTransition , isPending ] = useTransition ( {
53
- timeoutMs : 1500 ,
54
- } ) ;
51
+ const [ isPending , startTransition ] = useTransition ( ) ;
52
+ const refresh = useCacheRefresh ( ) ;
55
53
56
54
useEffect ( ( ) => {
57
55
document . body . style . cursor = isPending ? 'wait' : '' ;
@@ -62,6 +60,7 @@ function Router() {
62
60
startTransition ( ( ) => {
63
61
// TODO: Here, There, and Everywhere.
64
62
// TODO: Instant Transitions, somehow.
63
+ refresh ( ) ;
65
64
dispatch ( {
66
65
type : 'completeNavigation' ,
67
66
root : < App route = { url } /> ,
@@ -73,7 +72,7 @@ function Router() {
73
72
url,
74
73
} ) ;
75
74
} ,
76
- [ startTransition ]
75
+ [ startTransition , refresh ]
77
76
) ;
78
77
79
78
useEffect ( ( ) => {
@@ -95,9 +94,9 @@ function Router() {
95
94
96
95
return (
97
96
< Suspense fallback = { < h2 > Loading...</ h2 > } >
98
- < CacheProvider value = { state . cache } >
97
+ < Cache >
99
98
< RouterProvider value = { routeContext } > { state . root } </ RouterProvider >
100
- </ CacheProvider >
99
+ </ Cache >
101
100
</ Suspense >
102
101
) ;
103
102
}
0 commit comments