From 3e011db6e7ed33ddaf9d0048d6d7848bc3133da4 Mon Sep 17 00:00:00 2001 From: resolritter <17429390+resolritter@users.noreply.github.com> Date: Tue, 2 May 2023 21:54:19 -0300 Subject: [PATCH 1/4] introduce throwOnFailure option for prerender --- packages/preact-iso/prerender.d.ts | 1 + packages/preact-iso/prerender.js | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/packages/preact-iso/prerender.d.ts b/packages/preact-iso/prerender.d.ts index 4e9673367..3a3602e60 100644 --- a/packages/preact-iso/prerender.d.ts +++ b/packages/preact-iso/prerender.d.ts @@ -3,6 +3,7 @@ import { VNode } from 'preact'; export interface PrerenderOptions { maxDepth?: number; props?: Record; + throwOnFailure?: boolean; } export interface PrerenderResult { diff --git a/packages/preact-iso/prerender.js b/packages/preact-iso/prerender.js index 538096c06..6348c75a2 100644 --- a/packages/preact-iso/prerender.js +++ b/packages/preact-iso/prerender.js @@ -14,6 +14,7 @@ options.vnode = vnode => { * @param {object} [options] * @param {number} [options.maxDepth = 10] The maximum number of nested asynchronous operations to wait for before flushing * @param {object} [options.props] Additional props to merge into the root JSX element + * @param {boolean} [options.throwOnFailure] Controls if an exception should be thrown in case rendering fails */ export default async function prerender(vnode, options) { options = options || {}; @@ -47,6 +48,13 @@ export default async function prerender(vnode, options) { try { let html = await render(); + if (typeof html !== "string") { + if (options.throwOnFailure) { + throw new Error(`Pre-rendering failed! render() evaluated to: ${html}`); + } else { + html = ""; + } + } html += ``; return { html, links }; } finally { From c36732e025ed0e6f0b55a83782d2f812d7b63ab7 Mon Sep 17 00:00:00 2001 From: resolritter <17429390+resolritter@users.noreply.github.com> Date: Tue, 2 May 2023 22:13:55 -0300 Subject: [PATCH 2/4] change to singlequote --- packages/preact-iso/prerender.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/preact-iso/prerender.js b/packages/preact-iso/prerender.js index 6348c75a2..96909abe2 100644 --- a/packages/preact-iso/prerender.js +++ b/packages/preact-iso/prerender.js @@ -48,11 +48,11 @@ export default async function prerender(vnode, options) { try { let html = await render(); - if (typeof html !== "string") { + if (typeof html !== 'string') { if (options.throwOnFailure) { throw new Error(`Pre-rendering failed! render() evaluated to: ${html}`); } else { - html = ""; + html = ''; } } html += ``; From 2d0e1364e1edf5d9fa5b3f3b6a18c74684aa1376 Mon Sep 17 00:00:00 2001 From: resolritter <17429390+resolritter@users.noreply.github.com> Date: Tue, 2 May 2023 22:20:35 -0300 Subject: [PATCH 3/4] add changeset --- .changeset/curvy-goats-occur.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/curvy-goats-occur.md diff --git a/.changeset/curvy-goats-occur.md b/.changeset/curvy-goats-occur.md new file mode 100644 index 000000000..58aed2f05 --- /dev/null +++ b/.changeset/curvy-goats-occur.md @@ -0,0 +1,5 @@ +--- +'preact-iso': minor +--- + +introduce throwOnFailure option From d4f8dd9af2cafbb377cc7797cb991f0a0135ca49 Mon Sep 17 00:00:00 2001 From: resolritter <17429390+resolritter@users.noreply.github.com> Date: Wed, 3 May 2023 00:55:45 -0300 Subject: [PATCH 4/4] better changeset message --- .changeset/curvy-goats-occur.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/curvy-goats-occur.md b/.changeset/curvy-goats-occur.md index 58aed2f05..7d34a9eca 100644 --- a/.changeset/curvy-goats-occur.md +++ b/.changeset/curvy-goats-occur.md @@ -2,4 +2,4 @@ 'preact-iso': minor --- -introduce throwOnFailure option +introduce throwOnFailure option for prerender