|
| 1 | +import { |
| 2 | + createFromReadableStream, |
| 3 | + createFromFetch, |
| 4 | + setServerCallback, |
| 5 | + createTemporaryReferenceSet, |
| 6 | + encodeReply, |
| 7 | +} from '@vitejs/plugin-rsc/browser' |
| 8 | +import React from 'react' |
| 9 | +import { createRoot, hydrateRoot } from 'react-dom/client' |
| 10 | +import { rscStream } from 'rsc-html-stream/client' |
| 11 | +import type { RscPayload } from './entry.rsc' |
| 12 | +import { GlobalErrorBoundary } from './error-boundary' |
| 13 | +import { createRscRenderRequest } from './request' |
| 14 | + |
| 15 | +async function main() { |
| 16 | + let setPayload: (value: RscPayload) => void |
| 17 | + |
| 18 | + const initialPayload = await createFromReadableStream<RscPayload>(rscStream) |
| 19 | + |
| 20 | + function BrowserRoot() { |
| 21 | + const [payload, setPayload_] = React.useState(initialPayload) |
| 22 | + |
| 23 | + React.useEffect(() => { |
| 24 | + setPayload = (value) => React.startTransition(() => setPayload_(value)) |
| 25 | + }, [setPayload_]) |
| 26 | + |
| 27 | + React.useEffect(() => { |
| 28 | + return listenNavigation(() => fetchRscPayload()) |
| 29 | + }, []) |
| 30 | + |
| 31 | + return payload.root |
| 32 | + } |
| 33 | + |
| 34 | + async function fetchRscPayload() { |
| 35 | + const renderRequest = createRscRenderRequest(window.location.href) |
| 36 | + const payload = await createFromFetch<RscPayload>(fetch(renderRequest)) |
| 37 | + setPayload(payload) |
| 38 | + } |
| 39 | + |
| 40 | + setServerCallback(async (id, args) => { |
| 41 | + const temporaryReferences = createTemporaryReferenceSet() |
| 42 | + const renderRequest = createRscRenderRequest(window.location.href, { |
| 43 | + id, |
| 44 | + body: await encodeReply(args, { temporaryReferences }), |
| 45 | + }) |
| 46 | + const payload = await createFromFetch<RscPayload>(fetch(renderRequest), { |
| 47 | + temporaryReferences, |
| 48 | + }) |
| 49 | + setPayload(payload) |
| 50 | + const { ok, data } = payload.returnValue! |
| 51 | + if (!ok) throw data |
| 52 | + return data |
| 53 | + }) |
| 54 | + |
| 55 | + const browserRoot = ( |
| 56 | + <React.StrictMode> |
| 57 | + <GlobalErrorBoundary> |
| 58 | + <BrowserRoot /> |
| 59 | + </GlobalErrorBoundary> |
| 60 | + </React.StrictMode> |
| 61 | + ) |
| 62 | + if ('__NO_HYDRATE' in globalThis) { |
| 63 | + createRoot(document).render(browserRoot) |
| 64 | + } else { |
| 65 | + hydrateRoot(document, browserRoot, { |
| 66 | + formState: initialPayload.formState, |
| 67 | + }) |
| 68 | + } |
| 69 | + |
| 70 | + if (import.meta.hot) { |
| 71 | + import.meta.hot.on('rsc:update', () => { |
| 72 | + fetchRscPayload() |
| 73 | + }) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +function listenNavigation(onNavigation: () => void) { |
| 78 | + window.addEventListener('popstate', onNavigation) |
| 79 | + |
| 80 | + const oldPushState = window.history.pushState |
| 81 | + window.history.pushState = function (...args) { |
| 82 | + const result = oldPushState.apply(this, args) |
| 83 | + onNavigation() |
| 84 | + return result |
| 85 | + } |
| 86 | + |
| 87 | + const oldReplaceState = window.history.replaceState |
| 88 | + window.history.replaceState = function (...args) { |
| 89 | + const result = oldReplaceState.apply(this, args) |
| 90 | + onNavigation() |
| 91 | + return result |
| 92 | + } |
| 93 | + |
| 94 | + function onClick(event: MouseEvent) { |
| 95 | + const link = (event.target as Element).closest('a') |
| 96 | + if ( |
| 97 | + link && |
| 98 | + link instanceof HTMLAnchorElement && |
| 99 | + link.href && |
| 100 | + (!link.target || link.target === '_self') && |
| 101 | + link.origin === location.origin && |
| 102 | + !link.hasAttribute('download') && |
| 103 | + event.button === 0 && |
| 104 | + !event.metaKey && |
| 105 | + !event.ctrlKey && |
| 106 | + !event.altKey && |
| 107 | + !event.shiftKey && |
| 108 | + !event.defaultPrevented |
| 109 | + ) { |
| 110 | + event.preventDefault() |
| 111 | + history.pushState(null, '', link.href) |
| 112 | + } |
| 113 | + } |
| 114 | + document.addEventListener('click', onClick) |
| 115 | + |
| 116 | + return () => { |
| 117 | + document.removeEventListener('click', onClick) |
| 118 | + window.removeEventListener('popstate', onNavigation) |
| 119 | + window.history.pushState = oldPushState |
| 120 | + window.history.replaceState = oldReplaceState |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +main() |
0 commit comments