Skip to content

Commit

Permalink
fix: serve files in _app from function, if not already handled (#11593)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
Rich-Harris and Rich-Harris authored Jan 11, 2024
1 parent cc71b3b commit fc9f654
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/old-masks-thank.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/adapter-cloudflare': patch
---

fix: serve static files in `_app` from function, if not already handled
5 changes: 5 additions & 0 deletions .changeset/rude-countries-sing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/adapter-cloudflare-workers': patch
---

fix: serve static files in `_app` from function, if not already handled
14 changes: 11 additions & 3 deletions packages/adapter-cloudflare-workers/files/entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ const static_asset_manifest = JSON.parse(static_asset_manifest_json);

const server = new Server(manifest);

const app_path = `/${manifest.appPath}/`;
const app_path = `/${manifest.appPath}`;

const immutable = `${app_path}/immutable/`;
const version_file = `${app_path}/version.json`;

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

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

Expand Down Expand Up @@ -59,7 +62,12 @@ export default {

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

if (is_static_asset || prerendered.has(pathname)) {
if (
is_static_asset ||
prerendered.has(pathname) ||
pathname === version_file ||
pathname.startsWith(immutable)
) {
return get_asset_from_kv(req, env, context, (request, options) => {
if (prerendered.has(pathname)) {
url.pathname = '/' + prerendered.get(pathname).file;
Expand Down
3 changes: 2 additions & 1 deletion packages/adapter-cloudflare/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ export default function (options = {}) {
writeFileSync(
`${tmp}/manifest.js`,
`export const manifest = ${builder.generateManifest({ relativePath })};\n\n` +
`export const prerendered = new Set(${JSON.stringify(builder.prerendered.paths)});\n`
`export const prerendered = new Set(${JSON.stringify(builder.prerendered.paths)});\n\n` +
`export const app_path = ${JSON.stringify(builder.getAppPath())};\n`
);

writeFileSync(
Expand Down
1 change: 1 addition & 0 deletions packages/adapter-cloudflare/placeholders.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ declare module 'MANIFEST' {

export const manifest: SSRManifest;
export const prerendered: Set<string>;
export const app_path: string;
}
12 changes: 10 additions & 2 deletions packages/adapter-cloudflare/src/worker.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { Server } from 'SERVER';
import { manifest, prerendered } from 'MANIFEST';
import { manifest, prerendered, app_path } from 'MANIFEST';
import * as Cache from 'worktop/cfw.cache';

const server = new Server(manifest);

const immutable = `/${app_path}/immutable/`;
const version_file = `/${app_path}/version.json`;

/** @type {import('worktop/cfw').Module.Worker<{ ASSETS: import('worktop/cfw.durable').Durable.Object }>} */
const worker = {
async fetch(req, env, context) {
Expand Down Expand Up @@ -33,7 +36,12 @@ const worker = {

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

if (is_static_asset || prerendered.has(pathname)) {
if (
is_static_asset ||
prerendered.has(pathname) ||
pathname === version_file ||
pathname.startsWith(immutable)
) {
res = await env.ASSETS.fetch(req);
} else if (location && prerendered.has(location)) {
if (search) location += search;
Expand Down

0 comments on commit fc9f654

Please sign in to comment.