MCPcopy
hub / github.com/vercel/next.js / invokeRender

Function invokeRender

packages/next/src/server/lib/router-server.ts:298–369  ·  view source on GitHub ↗
(
      parsedUrl: NextUrlWithParsedQuery,
      invokePath: string,
      handleIndex: number,
      additionalRequestMeta?: RequestMeta
    )

Source from the content-addressed store, hash-verified

296 const invokedOutputs = new Set<string>()
297
298 async function invokeRender(
299 parsedUrl: NextUrlWithParsedQuery,
300 invokePath: string,
301 handleIndex: number,
302 additionalRequestMeta?: RequestMeta
303 ) {
304 // invokeRender expects /api routes to not be locale prefixed
305 // so normalize here before continuing
306 if (
307 config.i18n &&
308 removePathPrefix(invokePath, config.basePath).startsWith(
309 `/${getRequestMeta(req, 'locale')}/api`
310 )
311 ) {
312 invokePath = fsChecker.handleLocale(
313 removePathPrefix(invokePath, config.basePath)
314 ).pathname
315 }
316
317 if (
318 req.headers['x-nextjs-data'] &&
319 fsChecker.getMiddlewareMatchers()?.length &&
320 removePathPrefix(invokePath, config.basePath) === '/404'
321 ) {
322 res.setHeader('x-nextjs-matched-path', parsedUrl.pathname || '')
323 res.statusCode = 404
324 res.setHeader('content-type', 'application/json')
325 res.end('{}')
326 return null
327 }
328
329 if (!handlers) {
330 throw new Error('Failed to initialize render server')
331 }
332
333 addRequestMeta(req, 'invokePath', invokePath)
334 addRequestMeta(req, 'invokeQuery', parsedUrl.query)
335 addRequestMeta(req, 'middlewareInvoke', false)
336
337 for (const key in additionalRequestMeta || {}) {
338 addRequestMeta(
339 req,
340 key as keyof RequestMeta,
341 additionalRequestMeta![key as keyof RequestMeta]
342 )
343 }
344
345 debug('invokeRender', req.url, req.headers)
346
347 try {
348 const initResult =
349 await renderServer?.instance?.initialize(renderServerOpts)
350 try {
351 await initResult?.requestHandler(req, res)
352 } catch (err) {
353 if (err instanceof NoFallbackError) {
354 await handleRequest(handleIndex + 1)
355 return

Callers 2

handleRequestFunction · 0.85
requestHandlerImplFunction · 0.85

Calls 11

removePathPrefixFunction · 0.90
getRequestMetaFunction · 0.90
addRequestMetaFunction · 0.90
isAbortErrorFunction · 0.90
startsWithMethod · 0.80
requestHandlerMethod · 0.80
handleRequestFunction · 0.70
endMethod · 0.65
debugFunction · 0.50
setHeaderMethod · 0.45
initializeMethod · 0.45

Tested by

no test coverage detected