Skip to content

Commit 316943f

Browse files
lukeedRich-Harris
andauthored
chore(cloudflare): optimize Cache usage; strict TS source (#4453)
* chore(cloudflare): optimize Cache usage; strict TS source * chore: add changeset * skip lib check for conflicts * bundle worktop (#4473) * bundle worktop * build on prepublishOnly * Update packages/adapter-cloudflare/tsconfig.json * update builder file paths Co-authored-by: Luke Edwards <[email protected]> * convert to JS (#4474) * Update .changeset/empty-falcons-run.md Co-authored-by: Rich Harris <[email protected]>
1 parent a11bae8 commit 316943f

File tree

7 files changed

+96
-93
lines changed

7 files changed

+96
-93
lines changed

.changeset/empty-falcons-run.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/adapter-cloudflare': patch
3+
---
4+
5+
Check for Cache match sooner; use `worktop` for types & Cache operations
+1-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
.DS_Store
2-
node_modules
3-
target
1+
/files

packages/adapter-cloudflare/files/worker.js

-85
This file was deleted.

packages/adapter-cloudflare/package.json

+6-3
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,16 @@
2323
],
2424
"main": "index.js",
2525
"scripts": {
26+
"build": "esbuild src/worker.js --bundle --outfile=files/worker.js --external:SERVER --external:MANIFEST --format=esm",
2627
"lint": "eslint --ignore-path .gitignore \"**/*.{ts,js,svelte}\" && npm run check-format",
2728
"format": "npm run check-format -- --write",
28-
"check": "tsc",
29-
"check-format": "prettier --check . --config ../../.prettierrc --ignore-path .gitignore"
29+
"check": "tsc --skipLibCheck",
30+
"check-format": "prettier --check . --config ../../.prettierrc --ignore-path .gitignore",
31+
"prepublishOnly": "npm run build"
3032
},
3133
"dependencies": {
32-
"esbuild": "^0.14.21"
34+
"esbuild": "^0.14.21",
35+
"worktop": "0.8.0-next.12"
3336
},
3437
"devDependencies": {
3538
"@types/ws": "^8.5.3",
+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { Server } from 'SERVER';
2+
import { manifest, prerendered } from 'MANIFEST';
3+
import * as Cache from 'worktop/cfw.cache';
4+
5+
const server = new Server(manifest);
6+
7+
const prefix = `/${manifest.appDir}/`;
8+
9+
/** @type {import('worktop/cfw').Module.Worker<{ ASSETS: import('worktop/cfw.durable').Durable.Object }>} */
10+
const worker = {
11+
async fetch(req, env, context) {
12+
try {
13+
let res = await Cache.lookup(req);
14+
if (res) return res;
15+
16+
let { pathname } = new URL(req.url);
17+
18+
// static assets
19+
if (pathname.startsWith(prefix)) {
20+
res = await env.ASSETS.fetch(req);
21+
22+
res = new Response(res.body, {
23+
headers: {
24+
// include original cache headers, minus cache-control which
25+
// is overridden, and etag which is no longer useful
26+
'cache-control': 'public, immutable, max-age=31536000',
27+
'content-type': res.headers.get('content-type'),
28+
'x-robots-tag': 'noindex'
29+
}
30+
});
31+
} else {
32+
// prerendered pages and index.html files
33+
pathname = pathname.replace(/\/$/, '') || '/';
34+
35+
let file = pathname.substring(1);
36+
37+
try {
38+
file = decodeURIComponent(file);
39+
} catch (err) {
40+
// ignore
41+
}
42+
43+
if (
44+
manifest.assets.has(file) ||
45+
manifest.assets.has(file + '/index.html') ||
46+
prerendered.has(pathname)
47+
) {
48+
res = await env.ASSETS.fetch(req);
49+
} else {
50+
// dynamically-generated pages
51+
res = await server.respond(req, {
52+
platform: { env, context },
53+
getClientAddress() {
54+
return req.headers.get('cf-connecting-ip');
55+
}
56+
});
57+
}
58+
}
59+
60+
// Writes to Cache only if allowed
61+
return Cache.save(req, res, context);
62+
} catch (e) {
63+
return new Response('Error rendering route: ' + (e.message || e.toString()), { status: 500 });
64+
}
65+
}
66+
};
67+
68+
export default worker;

packages/adapter-cloudflare/tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@
1111
"@sveltejs/kit": ["../kit/types/index"]
1212
}
1313
},
14-
"include": ["**/*.js", "ambient.d.ts"]
14+
"include": ["index.js", "ambient.d.ts", "src/worker.ts"]
1515
}

pnpm-lock.yaml

+15-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)