-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathconfig.ts
More file actions
208 lines (183 loc) · 5.02 KB
/
config.ts
File metadata and controls
208 lines (183 loc) · 5.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import type { BuildOptions } from "@opennextjs/aws/build/helper.js";
import {
BaseOverride,
LazyLoadedOverride,
OpenNextConfig as AwsOpenNextConfig,
type RoutePreloadingBehavior,
} from "@opennextjs/aws/types/open-next.js";
import type {
CDNInvalidationHandler,
IncrementalCache,
Queue,
TagCache,
} from "@opennextjs/aws/types/overrides.js";
import assetResolver from "./overrides/asset-resolver/index.js";
export type Override<T extends BaseOverride> = "dummy" | T | LazyLoadedOverride<T>;
/**
* Cloudflare specific overrides.
*
* See the [Caching documentation](https://opennext.js.org/cloudflare/caching))
*/
export type CloudflareOverrides = {
/**
* Sets the incremental cache implementation.
*/
incrementalCache?: Override<IncrementalCache>;
/**
* Sets the tag cache implementation.
*/
tagCache?: Override<TagCache>;
/**
* Sets the revalidation queue implementation
*/
queue?: "direct" | Override<Queue>;
/**
* Sets the automatic cache purge implementation
*/
cachePurge?: Override<CDNInvalidationHandler>;
/**
* Enable cache interception
* Should be `false` when PPR is used
* @default false
*/
enableCacheInterception?: boolean;
/**
* Route preloading behavior.
* Using a value other than "none" can result in higher CPU usage on cold starts.
* @default "none"
*/
routePreloadingBehavior?: RoutePreloadingBehavior;
/**
* List of external modules to exclude from the server bundle.
*/
serverExternals?: string[];
};
/**
* Defines the OpenNext configuration that targets the Cloudflare adapter
*
* @param config options that enabled you to configure the application's behavior
* @returns the OpenNext configuration object
*/
export function defineCloudflareConfig(config: CloudflareOverrides = {}): OpenNextConfig {
const {
incrementalCache,
tagCache,
queue,
cachePurge,
enableCacheInterception = false,
routePreloadingBehavior = "none",
serverExternals = [],
} = config;
return {
default: {
override: {
wrapper: "cloudflare-node",
converter: "edge",
proxyExternalRequest: "fetch",
incrementalCache: resolveIncrementalCache(incrementalCache),
tagCache: resolveTagCache(tagCache),
queue: resolveQueue(queue),
cdnInvalidation: resolveCdnInvalidation(cachePurge),
},
routePreloadingBehavior,
},
// node:crypto is used to compute cache keys
edgeExternals: ["node:crypto"],
cloudflare: {
useWorkerdCondition: true,
},
dangerous: {
enableCacheInterception,
},
middleware: {
external: true,
override: {
wrapper: "cloudflare-edge",
converter: "edge",
proxyExternalRequest: "fetch",
incrementalCache: resolveIncrementalCache(incrementalCache),
tagCache: resolveTagCache(tagCache),
queue: resolveQueue(queue),
},
assetResolver: () => assetResolver,
},
serverExternals,
};
}
function resolveIncrementalCache(value: CloudflareOverrides["incrementalCache"] = "dummy") {
if (typeof value === "string") {
return value;
}
return typeof value === "function" ? value : () => value;
}
function resolveTagCache(value: CloudflareOverrides["tagCache"] = "dummy") {
if (typeof value === "string") {
return value;
}
return typeof value === "function" ? value : () => value;
}
function resolveQueue(value: CloudflareOverrides["queue"] = "dummy") {
if (typeof value === "string") {
return value;
}
return typeof value === "function" ? value : () => value;
}
function resolveCdnInvalidation(value: CloudflareOverrides["cachePurge"] = "dummy") {
if (typeof value === "string") {
return value;
}
return typeof value === "function" ? value : () => value;
}
interface OpenNextConfig extends AwsOpenNextConfig {
cloudflare?: {
/**
* Whether to use the "workerd" build conditions when bundling the server.
* It is recommended to set it to `true` so that code specifically targeted to the
* workerd runtime is bundled.
*
* See https://esbuild.github.io/api/#conditions
*
* @default true
*/
useWorkerdCondition?: boolean;
/**
* Disable throwing an error when the config validation fails.
* This is useful for overriding some of the default provided by cloudflare.
* **USE AT YOUR OWN RISK**
* @default false
*/
dangerousDisableConfigValidation?: boolean;
/**
* Skew protection.
*
* Note: Skew Protection is experimental and might break on minor releases.
*
* @default false
*/
skewProtection?: {
// Whether to enable skew protection
enabled?: boolean;
// Maximum number of versions to retrieve
// @default 20
maxNumberOfVersions?: number;
// Maximum age of versions to retrieve in days
// @default 7
maxVersionAgeDays?: number;
};
};
serverExternals?: string[];
}
/**
* @param buildOpts build options from AWS
* @returns The OpenConfig specific to cloudflare
*/
export function getOpenNextConfig(buildOpts: BuildOptions): OpenNextConfig {
return buildOpts.config;
}
/**
* @returns Unique deployment ID
*/
export function getDeploymentId(): string {
return `dpl-${new Date().getTime().toString(36)}`;
}
export type { OpenNextConfig };