Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(cloudflare): optimize Cache usage; strict TS source #4453

Merged
merged 6 commits into from
Mar 31, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
---

Convert entry to TypeScript; check for Cache match sooner; use `worktop` for types & Cache operations
Rich-Harris marked this conversation as resolved.
Show resolved Hide resolved
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.ts --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": {
"typescript": "^4.6.2"
Expand Down
74 changes: 74 additions & 0 deletions packages/adapter-cloudflare/src/worker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { Server } from 'SERVER';
import { manifest, prerendered } from 'MANIFEST';
import * as Cache from 'worktop/cfw.cache';

import type { Module, Bindings } from 'worktop/cfw';
import type { Durable } from 'worktop/cfw.durable';

interface Environment extends Bindings {
ASSETS: Durable.Object;
}

const server = new Server(manifest);

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

const worker: Module.Worker<Environment> = {
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.