MCPcopy
hub / github.com/TanStack/router / render

Function render

examples/react/basic-ssr-file-based/src/entry-server.tsx:11–70  ·  view source on GitHub ↗
({
  req,
  res,
  head,
}: {
  head: string
  req: express.Request
  res: express.Response
})

Source from the content-addressed store, hash-verified

9import './fetch-polyfill'
10
11export async function render({
12 req,
13 res,
14 head,
15}: {
16 head: string
17 req: express.Request
18 res: express.Response
19}) {
20 // Convert the express request to a fetch request
21 const url = new URL(req.originalUrl || req.url, 'http://localhost:3000').href
22
23 const request = new Request(url, {
24 method: req.method,
25 headers: (() => {
26 const headers = new Headers()
27 for (const [key, value] of Object.entries(req.headers)) {
28 headers.set(key, value as any)
29 }
30 return headers
31 })(),
32 })
33
34 // Create a request handler
35 const handler = createRequestHandler({
36 request,
37 createRouter: () => {
38 const router = createRouter()
39
40 // Update each router instance with the head info from vite
41 router.update({
42 context: {
43 ...router.options.context,
44 head: head,
45 },
46 })
47 return router
48 },
49 })
50
51 // Let's use the default stream handler to create the response
52 const response = await handler(({ responseHeaders, router }) =>
53 renderRouterToString({
54 responseHeaders,
55 router,
56 children: <RouterServer router={router} />,
57 }),
58 )
59
60 // Convert the fetch response back to an express response
61 res.statusMessage = response.statusText
62 res.status(response.status)
63
64 response.headers.forEach((value, name) => {
65 res.setHeader(name, value)
66 })
67
68 // Stream the response body

Callers 15

index.test.tsxFile · 0.90
index.test.tsxFile · 0.90
index.test.tsxFile · 0.90
route.test.tsxFile · 0.90
setupFunction · 0.90
not-found.test.tsxFile · 0.90
setupFunction · 0.90
runTestFunction · 0.90
link.bench.tsxFile · 0.90

Calls 6

createRouterFunction · 0.90
createRequestHandlerFunction · 0.85
setMethod · 0.65
updateMethod · 0.65
handlerFunction · 0.50
renderRouterToStringFunction · 0.50

Tested by 15

setupFunction · 0.72
setupFunction · 0.72
runTestFunction · 0.72
setupFunction · 0.72
runTestFunction · 0.72
runTestFunction · 0.72
setupTestRouterFunction · 0.72
setupFunction · 0.72
setupFunction · 0.72
setupFunction · 0.40
setupFunction · 0.40
runTestFunction · 0.40