All files / src start.ts

0% Statements 0/7
0% Branches 0/2
0% Functions 0/3
0% Lines 0/6

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35                                                                     
// TanStack Start's global setup:
// - Server functions are same-origin calls only: a call from another site is
//   refused with 403 (Sec-Fetch-Site, then Origin, then Referer). Session
//   cookies are SameSite=Lax, which already stops cross-site POSTs; this
//   covers GETs and browsers that don't send SameSite.
// - Every server function call from the browser carries the page's trace
//   (src/lib/trace.ts), so the server's log lines for it share the browser's
//   trace id.
import {
  createCsrfMiddleware,
  createMiddleware,
  createStart,
} from '@tanstack/react-start'
import { browserTraceId, traceparent } from '#/lib/trace'
 
const traceMiddleware = createMiddleware({ type: 'function' }).client(
  ({ next }) => {
    const traceId = browserTraceId()
    return next(
      traceId === undefined
        ? {}
        : { headers: { traceparent: traceparent(traceId) } },
    )
  },
)
 
const csrfMiddleware = createCsrfMiddleware({
  filter: (ctx) => ctx.handlerType === 'serverFn',
})
 
export const startInstance = createStart(() => ({
  requestMiddleware: [csrfMiddleware],
  functionMiddleware: [traceMiddleware],
}))