From 3db514f869ef47f34efe2c4f07eaed2a4b4c6e1e Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Wed, 20 Sep 2023 12:28:32 +0200 Subject: [PATCH] fix: plugin script error halting JS execution (#1801) --- src/server/rendering/fresh_tags.tsx | 8 ++++- tests/fixture_plugin_error/deno.json | 17 ++++++++++ tests/fixture_plugin_error/dev.ts | 5 +++ tests/fixture_plugin_error/fresh.gen.ts | 18 +++++++++++ tests/fixture_plugin_error/islands/Island.tsx | 17 ++++++++++ tests/fixture_plugin_error/main.ts | 27 ++++++++++++++++ tests/fixture_plugin_error/routes/index.tsx | 5 +++ tests/plugin_test.ts | 32 ++++++++++++++++++- 8 files changed, 127 insertions(+), 2 deletions(-) create mode 100644 tests/fixture_plugin_error/deno.json create mode 100755 tests/fixture_plugin_error/dev.ts create mode 100644 tests/fixture_plugin_error/fresh.gen.ts create mode 100644 tests/fixture_plugin_error/islands/Island.tsx create mode 100644 tests/fixture_plugin_error/main.ts create mode 100644 tests/fixture_plugin_error/routes/index.tsx diff --git a/src/server/rendering/fresh_tags.tsx b/src/server/rendering/fresh_tags.tsx index a3a5f7f6..6dacc575 100644 --- a/src/server/rendering/fresh_tags.tsx +++ b/src/server/rendering/fresh_tags.tsx @@ -91,9 +91,15 @@ export function renderFreshTags( // Then it imports all plugin scripts and executes them (with their respective // state). + if (pluginScripts.length > 0) { + // Use `reportError` if available, otherwise throw in a different event + // loop tick to avoid halting the current script. + script += + `function runPlugin(fn,args){try{fn(args)}catch(err){setTimeout(() => {throw err})}}`; + } for (const [pluginName, entrypoint, i] of pluginScripts) { const url = addImport(`plugin-${pluginName}-${entrypoint}.js`); - script += `import p${i} from "${url}";p${i}(STATE[1][${i}]);`; + script += `import p${i} from "${url}";runPlugin(p${i},STATE[1][${i}]);`; } // Finally, it loads all island scripts and hydrates the islands using the diff --git a/tests/fixture_plugin_error/deno.json b/tests/fixture_plugin_error/deno.json new file mode 100644 index 00000000..3c18115d --- /dev/null +++ b/tests/fixture_plugin_error/deno.json @@ -0,0 +1,17 @@ +{ + "lock": false, + "imports": { + "$fresh/": "../../", + "preact": "https://esm.sh/preact@10.15.1", + "preact/": "https://esm.sh/preact@10.15.1/", + "preact-render-to-string": "https://esm.sh/*preact-render-to-string@6.2.1", + "@preact/signals": "https://esm.sh/*@preact/signals@1.1.3", + "@preact/signals-core": "https://esm.sh/@preact/signals-core@1.2.3", + "@preact/signals-core@1.2.3": "https://esm.sh/@preact/signals-core@1.2.3", + "@preact/signals-core@1.3.0": "https://esm.sh/@preact/signals-core@1.3.0" + }, + "compilerOptions": { + "jsx": "react-jsx", + "jsxImportSource": "preact" + } +} diff --git a/tests/fixture_plugin_error/dev.ts b/tests/fixture_plugin_error/dev.ts new file mode 100755 index 00000000..2d85d6c1 --- /dev/null +++ b/tests/fixture_plugin_error/dev.ts @@ -0,0 +1,5 @@ +#!/usr/bin/env -S deno run -A --watch=static/,routes/ + +import dev from "$fresh/dev.ts"; + +await dev(import.meta.url, "./main.ts"); diff --git a/tests/fixture_plugin_error/fresh.gen.ts b/tests/fixture_plugin_error/fresh.gen.ts new file mode 100644 index 00000000..b3d1d791 --- /dev/null +++ b/tests/fixture_plugin_error/fresh.gen.ts @@ -0,0 +1,18 @@ +// DO NOT EDIT. This file is generated by Fresh. +// This file SHOULD be checked into source version control. +// This file is automatically updated during development when running `dev.ts`. + +import * as $0 from "./routes/index.tsx"; +import * as $$0 from "./islands/Island.tsx"; + +const manifest = { + routes: { + "./routes/index.tsx": $0, + }, + islands: { + "./islands/Island.tsx": $$0, + }, + baseUrl: import.meta.url, +}; + +export default manifest; diff --git a/tests/fixture_plugin_error/islands/Island.tsx b/tests/fixture_plugin_error/islands/Island.tsx new file mode 100644 index 00000000..ced6810f --- /dev/null +++ b/tests/fixture_plugin_error/islands/Island.tsx @@ -0,0 +1,17 @@ +import { useEffect } from "preact/hooks"; +import { useSignal } from "@preact/signals"; + +export default function Island() { + const sig = useSignal(false); + const count = useSignal(0); + useEffect(() => { + sig.value = true; + }, []); + + return ( +
+

{count}

+ +
+ ); +} diff --git a/tests/fixture_plugin_error/main.ts b/tests/fixture_plugin_error/main.ts new file mode 100644 index 00000000..b1b0d543 --- /dev/null +++ b/tests/fixture_plugin_error/main.ts @@ -0,0 +1,27 @@ +/// +/// +/// +/// +/// + +import { start } from "$fresh/server.ts"; +import manifest from "./fresh.gen.ts"; + +function throwErr() { + throw new Error("Error thrown"); +} + +await start(manifest, { + plugins: [ + { + name: "thrower", + entrypoints: { + main: `data:application/javascript,export default ${throwErr}`, + }, + render(ctx) { + ctx.render(); + return { scripts: [{ entrypoint: "main", state: {} }] }; + }, + }, + ], +}); diff --git a/tests/fixture_plugin_error/routes/index.tsx b/tests/fixture_plugin_error/routes/index.tsx new file mode 100644 index 00000000..e0673948 --- /dev/null +++ b/tests/fixture_plugin_error/routes/index.tsx @@ -0,0 +1,5 @@ +import Island from "../islands/Island.tsx"; + +export default function Home() { + return ; +} diff --git a/tests/plugin_test.ts b/tests/plugin_test.ts index 498057f0..300ec70d 100644 --- a/tests/plugin_test.ts +++ b/tests/plugin_test.ts @@ -1,3 +1,4 @@ +import { assertMatch } from "$std/testing/asserts.ts"; import { ServerContext, Status } from "../server.ts"; import { assert, @@ -8,7 +9,12 @@ import { } from "./deps.ts"; import manifest from "./fixture_plugin/fresh.gen.ts"; import options from "./fixture_plugin/options.ts"; -import { runBuild, startFreshServer } from "./test_utils.ts"; +import { + clickWhenListenerReady, + runBuild, + startFreshServer, + withPageName, +} from "./test_utils.ts"; const ctx = await ServerContext.fromManifest(manifest, options); const handler = ctx.handler(); @@ -144,3 +150,27 @@ Deno.test("calls buildStart() and buildEnd()", async () => { "Plugin b: buildEnd", ]); }); + +Deno.test("plugin script doesn't halt island execution", async () => { + await withPageName( + "./tests/fixture_plugin_error/main.ts", + async (page, address) => { + let error; + page.on("pageerror", (err) => { + error = err; + }); + await page.goto(address); + await page.waitForSelector("#ready"); + + let text = await page.$eval("p", (el) => el.textContent!); + assertEquals(text, "0"); + + await clickWhenListenerReady(page, "button"); + + text = await page.$eval("p", (el) => el.textContent!); + assertEquals(text, "1"); + + assertMatch(String(error), /Error thrown/); + }, + ); +});