| 519 | * ``` |
| 520 | */ |
| 521 | export function createStartHandler<TRegister = Register>( |
| 522 | cbOrOptions: HandlerCallback<AnyRouter> | CreateStartHandlerOptions, |
| 523 | ): RequestHandler<TRegister> { |
| 524 | const handlerOptions: FinalManifestOptions = |
| 525 | typeof cbOrOptions === 'function' ? {} : cbOrOptions |
| 526 | const cb: HandlerCallback<AnyRouter> = |
| 527 | typeof cbOrOptions === 'function' ? cbOrOptions : cbOrOptions.handler |
| 528 | const finalManifestResolver = createFinalManifestResolver({ |
| 529 | ...handlerOptions, |
| 530 | cacheCreateTransform: process.env.TSS_DEV_SERVER !== 'true', |
| 531 | }) |
| 532 | const resolveManifestForRequest = |
| 533 | process.env.TSS_DEV_SERVER === 'true' |
| 534 | ? finalManifestResolver.resolveUncached |
| 535 | : finalManifestResolver.resolveCached |
| 536 | |
| 537 | if (process.env.TSS_DEV_SERVER !== 'true') { |
| 538 | finalManifestResolver.warmup({ |
| 539 | getBaseManifest: () => getBaseManifest(undefined), |
| 540 | }) |
| 541 | } |
| 542 | |
| 543 | const startRequestResolver: RequestHandler<Register> = async ( |
| 544 | request, |
| 545 | requestOpts, |
| 546 | ) => { |
| 547 | const signal = request.signal |
| 548 | let router: AnyRouter | undefined |
| 549 | let routerPromise: Promise<AnyRouter> | undefined |
| 550 | let responseOwnsCleanup = false |
| 551 | |
| 552 | try { |
| 553 | signal.throwIfAborted() |
| 554 | // normalizing and sanitizing the pathname here for server, so we always deal with the same format during SSR. |
| 555 | // during normalization paths like '//posts' are flattened to '/posts'. |
| 556 | // in these cases we would prefer to redirect to the new path |
| 557 | const { url, handledProtocolRelativeURL } = getNormalizedURL(request.url) |
| 558 | const href = url.pathname + url.search + url.hash |
| 559 | const origin = url.origin |
| 560 | |
| 561 | if (handledProtocolRelativeURL) { |
| 562 | return Response.redirect(url, 308) |
| 563 | } |
| 564 | |
| 565 | const entries = await waitForRequest(getEntries(), signal) |
| 566 | const isServerFnRequest = |
| 567 | !!SERVER_FN_BASE && url.pathname.startsWith(SERVER_FN_BASE) |
| 568 | const startInstance = entries.startEntry.startInstance |
| 569 | let startOptions: AnyStartInstanceOptions |
| 570 | if (startInstance) { |
| 571 | const pendingStartOptions = startInstance.getOptions() |
| 572 | startOptions = isPromise(pendingStartOptions) |
| 573 | ? await waitForRequest(pendingStartOptions, signal) |
| 574 | : pendingStartOptions |
| 575 | signal.throwIfAborted() |
| 576 | } else { |
| 577 | startOptions = {} as AnyStartInstanceOptions |
| 578 | } |