Skip to content

Commit

Permalink
Fix adapter-cloudflare-workers prerendering bug (#4626)
Browse files Browse the repository at this point in the history
* feat: allow adapter-cloudflare-workers to take esbuild options

* chore: add changeset

* fix: prerendered page resolution

* chore: update changeset

* chore: run prettier

* update

* Update .changeset/chilly-cars-battle.md

Co-authored-by: Joe Harlow <[email protected]>
  • Loading branch information
Rich-Harris and f5io authored Apr 15, 2022
1 parent e5f8065 commit bc905f2
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 9 deletions.
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

0 comments on commit bc905f2

Please sign in to comment.