|
| 1 | +import * as ReactClient from '@vitejs/plugin-rsc/browser' |
| 2 | +import React from 'react' |
| 3 | +import * as ReactDOMClient from 'react-dom/client' |
| 4 | +import type { RscPayload } from './entry.rsc' |
| 5 | + |
| 6 | +async function main() { |
| 7 | + // stash `setPayload` function to trigger re-rendering |
| 8 | + // from outside of `BrowserRoot` component (e.g. server function call, navigation, hmr) |
| 9 | + let setPayload: (v: RscPayload) => void |
| 10 | + |
| 11 | + const initialPayload = await ReactClient.createFromFetch<RscPayload>( |
| 12 | + fetch(window.location.href), |
| 13 | + ) |
| 14 | + |
| 15 | + // browser root component to (re-)render RSC payload as state |
| 16 | + function BrowserRoot() { |
| 17 | + const [payload, setPayload_] = React.useState(initialPayload) |
| 18 | + |
| 19 | + React.useEffect(() => { |
| 20 | + setPayload = (v) => React.startTransition(() => setPayload_(v)) |
| 21 | + }, [setPayload_]) |
| 22 | + |
| 23 | + // re-fetch/render on client side navigation |
| 24 | + React.useEffect(() => { |
| 25 | + return listenNavigation(() => fetchRscPayload()) |
| 26 | + }, []) |
| 27 | + |
| 28 | + return payload.root |
| 29 | + } |
| 30 | + |
| 31 | + // re-fetch RSC and trigger re-rendering |
| 32 | + async function fetchRscPayload() { |
| 33 | + const payload = await ReactClient.createFromFetch<RscPayload>( |
| 34 | + fetch(window.location.href), |
| 35 | + ) |
| 36 | + setPayload(payload) |
| 37 | + } |
| 38 | + |
| 39 | + // register a handler which will be internally called by React |
| 40 | + // on server function request after hydration. |
| 41 | + ReactClient.setServerCallback(async (id, args) => { |
| 42 | + const url = new URL(window.location.href) |
| 43 | + const temporaryReferences = ReactClient.createTemporaryReferenceSet() |
| 44 | + const payload = await ReactClient.createFromFetch<RscPayload>( |
| 45 | + fetch(url, { |
| 46 | + method: 'POST', |
| 47 | + body: await ReactClient.encodeReply(args, { temporaryReferences }), |
| 48 | + headers: { |
| 49 | + 'x-rsc-action': id, |
| 50 | + }, |
| 51 | + }), |
| 52 | + { temporaryReferences }, |
| 53 | + ) |
| 54 | + setPayload(payload) |
| 55 | + return payload.returnValue |
| 56 | + }) |
| 57 | + |
| 58 | + // hydration |
| 59 | + const browserRoot = ( |
| 60 | + <React.StrictMode> |
| 61 | + <BrowserRoot /> |
| 62 | + </React.StrictMode> |
| 63 | + ) |
| 64 | + ReactDOMClient.createRoot(document.body).render(browserRoot) |
| 65 | + |
| 66 | + // implement server HMR by trigering re-fetch/render of RSC upon server code change |
| 67 | + if (import.meta.hot) { |
| 68 | + import.meta.hot.on('rsc:update', () => { |
| 69 | + fetchRscPayload() |
| 70 | + }) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +// a little helper to setup events interception for client side navigation |
| 75 | +function listenNavigation(onNavigation: () => void) { |
| 76 | + window.addEventListener('popstate', onNavigation) |
| 77 | + |
| 78 | + const oldPushState = window.history.pushState |
| 79 | + window.history.pushState = function (...args) { |
| 80 | + const res = oldPushState.apply(this, args) |
| 81 | + onNavigation() |
| 82 | + return res |
| 83 | + } |
| 84 | + |
| 85 | + const oldReplaceState = window.history.replaceState |
| 86 | + window.history.replaceState = function (...args) { |
| 87 | + const res = oldReplaceState.apply(this, args) |
| 88 | + onNavigation() |
| 89 | + return res |
| 90 | + } |
| 91 | + |
| 92 | + function onClick(e: MouseEvent) { |
| 93 | + let link = (e.target as Element).closest('a') |
| 94 | + if ( |
| 95 | + link && |
| 96 | + link instanceof HTMLAnchorElement && |
| 97 | + link.href && |
| 98 | + (!link.target || link.target === '_self') && |
| 99 | + link.origin === location.origin && |
| 100 | + !link.hasAttribute('download') && |
| 101 | + e.button === 0 && // left clicks only |
| 102 | + !e.metaKey && // open in new tab (mac) |
| 103 | + !e.ctrlKey && // open in new tab (windows) |
| 104 | + !e.altKey && // download |
| 105 | + !e.shiftKey && |
| 106 | + !e.defaultPrevented |
| 107 | + ) { |
| 108 | + e.preventDefault() |
| 109 | + history.pushState(null, '', link.href) |
| 110 | + } |
| 111 | + } |
| 112 | + document.addEventListener('click', onClick) |
| 113 | + |
| 114 | + return () => { |
| 115 | + document.removeEventListener('click', onClick) |
| 116 | + window.removeEventListener('popstate', onNavigation) |
| 117 | + window.history.pushState = oldPushState |
| 118 | + window.history.replaceState = oldReplaceState |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +main() |
0 commit comments