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

Add globalThis.fetch #934

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/nice-chairs-mix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'wmr': minor
---

Adds resource fetch shim for prererendering
6 changes: 6 additions & 0 deletions packages/wmr/src/lib/prerender.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# hello world
10 changes: 10 additions & 0 deletions packages/wmr/test/fixtures/prerender-resource-fetch/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf8" />
<title>default title</title>
</head>
<body>
<script src="./index.js" type="module"></script>
</body>
</html>
4 changes: 4 additions & 0 deletions packages/wmr/test/fixtures/prerender-resource-fetch/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export async function prerender() {
const md = await fetch('content.md').then(res => res.text());
return { html: md, links: ['/'] };
}
11 changes: 11 additions & 0 deletions packages/wmr/test/production.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -878,6 +878,17 @@ describe('production', () => {
expect(instance.output.join('\n')).toMatch(/The following error was thrown during prerendering/i);
});
});

it('should support fetching resources from disk during prerender', async () => {
await loadFixture('prerender-resource-fetch', env);
instance = await runWmr(env.tmp.path, 'build', '--prerender');
const code = await instance.done;
expect(code).toBe(0);

const indexHtml = path.join(env.tmp.path, 'dist', 'index.html');
const index = await fs.readFile(indexHtml, 'utf8');
expect(index).toMatch(/# hello world/);
});
});

describe('Code Splitting', () => {
Expand Down