-
Notifications
You must be signed in to change notification settings - Fork 4
/
cloudflare-pages-worker.ts
165 lines (138 loc) · 5.05 KB
/
cloudflare-pages-worker.ts
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
import type { AppLoadContext, ServerBuild } from "@remix-run/cloudflare";
import { createRequestHandler as createRemixRequestHandler } from "@remix-run/cloudflare";
/**
* A function that returns the value to use as `context` in route `loader` and
* `action` functions.
*
* You can think of this as an escape hatch that allows you to pass
* environment/platform-specific values through to your loader/action.
*/
export type GetLoadContextFunction<Env = any> = (
context: EventContext<Env, any, any>,
) => AppLoadContext;
export type RequestHandler<Env = any> = PagesFunction<Env>;
export interface createPagesFunctionHandlerParams<Env = any> {
build: ServerBuild;
getLoadContext?: GetLoadContextFunction<Env>;
mode?: string;
}
export function createRequestHandler<Env = any>({
build,
getLoadContext,
mode,
}: createPagesFunctionHandlerParams<Env>): RequestHandler<Env> {
let handleRequest = createRemixRequestHandler(build, mode);
return (context) => {
let loadContext = getLoadContext?.(context);
return handleRequest(context.request, loadContext);
};
}
declare const process: any;
export function createPagesFunctionHandler<Env = any>({
build,
getLoadContext,
mode,
}: createPagesFunctionHandlerParams<Env>) {
let handleRequest = createRequestHandler<Env>({
build,
getLoadContext,
mode,
});
let handleFetch = async (context: EventContext<Env, any, any>) => {
let response: Response | undefined;
// https://github.com/cloudflare/wrangler2/issues/117
context.request.headers.delete("if-none-match");
try {
response = await (context.env as any).ASSETS.fetch(
context.request.url,
context.request.clone(),
);
response =
response && response.status >= 200 && response.status < 400
? new Response(response.body, response)
: undefined;
} catch {}
if (!response) {
response = await handleRequest(context);
}
return response;
};
// This function adds caching with stale-while-revalidate
let cachedHandleFetch = async (context: EventContext<Env, any, any>) => {
// Open cache
// https://developers.cloudflare.com/workers/runtime-apis/cache/
const url = new URL(context.request.url);
const useCache =
url.hostname !== "localhost" && context.request.method === "GET";
const cacheKey = new Request(url.href, context.request);
const cache = useCache
? await caches.open(`custom:${build.assets.version}`)
: undefined;
// Save to cache function
let saveToCache = (response: Response) => {
if (response.ok && cache && response.headers.has("Cache-Control")) {
// Store the fetched response as cacheKey
// Use waitUntil so you can return the response without blocking on
// writing to cache
response.headers.set("X-SWR-Date", new Date().toUTCString());
// Hack: Extend s-maxage with stale-while-revalidate time
const cacheControl = response.headers.get("Cache-Control");
let matches = cacheControl?.match(/s-maxage=(\d+)/);
const sMaxage = matches ? parseInt(matches[1], 10) : 0;
matches = cacheControl?.match(/stale-while-revalidate=(\d+)/);
const staleWhileRevalidate = matches ? parseInt(matches[1], 10) : 0;
if (sMaxage && staleWhileRevalidate) {
response.headers.set(
"Cache-Control",
cacheControl?.replace(
/s-maxage=(\d+)/,
`s-maxage=${sMaxage + staleWhileRevalidate}`,
) ?? "",
);
}
context.waitUntil(cache.put(cacheKey, response.clone()));
}
};
const cachedResponse = await cache?.match(cacheKey);
if (cachedResponse) {
// stale-while-revalidate
const responseDate = cachedResponse.headers.get("X-SWR-Date");
const cacheControl = cachedResponse.headers.get("Cache-Control");
const date = responseDate ? new Date(responseDate) : null;
const secondsSinceDate = date
? (new Date().getTime() - date.getTime()) / 1000
: 0;
let matches = cacheControl?.match(/s-maxage=(\d+)/);
const sMaxage = matches ? parseInt(matches[1], 10) : 0;
matches = cacheControl?.match(/stale-while-revalidate=(\d+)/);
const staleWhileRevalidate = matches ? parseInt(matches[1], 10) : 0;
if (
sMaxage &&
staleWhileRevalidate &&
secondsSinceDate &&
sMaxage - staleWhileRevalidate < secondsSinceDate
) {
context.waitUntil(handleFetch(context).then(saveToCache));
}
return cachedResponse;
}
const response = await handleFetch(context);
saveToCache(response);
return response;
};
return async (context: EventContext<Env, any, any>) => {
try {
return await cachedHandleFetch(context);
} catch (e) {
if (process.env.NODE_ENV === "development" && e instanceof Error) {
console.error(e);
return new Response(e.message || e.toString(), {
status: 500,
});
}
return new Response("Internal Error", {
status: 500,
});
}
};
}