Skip to content

Commit fdcfb87

Browse files
authored
remove cloudflare worker asset check (#9040)
1 parent a7ad5af commit fdcfb87

File tree

3 files changed

+38
-54
lines changed

3 files changed

+38
-54
lines changed

.changeset/honest-hairs-try.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/adapter-cloudflare': patch
3+
---
4+
5+
fix: remove redundant cloudflare worker static asset serving

packages/adapter-cloudflare/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,12 @@ function get_routes_json(builder, assets) {
118118
function generate_headers(app_dir) {
119119
return `
120120
# === START AUTOGENERATED SVELTE IMMUTABLE HEADERS ===
121+
/${app_dir}/*
122+
X-Robots-Tag: noindex
123+
Cache-Control: no-cache
121124
/${app_dir}/immutable/*
125+
! Cache-Control
122126
Cache-Control: public, immutable, max-age=31536000
123-
X-Robots-Tag: noindex
124127
# === END AUTOGENERATED SVELTE IMMUTABLE HEADERS ===
125128
`.trimEnd();
126129
}

packages/adapter-cloudflare/src/worker.js

Lines changed: 29 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import * as Cache from 'worktop/cfw.cache';
44

55
const server = new Server(manifest);
66

7-
const app_path = `/${manifest.appPath}/`;
8-
97
/** @type {import('worktop/cfw').Module.Worker<{ ASSETS: import('worktop/cfw.durable').Durable.Object }>} */
108
const worker = {
119
async fetch(req, env, context) {
@@ -17,64 +15,42 @@ const worker = {
1715
if (res) return res;
1816

1917
let { pathname } = new URL(req.url);
18+
try {
19+
pathname = decodeURIComponent(pathname);
20+
} catch {
21+
// ignore invalid URI
22+
}
2023

21-
// generated files
22-
if (pathname.startsWith(app_path)) {
23-
res = await env.ASSETS.fetch(req);
24-
if (!res.ok) return res;
24+
const stripped_pathname = pathname.replace(/\/$/, '');
2525

26-
const cache_control = pathname.startsWith(app_path + 'immutable/')
27-
? 'public, immutable, max-age=31536000'
28-
: 'no-cache';
26+
// prerendered pages and /static files
27+
let is_static_asset = false;
28+
const filename = stripped_pathname.substring(1);
29+
if (filename) {
30+
is_static_asset =
31+
manifest.assets.has(filename) || manifest.assets.has(filename + '/index.html');
32+
}
2933

30-
res = new Response(res.body, {
34+
const location = pathname.at(-1) === '/' ? stripped_pathname : pathname + '/';
35+
36+
if (is_static_asset || prerendered.has(pathname)) {
37+
res = await env.ASSETS.fetch(req);
38+
} else if (location && prerendered.has(location)) {
39+
res = new Response('', {
40+
status: 308,
3141
headers: {
32-
// include original headers, minus cache-control which
33-
// is overridden, and etag which is no longer useful
34-
'cache-control': cache_control,
35-
'content-type': res.headers.get('content-type'),
36-
'x-robots-tag': 'noindex'
42+
location
3743
}
3844
});
3945
} else {
40-
// prerendered pages and /static files
41-
42-
try {
43-
pathname = decodeURIComponent(pathname);
44-
} catch {
45-
// ignore invalid URI
46-
}
47-
48-
const stripped_pathname = pathname.replace(/\/$/, '');
49-
50-
let is_static_asset = false;
51-
const filename = stripped_pathname.substring(1);
52-
if (filename) {
53-
is_static_asset =
54-
manifest.assets.has(filename) || manifest.assets.has(filename + '/index.html');
55-
}
56-
57-
const counterpart_route = pathname.at(-1) === '/' ? stripped_pathname : pathname + '/';
58-
59-
if (is_static_asset || prerendered.has(pathname)) {
60-
res = await env.ASSETS.fetch(req);
61-
} else if (counterpart_route && prerendered.has(counterpart_route)) {
62-
res = new Response('', {
63-
status: 308,
64-
headers: {
65-
location: counterpart_route
66-
}
67-
});
68-
} else {
69-
// dynamically-generated pages
70-
res = await server.respond(req, {
71-
// @ts-ignore
72-
platform: { env, context, caches },
73-
getClientAddress() {
74-
return req.headers.get('cf-connecting-ip');
75-
}
76-
});
77-
}
46+
// dynamically-generated pages
47+
res = await server.respond(req, {
48+
// @ts-ignore
49+
platform: { env, context, caches },
50+
getClientAddress() {
51+
return req.headers.get('cf-connecting-ip');
52+
}
53+
});
7854
}
7955

8056
// Writes to Cache only if allowed & specified

0 commit comments

Comments
 (0)