@@ -25,25 +25,21 @@ export const CURRENT_VERSION_ID = "current";
25
25
* @param request
26
26
* @returns
27
27
*/
28
- export function maybeGetSkewProtectionResponse (
29
- request : Request ,
30
- assets : Fetcher | undefined
31
- ) : Promise < Response > | Response | undefined {
28
+ export function maybeGetSkewProtectionResponse ( request : Request ) : Promise < Response > | Response | undefined {
32
29
// no early return as esbuild would not treeshake the code.
33
30
if ( __SKEW_PROTECTION_ENABLED__ ) {
34
31
const url = new URL ( request . url ) ;
35
32
36
33
// Skew protection is only active for the latest version of the app served on a custom domain.
37
- // For `localhost` and older deployments we still need to serve assets when the worker runs first.
38
34
if ( url . hostname === "localhost" || url . pathname . endsWith ( ".workers.dev" ) ) {
39
- return maybeFetchAsset ( request , assets ) ;
35
+ return undefined ;
40
36
}
41
37
42
38
const requestDeploymentId = request . headers . get ( "x-deployment-id" ) ?? url . searchParams . get ( "dpl" ) ;
43
39
44
40
if ( ! requestDeploymentId || requestDeploymentId === process . env . DEPLOYMENT_ID ) {
45
41
// The request does not specify a deployment id or it is the current deployment id
46
- return maybeFetchAsset ( request , assets ) ;
42
+ return undefined ;
47
43
}
48
44
49
45
const mapping = process . env [ DEPLOYMENT_MAPPING_ENV_NAME ]
@@ -69,69 +65,8 @@ export function maybeGetSkewProtectionResponse(
69
65
}
70
66
}
71
67
72
- /**
73
- * Fetches a file from the assets when the path is a known asset.
74
- *
75
- * @param request The incoming request
76
- * @param assets The Fetcher used to retrieve assets
77
- * @returns A `Promise<Response>` when the path is an assets, undefined otherwise
78
- */
79
- function maybeFetchAsset ( request : Request , assets : Fetcher | undefined ) : Promise < Response > | undefined {
80
- if ( ! assets ) {
81
- return undefined ;
82
- }
83
-
84
- let path = new URL ( request . url ) . pathname ;
85
- const basePath = globalThis . __NEXT_BASE_PATH__ ;
86
- if ( basePath && path . startsWith ( basePath ) ) {
87
- path = path . slice ( basePath . length ) ;
88
- }
89
- if ( path . startsWith ( "/_next/static/" ) || isFileInTree ( path , __CF_ASSETS_TREE__ ) ) {
90
- return assets . fetch ( request ) ;
91
- }
92
- }
93
-
94
- /**
95
- * A node represents a folder in the file tree
96
- */
97
- export type FolderNode = {
98
- // List of files in this folder
99
- f : string [ ] ;
100
- // Sub-folders.
101
- d : Record < string , FolderNode > ;
102
- } ;
103
-
104
- /**
105
- * Checks whether a file is in the tree
106
- *
107
- * @param filepath The path to the file
108
- * @param tree The root node of the tree
109
- * @returns Whether the file is in this tree
110
- */
111
- export function isFileInTree ( filepath : string , tree : FolderNode ) : boolean {
112
- // Split the filename into components, filtering out empty strings from potential leading/trailing slashes
113
- const parts = filepath . split ( "/" ) . filter ( Boolean ) ;
114
-
115
- if ( parts . length === 0 ) {
116
- return false ; // An empty filename cannot be in the tree
117
- }
118
-
119
- let currentNode : FolderNode | undefined = tree ;
120
-
121
- // Traverse through folder parts
122
- for ( let i = 0 ; i < parts . length - 1 ; i ++ ) {
123
- currentNode = currentNode . d [ parts [ i ] as string ] ;
124
- if ( ! currentNode ) {
125
- return false ; // Folder not found in the tree
126
- }
127
- }
128
- // Check if the file exists in the current node's files array
129
- return currentNode . f . includes ( parts . at ( - 1 ) as string ) ;
130
- }
131
-
132
68
/* eslint-disable no-var */
133
69
declare global {
134
70
var __SKEW_PROTECTION_ENABLED__ : boolean ;
135
- var __CF_ASSETS_TREE__ : FolderNode ;
136
71
}
137
72
/* eslint-enable no-var */
0 commit comments