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

Prerender error and workarounds #178

Closed
thangngoc89 opened this issue Jul 3, 2017 · 8 comments · Fixed by #195
Closed

Prerender error and workarounds #178

thangngoc89 opened this issue Jul 3, 2017 · 8 comments · Fixed by #195
Labels

Comments

@thangngoc89
Copy link
Collaborator

thangngoc89 commented Jul 3, 2017

Preact-cli comes with prerender which render your / route using server-side rendering technique. So if you run npm start successfully but npm run build produces some errors, this mean that the prerender is failing because of some code requires DOM to work.

Workarounds:

  • Wrap your with this to make sure it won't execute when prerendering.
if (typeof window !== "undefined") {
  // Your code here
}
  • Turn off preredering by run npm run build -- --no-prerender
@nephix
Copy link

nephix commented Jul 3, 2017

Had the same issue using HashRouter. npm run build -- --prerender=false didn't work for me but the window !== "undefined" check did.

@thangngoc89
Copy link
Collaborator Author

@nephix I updated the command. So sorry. npm run build -- --no-prerender

@rkostrzewski
Copy link
Collaborator

@thangngoc89 what's expected outcome of this issue? Friendly error message with steps how to write code which could be used on node side?

I guess we could wrap prerender with try catch block that checks for TypeErrors (possibly with some whitelist of variables like window, document, location, localStorage and others) and prints user friendly message with steps on how to fix this.

@nephix
Copy link

nephix commented Jul 3, 2017

IMO the example app that preact-cli creates should show how to handle the issue if DOM is required on /

@thangngoc89
Copy link
Collaborator Author

@rkostrzewski Yup. A friendly error for user.

@glegenty
Copy link

Hi !

I try to prerender the default template with no extra code but it fails.
i simple use npm run build and i got this :

Unable to read file: /Users/jorel/Projects/proto/prerender/src/external "preact"
Error: No valid exports main found for '/Users/jorel/Projects/proto/prerender/node_modules/preact'
method: HteQ
at: /Users/jorel/Projects/proto/prerender/src/external "preact":1:17

I'm on macOS Catalina with node 13.6.0

Did i miss something ?

@glegenty
Copy link

The issue was coming from node 13.6. I siwtched to 12.6.2 and the prerender is working fine. The only issue that i see now is that the home page seems to not be rendered. I cannot find the html file in the build folder.

@ForsakenHarmony
Copy link
Member

You might want to update to the latest preact version, that should fix the first problem you had

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants