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 #4626

Merged
merged 8 commits into from
Apr 15, 2022
Merged
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
5 changes: 5 additions & 0 deletions .changeset/chilly-cars-battle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/adapter-cloudflare-workers': patch
---

- 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
21 changes: 14 additions & 7 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 All @@ -21,9 +21,8 @@ export default {
if (url.pathname.startsWith(prefix)) {
/** @type {Response} */
const res = await get_asset_from_kv(req, env, context);
if (is_error(res.status)) {
return res;
}
if (is_error(res.status)) return res;

return new Response(res.body, {
headers: {
// include original cache headers, minus cache-control which
Expand All @@ -50,7 +49,14 @@ export default {
manifest.assets.has(file + '/index.html') ||
prerendered.has(pathname || '/')
) {
return get_asset_from_kv(req, env, context);
return get_asset_from_kv(req, env, context, (request, options) => {
if (prerendered.has(pathname || '/')) {
url.pathname = '/' + prerendered.get(pathname || '/').file;
return new Request(url.toString(), request);
}

return mapRequestToAsset(request, options);
});
}

// dynamically-generated pages
Expand All @@ -72,7 +78,7 @@ export default {
* @param {any} env
* @param {any} context
*/
async function get_asset_from_kv(req, env, context) {
async function get_asset_from_kv(req, env, context, map = mapRequestToAsset) {
try {
return await getAssetFromKV(
{
Expand All @@ -83,7 +89,8 @@ 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: map
}
);
} catch (e) {
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