preactjs / wmr

👩‍🚀 The tiny all-in-one development tool for modern web apps.
https://wmr.dev/
MIT License
4.92k stars 109 forks source link

Introduce throwOnFailure option for prerender #956

Open resolritter opened 1 year ago

resolritter commented 1 year ago

Problem

Due to some unknown issue with lazy, the prerender function was not working as intended for my components. I eventually overcame that problem, but I did not know it was was happening in the first plac until I looked into the generated HTML and noticed it was wrong. In other words, the prerender is failing silently since the error path is not being handled correctly, as I'll explain below.

The problem is related to this line:

https://github.com/preactjs/wmr/blob/3c5672ecd2f958c8eaf372d33c084dc69228ae3f/packages/preact-iso/prerender.js#L49

The render function was returning undefined because my components could not be rendered correctly. Then we go into the next line:

https://github.com/preactjs/wmr/blob/3c5672ecd2f958c8eaf372d33c084dc69228ae3f/packages/preact-iso/prerender.js#L50

Since html was undefined, the generated HTML would become undefined<script type="isodata"></script>, which is not what I want.

Solution

There should be a way of generating an exception in case the components cannot be rendered correctly. For that, this PR introduces the throwOnFailure option which controls what happens in case rendering fails. It defaults to a falsy value for preserving prerender's current behavior, but arguably it should be true.

changeset-bot[bot] commented 1 year ago

🦋 Changeset detected

Latest commit: d4f8dd9af2cafbb377cc7797cb991f0a0135ca49

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package | Name | Type | | ---------- | ----- | | preact-iso | Minor |

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR