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}
+ +