-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathindex.js
More file actions
146 lines (131 loc) · 3.78 KB
/
Copy pathindex.js
File metadata and controls
146 lines (131 loc) · 3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import {
Suspense,
createElement as h,
startTransition,
use,
useDeferredValue,
useEffect,
useRef,
useState,
useTransition,
} from 'react'
import { createRoot } from 'react-dom/client'
import * as RSC from 'react-server-dom-esm/client'
import { contentCache, useContentCache, generateKey } from './content-cache.js'
import { ErrorBoundary } from './error-boundary.js'
import { shipFallbackSrc } from './img-utils.js'
import { RouterContext, getGlobalLocation, useLinkHandler } from './router.js'
function fetchContent(location) {
return fetch(`/rsc${location}`)
}
function createFromFetch(fetchPromise) {
return RSC.createFromFetch(fetchPromise, {
moduleBaseURL: `${window.location.origin}/ui`,
callServer,
})
}
async function callServer(id, args) {
const fetchPromise = fetch(`/action${getGlobalLocation()}`, {
method: 'POST',
headers: { 'rsc-action': id },
body: await RSC.encodeReply(args),
})
const actionResponsePromise = createFromFetch(fetchPromise)
const { returnValue } = await actionResponsePromise
return returnValue
}
const initialLocation = getGlobalLocation()
const initialContentPromise = createFromFetch(fetchContent(initialLocation))
let initialContentKey = window.history.state?.key
if (!initialContentKey) {
initialContentKey = generateKey()
window.history.replaceState({ key: initialContentKey }, '')
}
contentCache.set(initialContentKey, initialContentPromise)
function Root() {
const latestNav = useRef(null)
const contentCache = useContentCache()
const [nextLocation, setNextLocation] = useState(getGlobalLocation)
const [contentKey, setContentKey] = useState(initialContentKey)
const [, startTransition] = useTransition()
const location = useDeferredValue(nextLocation)
const contentPromise = contentCache.get(contentKey)
useEffect(() => {
function handlePopState() {
const nextLocation = getGlobalLocation()
setNextLocation(nextLocation)
const historyKey = window.history.state?.key ?? generateKey()
if (!contentCache.has(historyKey)) {
const fetchPromise = fetchContent(nextLocation)
const nextContentPromise = createFromFetch(fetchPromise)
contentCache.set(historyKey, nextContentPromise)
startTransition(() => setContentKey(historyKey))
} else {
setContentKey(historyKey)
}
}
window.addEventListener('popstate', handlePopState)
return () => window.removeEventListener('popstate', handlePopState)
}, [contentCache])
function navigate(nextLocation, { replace = false } = {}) {
setNextLocation(nextLocation)
const thisNav = Symbol(`Nav for ${nextLocation}`)
latestNav.current = thisNav
const newContentKey = generateKey()
const nextContentPromise = createFromFetch(
fetchContent(nextLocation).then((response) => {
if (thisNav !== latestNav.current) return
if (replace) {
window.history.replaceState({ key: newContentKey }, '', nextLocation)
} else {
window.history.pushState({ key: newContentKey }, '', nextLocation)
}
return response
}),
)
contentCache.set(newContentKey, nextContentPromise)
startTransition(() => setContentKey(newContentKey))
}
useLinkHandler(navigate)
return h(
RouterContext,
{
value: {
navigate,
location,
nextLocation,
},
},
// 🐨 the contentPromise is now an object with a root property, update this
// to render the root: 💰 use(contentPromise).root
use(contentPromise),
)
}
startTransition(() => {
createRoot(document.getElementById('root')).render(
h(
'div',
{ className: 'app-wrapper' },
h(
ErrorBoundary,
{
fallback: h(
'div',
{ className: 'app-error' },
h('p', null, 'Something went wrong!'),
),
},
h(
Suspense,
{
fallback: h('img', {
style: { maxWidth: 400 },
src: shipFallbackSrc,
}),
},
h(Root),
),
),
),
)
})