Open History to see the last commit
1import {
2 buildManifest,
3 resolveTransformAssetsConfig,
4 transformManifestAssets,
5} from './transformAssetUrls'
6import {
7 getStaticHandlerInlineCssDefault,
8 resolveInlineCssForRequest,
9} from './inlineCss'
10import type { ServerManifest } from '@tanstack/router-core'
11import type { HandlerInlineCssOption } from './inlineCss'
12import type {
13 CreateTransformAssetsContext,
14 TransformAssets,
15 TransformAssetsFn,
16} from './transformAssetUrls'
17β
18export type { HandlerInlineCssOption, TransformAssets }
19β
20export interface FinalManifestOptions {
21 /**
22 * Controls whether Start inlines build-collected CSS by default at runtime.
23 *
24 * This only has an effect when the build was created with
25 * `server.build.inlineCss` enabled. Pass a callback to decide per request.
26 * `handler(request, { inlineCss })` overrides this value for that request.
27 *
28 * @default true
29 */
30 inlineCss?: HandlerInlineCssOption
31 /**
32 * Transform manifest-managed asset URLs and attributes at runtime, e.g. to
33 * prepend a CDN prefix.
34 *
35 * This covers JS preloads, manifest script tags, CSS links, and URLs inside
36 * build-collected inline CSS. Asset imports used directly in
37 * components should be handled by the bundler instead.
38 */
39 transformAssets?: TransformAssets
40}
41β
42type FinalManifestCacheKey = 'inline-css' | 'linked-css'
43type FinalManifestCache = Map<FinalManifestCacheKey, Promise<ServerManifest>>
44export type GetBaseManifest = () => Promise<ServerManifest>
45β
46export interface FinalManifestRequestOptions {
47 request: Request
48 requestInlineCss: boolean | undefined
49 getBaseManifest: GetBaseManifest
50}
51β
52interface FinalManifestTransformResolver {
53 cache: boolean
54 warmup: boolean
55 getTransformFn: (
56 ctx: CreateTransformAssetsContext,
57 ) => Promise<TransformAssetsFn | undefined>
58 clearCachedCreateTransform: () => void
59}
60β
61export interface FinalManifestResolver {
62 warmup: (opts: {
63 getBaseManifest: GetBaseManifest
64 }) => Promise<ServerManifest> | undefined
65 resolveCached: (opts: FinalManifestRequestOptions) => Promise<ServerManifest>
66 resolveUncached: (
67 opts: FinalManifestRequestOptions,
68 ) => Promise<ServerManifest>
69}
70β
71export function createCachedBaseManifestLoader(
72 loadBaseManifest: GetBaseManifest,
73): GetBaseManifest {
74 let baseManifestPromise: Promise<ServerManifest> | undefined
75β
76 return () => {
77 if (!baseManifestPromise) {
78 baseManifestPromise = loadBaseManifest().catch((error) => {
79 baseManifestPromise = undefined
80 throw error
81 })
82 }
83β
84 return baseManifestPromise
85 }
86}
87β
88function createFinalManifestTransformResolver(
89 transformAssets: TransformAssets | undefined,
90 opts: { cacheCreateTransform: boolean },
91): FinalManifestTransformResolver {
92 const transformConfig =
93 transformAssets !== undefined
94 ? resolveTransformAssetsConfig(transformAssets)
95 : undefined
96 const cache = transformConfig ? transformConfig.cache : true
97 const warmup =
98 !!transformAssets &&
99 typeof transformAssets === 'object' &&
100 'warmup' in transformAssets &&
101 transformAssets.warmup === true
102β
103 let cachedCreateTransformPromise: Promise<TransformAssetsFn> | undefined
104β
105 const clearCachedCreateTransform = () => {
106 cachedCreateTransformPromise = undefined
107 }
108β
109 return {
110 cache,
111 warmup,
112 clearCachedCreateTransform,
113 getTransformFn: async (ctx) => {
114 if (!transformConfig) return undefined
115β
116 if (transformConfig.type !== 'createTransform') {
117 return transformConfig.transformFn
118 }
119β
120 if (!cache || !opts.cacheCreateTransform) {
121 return transformConfig.createTransform(ctx)
122 }
123β
124 if (!cachedCreateTransformPromise) {
125 cachedCreateTransformPromise = Promise.resolve(
126 transformConfig.createTransform(ctx),
127 ).catch((error) => {
128 clearCachedCreateTransform()
129 throw error
130 })
131 }
132β
133 return cachedCreateTransformPromise
134 },
135 }
136}
137β
138export function createFinalManifestResolver(
139 opts: FinalManifestOptions & { cacheCreateTransform: boolean },
140): FinalManifestResolver {
141 const finalManifestCache: FinalManifestCache = new Map()
142 const transformResolver = createFinalManifestTransformResolver(
143 opts.transformAssets,
144 { cacheCreateTransform: opts.cacheCreateTransform },
145 )
146 const handlerDefaultInlineCss = getStaticHandlerInlineCssDefault(
147 opts.inlineCss,
148 )
149β
150 const getRequestManifestOptions = async (
151 requestOpts: FinalManifestRequestOptions,
152 ) => {
153 const transformFn = await transformResolver.getTransformFn({
154 warmup: false,
155 request: requestOpts.request,
156 })
157 const inlineCss = await resolveInlineCssForRequest({
158 request: requestOpts.request,
159 handlerInlineCss: opts.inlineCss,
160 requestInlineCss: requestOpts.requestInlineCss,
161 })
162β
163 return {
164 getBaseManifest: requestOpts.getBaseManifest,
165 transformFn,
166 cache: transformResolver.cache,
167 inlineCss,
168 }
169 }
170β
171 const resolveRequest = async (
172 requestOpts: FinalManifestRequestOptions,
173 cache: FinalManifestCache | undefined,
174 ) => {
175 return resolveFinalManifest({
176 ...(await getRequestManifestOptions(requestOpts)),
177 finalManifestCache: cache,
178 })
179 }
180β
181 return {
182 warmup: ({ getBaseManifest }) =>
183 warmupFinalManifest({
184 enabled: transformResolver.warmup,
185 handlerDefaultInlineCss,
186 cache: transformResolver.cache,
187 finalManifestCache,
188 getBaseManifest,
189 getTransformFn: () =>
190 transformResolver.getTransformFn({ warmup: true }),
191 onError: transformResolver.clearCachedCreateTransform,
192 }),
193 resolveCached: (requestOpts) =>
194 resolveRequest(requestOpts, finalManifestCache),
195 resolveUncached: (requestOpts) => resolveRequest(requestOpts, undefined),
196 }
197}
198β
199function getFinalManifestCacheKey(inlineCss: boolean): FinalManifestCacheKey {
200 return inlineCss ? 'inline-css' : 'linked-css'
201}
202β
203function cacheFinalManifestPromise(
204 cachedFinalManifestPromises: FinalManifestCache,
205 cacheKey: FinalManifestCacheKey,
206 promise: Promise<ServerManifest>,
207): Promise<ServerManifest> {
208 const cachedFinalManifestPromise = promise.catch((error) => {
209 if (
210 cachedFinalManifestPromises.get(cacheKey) === cachedFinalManifestPromise
211 ) {
212 cachedFinalManifestPromises.delete(cacheKey)
213 }
214 throw error
215 })
216β
217 cachedFinalManifestPromises.set(cacheKey, cachedFinalManifestPromise)
218 return cachedFinalManifestPromise
219}
220β
221function getOrCreateCachedFinalManifestPromise(
222 cachedFinalManifestPromises: FinalManifestCache,
223 cacheKey: FinalManifestCacheKey,
224 computeFinalManifest: () => Promise<ServerManifest>,
225): Promise<ServerManifest> {
226 const cachedFinalManifestPromise = cachedFinalManifestPromises.get(cacheKey)
227 if (cachedFinalManifestPromise) {
228 return cachedFinalManifestPromise
229 }
230β
231 return cacheFinalManifestPromise(
232 cachedFinalManifestPromises,
233 cacheKey,
234 Promise.resolve().then(computeFinalManifest),
235 )
236}
237β
238async function buildFinalManifest(opts: {
239 base: ServerManifest
240 transformFn: TransformAssetsFn | undefined
241 inlineCss: boolean
242}): Promise<ServerManifest> {
243 return opts.transformFn
244 ? await transformManifestAssets(opts.base, opts.transformFn, {
245 inlineCss: opts.inlineCss,
246 })
247 : buildManifest(opts.base, { inlineCss: opts.inlineCss })
248}
249β
250async function resolveFinalManifest(opts: {
251 getBaseManifest: () => Promise<ServerManifest>
252 transformFn: TransformAssetsFn | undefined
253 cache: boolean
254 inlineCss: boolean
255 finalManifestCache?: FinalManifestCache
256}): Promise<ServerManifest> {
257 const computeFinalManifest = async () => {
258 return buildFinalManifest({
259 base: await opts.getBaseManifest(),
260 transformFn: opts.transformFn,
261 inlineCss: opts.inlineCss,
262 })
263 }
264β
265 if (opts.finalManifestCache && (!opts.transformFn || opts.cache)) {
266 return getOrCreateCachedFinalManifestPromise(
267 opts.finalManifestCache,
268 getFinalManifestCacheKey(opts.inlineCss),
269 computeFinalManifest,
270 )
271 }
272β
273 return computeFinalManifest()
274}
275β
276function warmupFinalManifest(opts: {
277 enabled: boolean
278 handlerDefaultInlineCss: boolean | undefined
279 cache: boolean
280 finalManifestCache: FinalManifestCache
281 getBaseManifest: () => Promise<ServerManifest>
282 getTransformFn: () => Promise<TransformAssetsFn | undefined>
283 onError?: () => void
284}): Promise<ServerManifest> | undefined {
285 if (
286 !opts.enabled ||
287 opts.handlerDefaultInlineCss === undefined ||
288 !opts.cache
289 ) {
290 return undefined
291 }
292β
293 const inlineCss = opts.handlerDefaultInlineCss
294 const warmupPromise = getOrCreateCachedFinalManifestPromise(
295 opts.finalManifestCache,
296 getFinalManifestCacheKey(inlineCss),
297 async () => {
298 const [base, transformFn] = await Promise.all([
299 opts.getBaseManifest(),
300 opts.getTransformFn(),
301 ])
302β
303 return buildFinalManifest({
304 base,
305 transformFn,
306 inlineCss,
307 })
308 },
309 )
310β
311 if (opts.onError) {
312 void warmupPromise.catch(opts.onError)
313 }
314β
315 return warmupPromise
316}