MCPcopy Create free account
hub / github.com/cloudflare/meet / createKvAssetHandler

Function createKvAssetHandler

server.ts:27–96  ·  view source on GitHub ↗
(ASSET_MANIFEST: Record<string, string>)

Source from the content-addressed store, hash-verified

25}
26
27export const createKvAssetHandler = (ASSET_MANIFEST: Record<string, string>) =>
28 async function handleAsset(
29 request: Request,
30 env: any,
31 ctx: any,
32 build: ServerBuild
33 ) {
34 const ASSET_NAMESPACE = env.__STATIC_CONTENT
35
36 // Apparently it's fine to fake this event to use with modules format
37 // https://github.com/cloudflare/kv-asset-handler#es-modules
38 const event = Object.assign(new Event('fetch'), {
39 request,
40 waitUntil(promise: Promise<unknown>) {
41 return ctx.waitUntil(promise)
42 },
43 // These shouldn't be used
44 respondWith: notImplemented,
45 passThroughOnException: notImplemented,
46 })
47
48 try {
49 if (mode === 'development') {
50 return await getAssetFromKV(event, {
51 cacheControl: {
52 bypassCache: true,
53 },
54 ASSET_MANIFEST,
55 ASSET_NAMESPACE,
56 })
57 }
58
59 let cacheControl = {}
60 let url = new URL(event.request.url)
61 let assetpath = build.assets.url.split('/').slice(0, -1).join('/')
62 let requestpath = url.pathname.split('/').slice(0, -1).join('/')
63
64 if (requestpath.startsWith(assetpath)) {
65 // Assets are hashed by Remix so are safe to cache in the browser
66 // And they're also hashed in KV storage, so are safe to cache on the edge
67 cacheControl = {
68 bypassCache: false,
69 edgeTTL: 31536000,
70 browserTTL: 31536000,
71 }
72 } else {
73 // Assets are not necessarily hashed in the request URL, so we cannot cache in the browser
74 // But they are hashed in KV storage, so we can cache on the edge
75 cacheControl = {
76 bypassCache: false,
77 edgeTTL: 31536000,
78 }
79 }
80
81 return await getAssetFromKV(event, {
82 cacheControl,
83 ASSET_MANIFEST,
84 ASSET_NAMESPACE,

Callers 1

server.tsFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected