Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce throwOnFailure option for prerender #956

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/curvy-goats-occur.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'preact-iso': minor
---

introduce throwOnFailure option for prerender
1 change: 1 addition & 0 deletions packages/preact-iso/prerender.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { VNode } from 'preact';
export interface PrerenderOptions {
maxDepth?: number;
props?: Record<string, unknown>;
throwOnFailure?: boolean;
}

export interface PrerenderResult {
Expand Down
8 changes: 8 additions & 0 deletions packages/preact-iso/prerender.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 || {};
Expand Down Expand Up @@ -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 += `<script type="isodata"></script>`;
return { html, links };
} finally {
Expand Down