-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
87 lines (75 loc) · 2.62 KB
/
server.js
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
import { getAssetFromKV } from "@cloudflare/kv-asset-handler";
import { createRequestHandler } from "@remix-run/cloudflare";
import __STATIC_CONTENT_MANIFEST from "__STATIC_CONTENT_MANIFEST";
import path from 'node:path';
import * as remixBuild from "./build/server";
const MANIFEST = JSON.parse(__STATIC_CONTENT_MANIFEST);
const handleRemixRequest = createRequestHandler(remixBuild);
export default {
async fetch(request, env, ctx) {
try {
const url = new URL(request.url);
const ext = path.extname(url.pathname)
// console.log('Detected extension', ext)
// console.log(url.pathname.endsWith('/'), url.pathname !== '/', ext.length === 0)
if (url.pathname.endsWith('/') && url.pathname !== '/' && ext.length === 0) {
// console.log('Redirecting due to extensionless slash')
const isDataRequest = url.searchParams.has('_data')
if (isDataRequest) {
url.searchParams.delete('_data')
const redirectUrl = url.origin + url.pathname.substring(0, url.pathname.length - 1)
return new Response(undefined, {
headers: {
'x-remix-redirect': redirectUrl
},
status: 204
})
}
// console.log('Not trailing slash URL')
const redirectUrl = url.origin + url.pathname.substring(0, url.pathname.length - 1)
return Response.redirect(redirectUrl, 301)
}
const ttl = url.pathname.startsWith("/build/")
? 60 * 60 * 24 * 365 // 1 year
: 60 * 5; // 5 minutes
return await getAssetFromKV(
{
request,
waitUntil: ctx.waitUntil.bind(ctx),
},
{
ASSET_NAMESPACE: env.__STATIC_CONTENT,
ASSET_MANIFEST: MANIFEST,
cacheControl: {
browserTTL: ttl,
edgeTTL: ttl,
},
}
);
} catch (error) {
// No-op
}
console.log('CF env', env)
try {
const loadContext = {
cloudflare: {
// This object matches the return value from Wrangler's
// `getPlatformProxy` used during development via Remix's
// `cloudflareDevProxyVitePlugin`:
// https://developers.cloudflare.com/workers/wrangler/api/#getplatformproxy
cf: request.cf,
ctx: {
waitUntil: ctx.waitUntil,
passThroughOnException: ctx.passThroughOnException,
},
caches,
env,
},
};
return await handleRemixRequest(request, loadContext);
} catch (error) {
console.log(error);
return new Response("An unexpected error occurred", { status: 500 });
}
},
};