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