From 0fd72d76cb9c2ea7583d45e279df194db246ccf5 Mon Sep 17 00:00:00 2001 From: Ryan Christian Date: Sun, 23 Jan 2022 21:51:02 -0600 Subject: [PATCH 1/3] fix: First, not last, non-external script in HTML should have prerender() --- packages/wmr/src/lib/prerender.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/wmr/src/lib/prerender.js b/packages/wmr/src/lib/prerender.js index 5d5699aa..8544a314 100644 --- a/packages/wmr/src/lib/prerender.js +++ b/packages/wmr/src/lib/prerender.js @@ -76,7 +76,7 @@ async function workerCode({ cwd, out, publicPath, customRoutes }) { const SCRIPT_TAG = /]*?)?\s+src=(['"]?)([^>]*?)\1(?:\s[^>]*?)?>/g; let match; - while ((match = SCRIPT_TAG.exec(tpl))) { + while ((match = SCRIPT_TAG.exec(tpl)) && !script) { // Ignore external urls if (!match || /^(?:https?|file|data)/.test(match[2])) continue; From f75af7cc52460548c077b1f8a56d778962856107 Mon Sep 17 00:00:00 2001 From: Ryan Christian Date: Sun, 23 Jan 2022 21:55:28 -0600 Subject: [PATCH 2/3] docs: Adding changeset --- .changeset/pink-turtles-switch.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/pink-turtles-switch.md diff --git a/.changeset/pink-turtles-switch.md b/.changeset/pink-turtles-switch.md new file mode 100644 index 00000000..9ad4e30e --- /dev/null +++ b/.changeset/pink-turtles-switch.md @@ -0,0 +1,5 @@ +--- +'wmr': patch +--- + +First script tag in HTML should be assumed to have prerender(), not last From 4494e19a52e1cf11c1849b6d66ebc2181dab001d Mon Sep 17 00:00:00 2001 From: Ryan Christian Date: Sun, 23 Jan 2022 22:20:07 -0600 Subject: [PATCH 3/3] refactor: Flipping prerender() detection to last script in HTML --- packages/wmr/src/lib/prerender.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/wmr/src/lib/prerender.js b/packages/wmr/src/lib/prerender.js index 8544a314..8f78f6d9 100644 --- a/packages/wmr/src/lib/prerender.js +++ b/packages/wmr/src/lib/prerender.js @@ -70,13 +70,13 @@ async function workerCode({ cwd, out, publicPath, customRoutes }) { // Grab the generated HTML file, which we'll use as a template: const tpl = await fs.readFile(path.resolve(cwd, out, 'index.html'), 'utf-8'); - // The first script in the file that is not external is assumed to have a + // The last script in the file that is not external is assumed to have a // `prerender` export let script; const SCRIPT_TAG = /]*?)?\s+src=(['"]?)([^>]*?)\1(?:\s[^>]*?)?>/g; let match; - while ((match = SCRIPT_TAG.exec(tpl)) && !script) { + while ((match = SCRIPT_TAG.exec(tpl))) { // Ignore external urls if (!match || /^(?:https?|file|data)/.test(match[2])) continue; @@ -103,7 +103,7 @@ async function workerCode({ cwd, out, publicPath, customRoutes }) { // const App = m.default || m[Object.keys(m)[0]]; if (typeof doPrerender !== 'function') { - throw Error(`No prerender() function was exported by the first