|
1 | 1 | import './instrument.server.mjs' |
2 | 2 |
|
3 | 3 | import handler, { createServerEntry } from '@tanstack/react-start/server-entry' |
| 4 | +import { |
| 5 | + installProductionFetchProbe, |
| 6 | + installProductionProcessProbe, |
| 7 | + logRequestEnd, |
| 8 | + logRequestError, |
| 9 | + logRequestStart, |
| 10 | + runWithRequestDiagnostics, |
| 11 | +} from '~/utils/prod-diagnostics.server' |
| 12 | + |
| 13 | +installProductionFetchProbe() |
| 14 | +installProductionProcessProbe() |
4 | 15 |
|
5 | 16 | export default createServerEntry({ |
6 | 17 | async fetch(request) { |
7 | | - const url = new URL(request.url) |
8 | | - |
9 | | - // Redirect to markdown version if AI/LLM requests text/markdown for doc pages |
10 | | - const acceptHeader = request.headers.get('Accept') || '' |
11 | | - if ( |
12 | | - acceptHeader.includes('text/markdown') && |
13 | | - url.pathname.includes('/docs/') && |
14 | | - !url.pathname.endsWith('.md') |
15 | | - ) { |
16 | | - return new Response(null, { |
17 | | - status: 303, |
18 | | - headers: { |
19 | | - Location: `${url.pathname}.md`, |
20 | | - }, |
21 | | - }) |
22 | | - } |
23 | | - |
24 | | - const response = await handler.fetch(request) |
25 | | - |
26 | | - // Add COOP/COEP headers for /builder route (required for WebContainer) |
27 | | - if (url.pathname === '/builder' || url.pathname.startsWith('/builder/')) { |
28 | | - const newHeaders = new Headers(response.headers) |
29 | | - newHeaders.set('Cross-Origin-Opener-Policy', 'same-origin') |
30 | | - newHeaders.set('Cross-Origin-Embedder-Policy', 'require-corp') |
31 | | - |
32 | | - return new Response(response.body, { |
33 | | - status: response.status, |
34 | | - statusText: response.statusText, |
35 | | - headers: newHeaders, |
36 | | - }) |
37 | | - } |
38 | | - |
39 | | - return response |
| 18 | + return runWithRequestDiagnostics(request, async (context) => { |
| 19 | + const url = new URL(request.url) |
| 20 | + logRequestStart(context) |
| 21 | + |
| 22 | + try { |
| 23 | + const acceptHeader = request.headers.get('Accept') || '' |
| 24 | + if ( |
| 25 | + acceptHeader.includes('text/markdown') && |
| 26 | + url.pathname.includes('/docs/') && |
| 27 | + !url.pathname.endsWith('.md') |
| 28 | + ) { |
| 29 | + logRequestEnd(context, 303, { redirectToMarkdown: true }) |
| 30 | + return new Response(null, { |
| 31 | + status: 303, |
| 32 | + headers: { |
| 33 | + Location: `${url.pathname}.md`, |
| 34 | + }, |
| 35 | + }) |
| 36 | + } |
| 37 | + |
| 38 | + const response = await handler.fetch(request) |
| 39 | + |
| 40 | + if ( |
| 41 | + url.pathname === '/builder' || |
| 42 | + url.pathname.startsWith('/builder/') |
| 43 | + ) { |
| 44 | + const newHeaders = new Headers(response.headers) |
| 45 | + newHeaders.set('Cross-Origin-Opener-Policy', 'same-origin') |
| 46 | + newHeaders.set('Cross-Origin-Embedder-Policy', 'require-corp') |
| 47 | + |
| 48 | + logRequestEnd(context, response.status, { |
| 49 | + builderIsolatedHeaders: true, |
| 50 | + }) |
| 51 | + return new Response(response.body, { |
| 52 | + status: response.status, |
| 53 | + statusText: response.statusText, |
| 54 | + headers: newHeaders, |
| 55 | + }) |
| 56 | + } |
| 57 | + |
| 58 | + logRequestEnd(context, response.status) |
| 59 | + return response |
| 60 | + } catch (error) { |
| 61 | + logRequestError(context, error) |
| 62 | + throw error |
| 63 | + } |
| 64 | + }) |
40 | 65 | }, |
41 | 66 | }) |
0 commit comments