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

fix: adapter-cloudflare-workers prerendering bug [w/ reproduction] #4467

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions .changeset/chilly-cars-battle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@sveltejs/adapter-cloudflare-workers': patch
---

- Support esbuild options for @sveltejs/adapter-cloudflare-workers
- Fix an issue related to prerendered pages incorrectly resolving in @sveltejs/adapter-cloudflare-workers
2 changes: 1 addition & 1 deletion packages/adapter-cloudflare-workers/ambient.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ declare module 'MANIFEST' {
import { SSRManifest } from '@sveltejs/kit';

export const manifest: SSRManifest;
export const prerendered: Set<string>;
export const prerendered: Map<string, { file: string }>;
}

declare module '__STATIC_CONTENT_MANIFEST' {
Expand Down
13 changes: 10 additions & 3 deletions packages/adapter-cloudflare-workers/files/entry.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Server } from 'SERVER';
import { manifest, prerendered } from 'MANIFEST';
import { getAssetFromKV } from '@cloudflare/kv-asset-handler';
import { getAssetFromKV, mapRequestToAsset } from '@cloudflare/kv-asset-handler';
import static_asset_manifest_json from '__STATIC_CONTENT_MANIFEST';
const static_asset_manifest = JSON.parse(static_asset_manifest_json);

Expand Down Expand Up @@ -83,8 +83,15 @@ async function get_asset_from_kv(req, env, context) {
},
{
ASSET_NAMESPACE: env.__STATIC_CONTENT,
ASSET_MANIFEST: static_asset_manifest
}
ASSET_MANIFEST: static_asset_manifest,
mapRequestToAsset(request, options) {
if (prerendered.has(pathname || '/')) {
url.pathname = '/' + prerendered.get(pathname || '/').file;
return new Request(url.toString(), request);
}
return mapRequestToAsset(request, options);
}
}
);
} catch (e) {
const status = is_error(e.status) ? e.status : 500;
Expand Down
4 changes: 3 additions & 1 deletion packages/adapter-cloudflare-workers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ export default function (options = {}) {
`${tmp}/manifest.js`,
`export const manifest = ${builder.generateManifest({
relativePath
})};\n\nexport const prerendered = new Set(${JSON.stringify(builder.prerendered.paths)});\n`
})};\n\nexport const prerendered = new Map(${JSON.stringify(
Array.from(builder.prerendered.pages.entries())
)});\n`
);

await esbuild.build({
Expand Down