vite icon indicating copy to clipboard operation
vite copied to clipboard

Add window check to preloader to support execution of web bundles in Node

Open rschristian opened this issue 6 months ago • 0 comments

Description

Preact tooling has historically offered "headless prerendering" through tools like Preact-CLI and WMR which abstracted away the prerendering process. Now that we fully recommend Vite for new projects (thanks for building out such an awesome tool!), this has been the #​1 most asked for feature by a huge margin from our users.

However, our users have really struggled with this. While I fully appreciate that Vite has a dedicated SSR mode/build to really give the user the ability to customize as they'd like, this is, subjectively, not a great UX for many users such as our own who are completely overwhelmed and unable to make any use of this. Tools like Vike haven't been much better.

As such, for the Preact preset, I've created a prerender plugin that will abstract all of this away in a WMR-like API and singular (client) build, but, there's a singular issue: the preloader. It's the only item that breaks execution of the web bundle in Node. I don't want to flat-out disable it for consumers just so the plugin can support prerendering, but patching it with a window check might just be bordering on "reasonable enough" to warrant upstreaming, perhaps.

I'll fully understand if this is undesired but thought I'd at least ask to see if this could be upstreamed.

Suggested solution

All that's needed is a window check inserted at the end of this conditional:

-  if (__VITE_IS_MODERN__ && deps && deps.length > 0) {
+  if (__VITE_IS_MODERN__ && deps && deps.length > 0 && typeof window !== 'undefined') {

Alternative

N/A. I believe this is the simplest fix.

Additional context

No response

Validations

rschristian avatar Dec 17 '23 05:12 rschristian