@@ -7,6 +7,7 @@ import assetManifestJSON from '__STATIC_CONTENT_MANIFEST';
77export interface Env {
88 // CF Worker Sites automatically injects this to point to the KV namespace
99 __STATIC_CONTENT : string ;
10+ POLYKEY_DOCS_ENV : string ;
1011}
1112
1213const router = ittyRouter . Router ( ) ;
@@ -30,9 +31,8 @@ function mapRequestToDocs(req: Request): Request {
3031
3132router . all ( '*' , async ( req : Request , env : Env , ctx : ExecutionContext ) => {
3233 const cacheControl = {
33- browserTTL : 30 * 24 * 60 * 60 ,
3434 edgeTTL : 2 * 24 * 60 * 60 ,
35- bypassCache : false ,
35+ bypassCache : env . POLYKEY_DOCS_ENV === 'development' ? true : false ,
3636 } ;
3737 const url = new URL ( req . url ) ;
3838 // Check if the URL pathname is exactly '/docs'
@@ -44,7 +44,7 @@ router.all('*', async (req: Request, env: Env, ctx: ExecutionContext) => {
4444 // wrangler as a cache busting measure.
4545 const assetManifest = JSON . parse ( assetManifestJSON ) ;
4646 try {
47- return await cloudflareKVAssetHandler . getAssetFromKV (
47+ const response = await cloudflareKVAssetHandler . getAssetFromKV (
4848 {
4949 request : req ,
5050 waitUntil : ctx . waitUntil . bind ( ctx ) ,
@@ -56,6 +56,14 @@ router.all('*', async (req: Request, env: Env, ctx: ExecutionContext) => {
5656 ASSET_MANIFEST : assetManifest ,
5757 } ,
5858 ) ;
59+
60+ if ( req . url . includes ( 'assets' ) ) {
61+ response . headers . set ( 'Cache-Control' , 'max-age=31536000, immutable' ) ;
62+ } else {
63+ response . headers . set ( 'Cache-Control' , 'max-age=86400' ) ;
64+ }
65+
66+ return response ;
5967 } catch ( e ) {
6068 if ( e instanceof cloudflareKVAssetHandler . NotFoundError ) {
6169 console . log ( 'Requested resource not found' , e . message ) ;
@@ -74,8 +82,14 @@ router.all('*', async (req: Request, env: Env, ctx: ExecutionContext) => {
7482 ASSET_MANIFEST : assetManifest ,
7583 } ,
7684 ) ;
85+
86+ const headers = new Headers ( response404 . headers ) ;
87+
88+ headers . set ( 'Cache-Control' , 'max-age=86400' ) ;
89+
7790 return new Response ( response404 . body , {
7891 ...response404 ,
92+ headers,
7993 status : 404 ,
8094 } ) ;
8195 } else if ( e instanceof cloudflareKVAssetHandler . MethodNotAllowedError ) {
0 commit comments