preactjs / preact-router

:earth_americas: URL router for Preact.
http://npm.im/preact-router
MIT License
1.02k stars 155 forks source link

SSR Prerendering not working with Router 3.0 and Preact X #310

Open matthewharwood opened 5 years ago

matthewharwood commented 5 years ago

Recreate:

Will throw this error:

matty@laptop: ~/Github/genesis/www/mh on master [+!?]
$ npm run build

> mh@0.0.0 build /Users/matty/Github/genesis/www/mh
> preact build

Build [==                  ] 10% (0.0s) building modules[Browserslist] Could not parse /Users/matty/Github/genesis/package.json. Ignoring it.
Build [=================== ] 95% (2.2s) emittingUnable to read file: /Users/matty/Github/genesis/www/mh/src/node_modules/preact/hooks/dist/hooks.module.js

TypeError: Cannot read property '__H' of undefined
method: null
at: /Users/matty/Github/genesis/www/mh/src/node_modules/preact/hooks/dist/hooks.module.js:1:257

Source code:

This could be caused by using DOM or Web APIs.
Pre-render runs in node and has no access to globals available in browsers.

Consider wrapping code producing error in: 'if (typeof window !== "undefined") { ... }'

Alternatively use 'preact build --no-prerender' to disable prerendering.

See https://github.com/developit/preact-cli#pre-rendering for further information.npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! mh@0.0.0 build: `preact build`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the mh@0.0.0 build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/matty/.npm/_logs/2019-04-26T18_23_42_875Z-debug.log

If you change the router to 2.6.1 and preact to latest ssr/prerender works just fine.

Any help?

kidqueb commented 5 years ago

https://github.com/developit/preact-render-to-string/releases/tag/5.0.2

fixed in this preact-render-to-string release, but needs to be updated in preact-cli

matthewharwood commented 5 years ago

@kidqueb I installed preact-cli@rc.1 and it seems to be getting further; however, now I'm getting a new error

matty@laptop: ~/Github/genesis/www/mh on logo [!]
$ npx preact build
Build [] 95% (2.7s) emitting
 ssr-bundle.60186.css ⏤  495 B (+495 B)
        ssr-bundle.js ⏤  17.8 kB (+17.8 kB)

Build [] 10% (0.0s) building(node:13069) DeprecationWarning: Tapable.apply is deprecated. Call apply on the plugin directly instead
Build [] 90% (1.2s) module assets processing✖ ERROR TypeError: Cannot read property 'hasErrors' of undefined
    at showStats (/Users/matty/Github/genesis/www/mh/node_modules/preact-cli/lib/lib/webpack/run-webpack.js:118:12)
    at compiler.run (/Users/matty/Github/genesis/www/mh/node_modules/preact-cli/lib/lib/webpack/run-webpack.js:106:4)
    at finalCallback (/Users/matty/Github/genesis/www/mh/node_modules/webpack/lib/Compiler.js:220:39)
    at onCompiled (/Users/matty/Github/genesis/www/mh/node_modules/webpack/lib/Compiler.js:228:20)
    at compilation.seal.err (/Users/matty/Github/genesis/www/mh/node_modules/webpack/lib/Compiler.js:626:23)
    at hooks.optimizeChunkAssets.callAsync.err (/Users/matty/Github/genesis/www/mh/node_modules/webpack/lib/Compilation.js:1317:14)
    at _promise1.then._err1 (eval at create (/Users/matty/Github/genesis/www/mh/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:22:1)