Skip to content

Commit

Permalink
chore(cloudflare): optimize Cache usage; strict TS source (#4453)
Browse files Browse the repository at this point in the history
* 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 <luke.edwards05@gmail.com>

* convert to JS (#4474)

* Update .changeset/empty-falcons-run.md

Co-authored-by: Rich Harris <richard.a.harris@gmail.com>
  • Loading branch information
lukeed and Rich-Harris authored Mar 31, 2022
1 parent a11bae8 commit 316943f
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 93 deletions.
5 changes: 5 additions & 0 deletions .changeset/empty-falcons-run.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/adapter-cloudflare': patch
---

Check for Cache match sooner; use `worktop` for types & Cache operations
4 changes: 1 addition & 3 deletions packages/adapter-cloudflare/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
.DS_Store
node_modules
target
/files
85 changes: 0 additions & 85 deletions packages/adapter-cloudflare/files/worker.js

This file was deleted.

9 changes: 6 additions & 3 deletions packages/adapter-cloudflare/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,16 @@
],
"main": "index.js",
"scripts": {
"build": "esbuild src/worker.js --bundle --outfile=files/worker.js --external:SERVER --external:MANIFEST --format=esm",
"lint": "eslint --ignore-path .gitignore \"**/*.{ts,js,svelte}\" && npm run check-format",
"format": "npm run check-format -- --write",
"check": "tsc",
"check-format": "prettier --check . --config ../../.prettierrc --ignore-path .gitignore"
"check": "tsc --skipLibCheck",
"check-format": "prettier --check . --config ../../.prettierrc --ignore-path .gitignore",
"prepublishOnly": "npm run build"
},
"dependencies": {
"esbuild": "^0.14.21"
"esbuild": "^0.14.21",
"worktop": "0.8.0-next.12"
},
"devDependencies": {
"@types/ws": "^8.5.3",
Expand Down
68 changes: 68 additions & 0 deletions packages/adapter-cloudflare/src/worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { Server } from 'SERVER';
import { manifest, prerendered } from 'MANIFEST';
import * as Cache from 'worktop/cfw.cache';

const server = new Server(manifest);

const prefix = `/${manifest.appDir}/`;

/** @type {import('worktop/cfw').Module.Worker<{ ASSETS: import('worktop/cfw.durable').Durable.Object }>} */
const worker = {
async fetch(req, env, context) {
try {
let res = await Cache.lookup(req);
if (res) return res;

let { pathname } = new URL(req.url);

// static assets
if (pathname.startsWith(prefix)) {
res = await env.ASSETS.fetch(req);

res = new Response(res.body, {
headers: {
// include original cache headers, minus cache-control which
// is overridden, and etag which is no longer useful
'cache-control': 'public, immutable, max-age=31536000',
'content-type': res.headers.get('content-type'),
'x-robots-tag': 'noindex'
}
});
} else {
// prerendered pages and index.html files
pathname = pathname.replace(/\/$/, '') || '/';

let file = pathname.substring(1);

try {
file = decodeURIComponent(file);
} catch (err) {
// ignore
}

if (
manifest.assets.has(file) ||
manifest.assets.has(file + '/index.html') ||
prerendered.has(pathname)
) {
res = await env.ASSETS.fetch(req);
} else {
// dynamically-generated pages
res = await server.respond(req, {
platform: { env, context },
getClientAddress() {
return req.headers.get('cf-connecting-ip');
}
});
}
}

// Writes to Cache only if allowed
return Cache.save(req, res, context);
} catch (e) {
return new Response('Error rendering route: ' + (e.message || e.toString()), { status: 500 });
}
}
};

export default worker;
2 changes: 1 addition & 1 deletion packages/adapter-cloudflare/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
"@sveltejs/kit": ["../kit/types/index"]
}
},
"include": ["**/*.js", "ambient.d.ts"]
"include": ["index.js", "ambient.d.ts", "src/worker.ts"]
}
16 changes: 15 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 316943f

Please sign in to comment.