diff --git a/.changeset/nice-chairs-mix.md b/.changeset/nice-chairs-mix.md new file mode 100644 index 00000000..ebf154ca --- /dev/null +++ b/.changeset/nice-chairs-mix.md @@ -0,0 +1,5 @@ +--- +'wmr': minor +--- + +Adds resource fetch shim for prererendering diff --git a/packages/wmr/src/lib/prerender.js b/packages/wmr/src/lib/prerender.js index a5283b8b..9f5297ca 100644 --- a/packages/wmr/src/lib/prerender.js +++ b/packages/wmr/src/lib/prerender.js @@ -96,6 +96,12 @@ async function workerCode({ cwd, out, publicPath, customRoutes }) { let head = { lang: '', title: '', elements: new Set() }; globalThis.wmr = { ssr: { head } }; + // @ts-ignore + globalThis.fetch = async url => { + const text = () => fs.readFile(`${out}/${String(url).replace(/^\//, '')}`, 'utf-8'); + return { text, json: () => text().then(JSON.parse) }; + }; + // Prevent Rollup from transforming `import()` here. const $import = new Function('s', 'return import(s)'); const m = await $import('file:///' + script); diff --git a/packages/wmr/test/fixtures/prerender-resource-fetch/content.md b/packages/wmr/test/fixtures/prerender-resource-fetch/content.md new file mode 100644 index 00000000..2be7c65a --- /dev/null +++ b/packages/wmr/test/fixtures/prerender-resource-fetch/content.md @@ -0,0 +1 @@ +# hello world diff --git a/packages/wmr/test/fixtures/prerender-resource-fetch/index.html b/packages/wmr/test/fixtures/prerender-resource-fetch/index.html new file mode 100644 index 00000000..d2c9fb7d --- /dev/null +++ b/packages/wmr/test/fixtures/prerender-resource-fetch/index.html @@ -0,0 +1,10 @@ + + +
+ +