1
- import { Server } from 'SERVER' ;
2
- import { manifest , prerendered } from 'MANIFEST' ;
3
- import { getAssetFromKV , mapRequestToAsset } from '@cloudflare/kv-asset-handler' ;
4
- import static_asset_manifest_json from '__STATIC_CONTENT_MANIFEST' ;
1
+ import { Server } from "SERVER" ;
2
+ import { manifest , prerendered } from "MANIFEST" ;
3
+ import {
4
+ getAssetFromKV ,
5
+ mapRequestToAsset ,
6
+ MethodNotAllowedError ,
7
+ NotFoundError ,
8
+ } from "@cloudflare/kv-asset-handler" ;
9
+ import static_asset_manifest_json from "__STATIC_CONTENT_MANIFEST" ;
5
10
const static_asset_manifest = JSON . parse ( static_asset_manifest_json ) ;
6
11
7
12
const server = new Server ( manifest ) ;
@@ -25,23 +30,23 @@ export default {
25
30
const res = await get_asset_from_kv ( req , env , context ) ;
26
31
if ( is_error ( res . status ) ) return res ;
27
32
28
- const cache_control = url . pathname . startsWith ( app_path + ' immutable/' )
29
- ? ' public, immutable, max-age=31536000'
30
- : ' no-cache' ;
33
+ const cache_control = url . pathname . startsWith ( app_path + " immutable/" )
34
+ ? " public, immutable, max-age=31536000"
35
+ : " no-cache" ;
31
36
32
37
return new Response ( res . body , {
33
38
headers : {
34
39
// include original headers, minus cache-control which
35
40
// is overridden, and etag which is no longer useful
36
- ' cache-control' : cache_control ,
37
- ' content-type' : res . headers . get ( ' content-type' ) ,
38
- ' x-robots-tag' : ' noindex'
39
- }
41
+ " cache-control" : cache_control ,
42
+ " content-type" : res . headers . get ( " content-type" ) ,
43
+ " x-robots-tag" : " noindex" ,
44
+ } ,
40
45
} ) ;
41
46
}
42
47
43
48
// prerendered pages and index.html files
44
- const pathname = url . pathname . replace ( / \/ $ / , '' ) ;
49
+ const pathname = url . pathname . replace ( / \/ $ / , "" ) ;
45
50
let file = pathname . substring ( 1 ) ;
46
51
47
52
try {
@@ -52,12 +57,12 @@ export default {
52
57
53
58
if (
54
59
manifest . assets . has ( file ) ||
55
- manifest . assets . has ( file + ' /index.html' ) ||
56
- prerendered . has ( pathname || '/' )
60
+ manifest . assets . has ( file + " /index.html" ) ||
61
+ prerendered . has ( pathname || "/" )
57
62
) {
58
63
return get_asset_from_kv ( req , env , context , ( request , options ) => {
59
- if ( prerendered . has ( pathname || '/' ) ) {
60
- url . pathname = '/' + prerendered . get ( pathname || '/' ) . file ;
64
+ if ( prerendered . has ( pathname || "/" ) ) {
65
+ url . pathname = "/" + prerendered . get ( pathname || "/" ) . file ;
61
66
return new Request ( url . toString ( ) , request ) ;
62
67
}
63
68
@@ -71,13 +76,13 @@ export default {
71
76
env,
72
77
context,
73
78
// @ts -expect-error lib.dom is interfering with workers-types
74
- caches
79
+ caches,
75
80
} ,
76
81
getClientAddress ( ) {
77
- return req . headers . get ( ' cf-connecting-ip' ) ;
78
- }
82
+ return req . headers . get ( " cf-connecting-ip" ) ;
83
+ } ,
79
84
} ) ;
80
- }
85
+ } ,
81
86
} ;
82
87
83
88
/**
@@ -86,19 +91,29 @@ export default {
86
91
* @param {any } context
87
92
*/
88
93
async function get_asset_from_kv ( req , env , context , map = mapRequestToAsset ) {
89
- return await getAssetFromKV (
90
- {
91
- request : req ,
92
- waitUntil ( promise ) {
93
- return context . waitUntil ( promise ) ;
94
- }
95
- } ,
96
- {
97
- ASSET_NAMESPACE : env . __STATIC_CONTENT ,
98
- ASSET_MANIFEST : static_asset_manifest ,
99
- mapRequestToAsset : map
94
+ try {
95
+ return await getAssetFromKV (
96
+ {
97
+ request : req ,
98
+ waitUntil ( promise ) {
99
+ return context . waitUntil ( promise ) ;
100
+ } ,
101
+ } ,
102
+ {
103
+ ASSET_NAMESPACE : env . __STATIC_CONTENT ,
104
+ ASSET_MANIFEST : static_asset_manifest ,
105
+ mapRequestToAsset : map ,
106
+ } ,
107
+ ) ;
108
+ } catch ( e ) {
109
+ if ( e instanceof NotFoundError ) {
110
+ return new Response ( "Not found" , { status : 404 } ) ;
111
+ } else if ( e instanceof MethodNotAllowedError ) {
112
+ return new Response ( "Method not allowed" , { status : 405 } ) ;
113
+ } else {
114
+ return new Response ( "Internal Error" , { status : 500 } ) ;
100
115
}
101
- ) ;
116
+ }
102
117
}
103
118
104
119
/**
0 commit comments