File
Blob: src/client/server/manifest.ts
| 1 | type ViteManifestEntry = { |
| 2 | file: string; |
| 3 | css?: string[]; |
| 4 | imports?: string[]; |
| 5 | }; |
| 6 | |
| 7 | type ViteManifest = Record<string, ViteManifestEntry>; |
| 8 | |
| 9 | let manifestPromise: Promise<ViteManifest | null> | null = null; |
| 10 | |
| 11 | async function fetchManifest(env: Env): Promise<ViteManifest | null> { |
| 12 | if (!env.ASSETS) { |
| 13 | return null; |
| 14 | } |
| 15 | |
| 16 | const url = new URL("/manifest.json", "https://assets.local"); |
| 17 | const response = await env.ASSETS.fetch(new Request(url.toString())); |
| 18 | if (!response.ok) { |
| 19 | return null; |
| 20 | } |
| 21 | |
| 22 | return (await response.json()) as ViteManifest; |
| 23 | } |
| 24 | |
| 25 | export async function getClientManifest(env: Env): Promise<ViteManifest | null> { |
| 26 | if (import.meta.env.DEV) { |
| 27 | return null; |
| 28 | } |
| 29 | |
| 30 | if (manifestPromise) { |
| 31 | return manifestPromise; |
| 32 | } |
| 33 | |
| 34 | const promise = fetchManifest(env); |
| 35 | manifestPromise = promise; |
| 36 | promise.catch(() => { |
| 37 | manifestPromise = null; |
| 38 | }); |
| 39 | return promise.catch(() => null); |
| 40 | } |