fix: plugin script error halting JS execution (#1801)

This commit is contained in:
Marvin Hagemeister
2023-09-20 12:28:32 +02:00
committed by GitHub
parent 0c37b5f9b9
commit 3db514f869
8 changed files with 127 additions and 2 deletions
+7 -1
View File
@@ -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
+17
View File
@@ -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"
}
}
+5
View File
@@ -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");
+18
View File
@@ -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;
@@ -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 (
<div id={sig.value ? "ready" : "not-ready"}>
<p>{count}</p>
<button onClick={() => count.value++}>click me</button>
</div>
);
}
+27
View File
@@ -0,0 +1,27 @@
/// <reference no-default-lib="true" />
/// <reference lib="dom" />
/// <reference lib="dom.iterable" />
/// <reference lib="dom.asynciterable" />
/// <reference lib="deno.ns" />
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: {} }] };
},
},
],
});
@@ -0,0 +1,5 @@
import Island from "../islands/Island.tsx";
export default function Home() {
return <Island />;
}
+31 -1
View File
@@ -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/);
},
);
});