Closed mwszekely closed 5 months ago
Ah, cheers. Vite must've altered their preloader.
Will need to take some time to figure out how to disable it.
Quick hack, if you need to use 5.3: add globalThis.__VITE_PRELOAD__ = []
to your config file.
Hey @rschristian, how would you add it to vite.config.js? I tried the following:
import { defineConfig } from 'vite';
import preact from '@preact/preset-vite';
export default defineConfig({
plugins: [
preact({ prerender: { enabled: true, renderTarget: '#app', additionalPrerenderRoutes: [ '/404' ] } })
],
resolve: {
alias: { 'src': '/src' }
},
define: {
__VITE_PRELOAD__: JSON.stringify('[]'),
// __VITE_PRELOAD__: JSON.stringify([]),
// __VITE_PRELOAD__: '[]',
// __VITE_PRELOAD__: [],
// and all the above using an env var
// __VITE_PRELOAD__: JSON.stringify(process.env.VITE_PRELOAD_VALUE),
},
});
I tried all possible combinations of versions of preact + preact-iso + @preact/preset-vite + vite, but the error error during build: [preact:prerender] __VITE_PRELOAD__ is not defined
is there 9 times out of 10.
Sometimes, after a fresh npm install, it just works. However, if i delete node_modules, package-lock.json, empty cache, run npm install, and npm run build, it usually doesn't work anymore.
Hope it's just that my __VITE_PRELOAD__ config is wrong... I would be very grateful if someone could point me in the right direction!
This will work:
import { defineConfig } from 'vite';
import preact from '@preact/preset-vite';
globalThis.__VITE_PRELOAD__ = []
export default defineConfig({
plugins: [
preact({ prerender: { enabled: true, renderTarget: '#app', additionalPrerenderRoutes: [ '/404' ] } })
],
});
This is a temporary hack of course, but I don't have time at the moment to address this.
Works great! As long as the build output is the same, makes no difference to me. Thanks a lot!
The HTML build output would be the same, JS definitely not. Their preloader is seemingly falling over, that hack I provide is filling in for the lack of a preload asset array (which means the preloader won't actually do anything).
I was totally wrong, not quite sure what they're doing but this works effectively w/out any change in build output. I was mistakenly looking at an intermediary representation.
Essentially, a new project created via
npm init preact
will error, but runningnpm install vite@~5.2
immediately after fixes it.Steps to reproduce:
npm init preact
npm run build
. The following error occurs:Thank you for any time taken to look into it!