Skip to content

Commit fc9f654

Browse files
authored
fix: serve files in _app from function, if not already handled (#11593)
* fix: serve files in _app from function, if not already handled * Update .changeset/old-masks-thank.md * fix * same but for cloudflare-workers * changeset --------- Co-authored-by: Rich Harris <rich.harris@vercel.com>
1 parent cc71b3b commit fc9f654

File tree

6 files changed

+34
-6
lines changed

6 files changed

+34
-6
lines changed

.changeset/old-masks-thank.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: serve static files in `_app` from function, if not already handled

.changeset/rude-countries-sing.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/adapter-cloudflare-workers': patch
3+
---
4+
5+
fix: serve static files in `_app` from function, if not already handled

packages/adapter-cloudflare-workers/files/entry.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ const static_asset_manifest = JSON.parse(static_asset_manifest_json);
66

77
const server = new Server(manifest);
88

9-
const app_path = `/${manifest.appPath}/`;
9+
const app_path = `/${manifest.appPath}`;
10+
11+
const immutable = `${app_path}/immutable/`;
12+
const version_file = `${app_path}/version.json`;
1013

1114
export default {
1215
/**
@@ -25,7 +28,7 @@ export default {
2528
const res = await get_asset_from_kv(req, env, context);
2629
if (is_error(res.status)) return res;
2730

28-
const cache_control = url.pathname.startsWith(app_path + 'immutable/')
31+
const cache_control = url.pathname.startsWith(immutable)
2932
? 'public, immutable, max-age=31536000'
3033
: 'no-cache';
3134

@@ -59,7 +62,12 @@ export default {
5962

6063
let location = pathname.at(-1) === '/' ? stripped_pathname : pathname + '/';
6164

62-
if (is_static_asset || prerendered.has(pathname)) {
65+
if (
66+
is_static_asset ||
67+
prerendered.has(pathname) ||
68+
pathname === version_file ||
69+
pathname.startsWith(immutable)
70+
) {
6371
return get_asset_from_kv(req, env, context, (request, options) => {
6472
if (prerendered.has(pathname)) {
6573
url.pathname = '/' + prerendered.get(pathname).file;

packages/adapter-cloudflare/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ export default function (options = {}) {
2828
writeFileSync(
2929
`${tmp}/manifest.js`,
3030
`export const manifest = ${builder.generateManifest({ relativePath })};\n\n` +
31-
`export const prerendered = new Set(${JSON.stringify(builder.prerendered.paths)});\n`
31+
`export const prerendered = new Set(${JSON.stringify(builder.prerendered.paths)});\n\n` +
32+
`export const app_path = ${JSON.stringify(builder.getAppPath())};\n`
3233
);
3334

3435
writeFileSync(

packages/adapter-cloudflare/placeholders.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ declare module 'MANIFEST' {
77

88
export const manifest: SSRManifest;
99
export const prerendered: Set<string>;
10+
export const app_path: string;
1011
}

packages/adapter-cloudflare/src/worker.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import { Server } from 'SERVER';
2-
import { manifest, prerendered } from 'MANIFEST';
2+
import { manifest, prerendered, app_path } from 'MANIFEST';
33
import * as Cache from 'worktop/cfw.cache';
44

55
const server = new Server(manifest);
66

7+
const immutable = `/${app_path}/immutable/`;
8+
const version_file = `/${app_path}/version.json`;
9+
710
/** @type {import('worktop/cfw').Module.Worker<{ ASSETS: import('worktop/cfw.durable').Durable.Object }>} */
811
const worker = {
912
async fetch(req, env, context) {
@@ -33,7 +36,12 @@ const worker = {
3336

3437
let location = pathname.at(-1) === '/' ? stripped_pathname : pathname + '/';
3538

36-
if (is_static_asset || prerendered.has(pathname)) {
39+
if (
40+
is_static_asset ||
41+
prerendered.has(pathname) ||
42+
pathname === version_file ||
43+
pathname.startsWith(immutable)
44+
) {
3745
res = await env.ASSETS.fetch(req);
3846
} else if (location && prerendered.has(location)) {
3947
if (search) location += search;

0 commit comments

Comments
 (0)