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

Feat proposal enable fetching external resources using native fetch API in Node.js version 18 later #935

Merged
merged 11 commits into from
Sep 26, 2022
5 changes: 5 additions & 0 deletions .changeset/young-guests-tap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'wmr': minor
---

Add fetching external resources using native fetch API when Node.js version 18 later
27 changes: 26 additions & 1 deletion packages/wmr/src/lib/prerender.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,33 @@ async function workerCode({ cwd, out, publicPath, customRoutes }) {
let head = { lang: '', title: '', elements: new Set() };
globalThis.wmr = { ssr: { head } };

// Define `_fetch` to avoid infinite loop in globalThis.fetch.
let _fetch;
const nodeVersion = process.version;
const isNodeVersionOver18 = Number(nodeVersion.split('.')[0].slice(1)) >= 18;
// Native fetch is only available in Node.js 18 later.
if (isNodeVersionOver18) {
_fetch = fetch;
}
// @ts-ignore
delete globalThis.fetch;
// @ts-ignore
globalThis.fetch = async url => {
globalThis.fetch = async (url, options) => {
takurinton marked this conversation as resolved.
Show resolved Hide resolved
const pattern = /^https?:\/\/[\w/:%#\\$&\\?\\(\\)~\\.=\\+\\-]+/;
takurinton marked this conversation as resolved.
Show resolved Hide resolved
const isExternalUrl = pattern.test(String(url));
if (isExternalUrl) {
// Use native fetch if Node.js version 18 later when `prerender`
if (isNodeVersionOver18) {
return _fetch(url, options).then(res => ({
text: () => res.text(),
json: () => res.text().then(JSON.parse)
}));
takurinton marked this conversation as resolved.
Show resolved Hide resolved
}
throw new Error(
`External links are not supported in your current Node.js version ${nodeVersion} (try v18 or later).\n ${url}`
);
}

const text = () => fs.readFile(`${out}/${String(url).replace(/^\//, '')}`, 'utf-8');
return { text, json: () => text().then(JSON.parse) };
};
Expand Down
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>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export async function prerender() {
const html = await fetch('https://preactjs.com').then(res => res.text());
return { html, links: ['/'] };
}
25 changes: 25 additions & 0 deletions packages/wmr/test/production.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,31 @@ describe('production', () => {
const index = await fs.readFile(indexHtml, 'utf8');
expect(index).toMatch(/# hello world/);
});

it('should not support fetching resources from external link prerender', async () => {
await loadFixture('prerender-external-resource-fetch', env);
instance = await runWmr(env.tmp.path, 'build', '--prerender');
const code = await instance.done;

const nodeVersion = process.version;
const isNodeVersionOver18 = Number(nodeVersion.split('.')[0].slice(1)) >= 18;
if (isNodeVersionOver18) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to run tests on multiple versions?

Copy link
Contributor Author

@takurinton takurinton Sep 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, and two versions, 12 and 14, are running now. This felt like a no problem in the future.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm also considering breaking down this test.

Instead of branching using if in one test, write a test for Node.js version 17 or less and a test for Node.js version 18 or more, and skip if the version is not applicable. I think it is also possible.
This part is one of the things I want to discuss in this PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of branching using if in one test, write a test for Node.js version 17 or less and a test for Node.js version 18 or more, and skip if the version is not applicable.

I like this idea. There's slightly different behavior between the two that (ideally) we'd test.

We can definitely alter the CI config to cover newer versions of Node.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I completely forgot about this.
Last time I called fetch without the leading slash here, but this test does not work with the regex changes you reviewed.
https://github.com/preactjs/wmr/blob/main/packages/wmr/test/fixtures/prerender-resource-fetch/index.js#L2

I think to need to modify the test here as well. I think the comments here will be helpful for that.
#935 (comment)

Copy link
Contributor Author

@takurinton takurinton Sep 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, no correction of above comment was necessary.

// when workflow of wmr runtime uses Node.js 18
// Until then check locally.
expect(code).toBe(0);
const indexHtml = path.join(env.tmp.path, 'dist', 'index.html');
const index = await fs.readFile(indexHtml, 'utf8');
expect(index).toMatch(/Preact/);
} else {
// Throw error when Node.js version under 17
await withLog(instance.output, async () => {
expect(code).toBe(1);
expect(instance.output.join('\n')).toMatch(
/Error: External links are not supported in your current Node.js version/
);
});
}
});
});

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