Open History to see the last commit
1import type { OnEarlyHints, ResponseLinkHeaderOptions } from './early-hints'
2β
3type BaseContext = {
4 nonce?: string
5}
6β
7type EarlyHintsOptions = {
8 /**
9 * Fire-and-forget callback for HTTP 103 Early Hints.
10 * Only invoked in production (when TSS_DEV_SERVER !== 'true').
11 *
12 * The `static` phase contains transformed manifest preloads and stylesheets
13 * for matched routes.
14 * The `dynamic` phase runs after route load, is skipped for redirects, and
15 * can contain route `head().links` or empty `hints` and `links` arrays.
16 * `hints` and `links` contain only values not emitted in earlier phases.
17 * `allHints` and `allLinks` contain all values collected so far for the
18 * request. Browsers generally process only the first 103 response for a
19 * navigation, so runtimes should usually write at most one Early Hints
20 * response.
21 *
22 * @example
23 * ```ts
24 * export default {
25 * async fetch(request) {
26 * return handler.fetch(request, {
27 * onEarlyHints: ({ links }) => {
28 * // Send 103 Early Hints via runtime-specific API
29 * }
30 * })
31 * }
32 * }
33 * ```
34 */
35 onEarlyHints?: OnEarlyHints
36 /**
37 * Append collected Early Hints `Link` values to the final HTML response's
38 * `Link` header. This is useful as a fallback when the runtime cannot write
39 * `103` responses, or for CDNs that generate Early Hints from response
40 * `Link` headers.
41 *
42 * `true` appends all collected static and dynamic links after Start confirms
43 * the request is not a redirect. Use `filter` to remove links that are not
44 * public and cache-safe for your deployment.
45 */
46 responseLinkHeader?: boolean | ResponseLinkHeaderOptions
47}
48β
49type InlineCssOptions = {
50 /**
51 * Controls whether Start inlines build-collected CSS for this request.
52 *
53 * This only has an effect when the build was created with
54 * `server.build.inlineCss` enabled. Defaults to `true` so builds with inline
55 * CSS enabled continue to inline CSS unless a request opts out.
56 */
57 inlineCss?: boolean
58}
59β
60export type RequestOptions<TRegister> = EarlyHintsOptions &
61 InlineCssOptions &
62 (TRegister extends {
63 server: { requestContext: infer TRequestContext }
64 }
65 ? TRequestContext extends undefined
66 ? { context?: TRequestContext & BaseContext }
67 : { context: TRequestContext & BaseContext }
68 : { context?: BaseContext })
69β
70// Utility type: true if T has any required keys, else false
71type HasRequired<T> = keyof T extends never
72 ? false
73 : {
74 [K in keyof T]-?: undefined extends T[K] ? never : K
75 }[keyof T] extends never
76 ? false
77 : true
78β
79export type RequestHandler<TRegister> =
80 HasRequired<RequestOptions<TRegister>> extends true
81 ? (
82 request: Request,
83 opts: RequestOptions<TRegister>,
84 ) => Promise<Response> | Response
85 : (
86 request: Request,
87 opts?: RequestOptions<TRegister>,
88 ) => Promise<Response> | Response