|
| 1 | +import { renderToString } from "react-dom/server"; |
| 2 | +import { |
| 3 | + createStaticHandler, |
| 4 | + createStaticRouter, |
| 5 | + StaticRouterProvider, |
| 6 | +} from "react-router"; |
| 7 | +import routes from "./app/routes.js"; |
| 8 | +import { StrictMode } from "react"; |
| 9 | + |
| 10 | +let { query, dataRoutes, queryRoute } = createStaticHandler(routes); |
| 11 | + |
| 12 | +export async function handler(request: Request) { |
| 13 | + // Decide if this is a request for data from our client loaders or the initial |
| 14 | + // document request for HTML. React Router Vite uses [path].data to make this |
| 15 | + // decision, headers could cause problems with a CDN, but it's good for |
| 16 | + // illustration here |
| 17 | + if (request.headers.get("Accept")?.includes("application/json")) { |
| 18 | + return handleDataRequest(request); |
| 19 | + } else { |
| 20 | + return handleDocumentRequest(request); |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +export async function handleDocumentRequest(request: Request) { |
| 25 | + // 1. Run action/loaders to get the routing context with `query` |
| 26 | + let context = await query(request); |
| 27 | + |
| 28 | + // If `query` returns a Response, send it raw (a route probably a redirected) |
| 29 | + if (context instanceof Response) { |
| 30 | + return context; |
| 31 | + } |
| 32 | + |
| 33 | + // 2. Create a static router for SSR |
| 34 | + let router = createStaticRouter(dataRoutes, context); |
| 35 | + |
| 36 | + // 3. Render everything with StaticRouterProvider |
| 37 | + let html = renderToString( |
| 38 | + <StrictMode> |
| 39 | + <StaticRouterProvider router={router} context={context} /> |
| 40 | + </StrictMode>, |
| 41 | + ); |
| 42 | + |
| 43 | + // Setup headers from action and loaders from deepest match |
| 44 | + let deepestMatch = context.matches[context.matches.length - 1]; |
| 45 | + let actionHeaders = context.actionHeaders[deepestMatch.route.id]; |
| 46 | + let loaderHeaders = context.loaderHeaders[deepestMatch.route.id]; |
| 47 | + |
| 48 | + let headers = new Headers(actionHeaders); |
| 49 | + |
| 50 | + if (loaderHeaders) { |
| 51 | + for (let [key, value] of loaderHeaders.entries()) { |
| 52 | + headers.append(key, value); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + headers.set("Content-Type", "text/html; charset=utf-8"); |
| 57 | + return new Response(`<!DOCTYPE html>${html}`, { |
| 58 | + status: context.statusCode, |
| 59 | + // 4. send proper headers |
| 60 | + headers, |
| 61 | + }); |
| 62 | +} |
| 63 | + |
| 64 | +export async function handleDataRequest(request: Request) { |
| 65 | + // 1. we don't want to proxy the browser request directly to our router, so we |
| 66 | + // make a new one. |
| 67 | + let newRequest = |
| 68 | + request.method === "POST" |
| 69 | + ? new Request(request.url, { |
| 70 | + method: request.method, |
| 71 | + headers: request.headers, |
| 72 | + // @ts-expect-error this is valid, types are wrong |
| 73 | + body: new URLSearchParams(await request.formData()), |
| 74 | + }) |
| 75 | + : new Request(request.url, { headers: request.headers }); |
| 76 | + |
| 77 | + // 2. get data from our router, queryRoute knows to call the action or loader |
| 78 | + // of the leaf route that matches |
| 79 | + let data = await queryRoute(newRequest); |
| 80 | + |
| 81 | + // 3. send the response |
| 82 | + return new Response(JSON.stringify(data), { |
| 83 | + headers: { "Content-Type": "application/json" }, |
| 84 | + }); |
| 85 | +} |
0 commit comments