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

Next 10.0.8 attempts to generate static error pages despite _error with getInitialProps #22815

Closed
merrywhether opened this issue Mar 6, 2021 · 16 comments · Fixed by #22887
Closed
Assignees
Milestone

Comments

@merrywhether
Copy link

merrywhether commented Mar 6, 2021

What version of Next.js are you using?

10.0.8

What version of Node.js are you using?

12.13.0

What browser are you using?

All

What operating system are you using?

macOS

How are you deploying your application?

Own infra

Describe the Bug

We have an _error component with getInitialProps (as does our _app component) and no /404 or /500 components, as we need dynamism on these routes for i18n, tracking, etc. Under 10.0.7, this correctly opted all error paths out of static optimization during build:

Warning: You have opted-out of Automatic Static Optimization due to `getInitialProps` in `pages/_app`. This does not opt-out pages with `getStaticProps`
Read more: https://err.sh/next.js/opt-out-auto-static-optimization

info  - Collecting page data  
Page                                           Size     First Load JS
┌ λ /                                          306 B           429 kB
├   /_app                                      0 B             296 kB
├ λ /404                                       308 B           429 kB
├ λ /login                                     309 B           429 kB
├ λ /not-found                                 309 B           429 kB
...etc

Under 10.0.8, next build attempts to erroneously generate static pages, which fails because they rely on dynamism (specifically data collected from our custom server and transported into Next via the req object):

Warning: You have opted-out of Automatic Static Optimization due to `getInitialProps` in `pages/_app`. This does not opt-out pages with `getStaticProps`
Read more: https://err.sh/next.js/opt-out-auto-static-optimization

info  - Collecting page data  
[  ==] info  - Generating static pages (0/3)
Error occurred prerendering page "/404". Read more: https://err.sh/next.js/prerender-error
...

Error occurred prerendering page "/404.html". Read more: https://err.sh/next.js/prerender-error
...

Error occurred prerendering page "/500". Read more: https://err.sh/next.js/prerender-error
...

info  - Generating static pages (3/3)

> Build error occurred
Error: Export encountered errors on following paths:
	/404
	/404.html
	/500

I'm guessing this was introduced alongside the new static /500 component introduction?

Expected Behavior

next build won't attempt to generate static pages for 404/500 paths when _error has getInitialProps defined and no static 404 or 500 components are defined

To Reproduce

Attempt to build a next app with _error with getInitialProps defined and no 500 or 404 components

@merrywhether merrywhether added the bug Issue was opened via the bug report template. label Mar 6, 2021
@merrywhether merrywhether changed the title Next 10.0.8 can erroneously generate static error pages Next 10.0.8 attempts to generate static error pages when _error has getInitialProps Mar 6, 2021
@merrywhether merrywhether changed the title Next 10.0.8 attempts to generate static error pages when _error has getInitialProps Next 10.0.8 attempts to generate static error pages despite _error with getInitialProps Mar 6, 2021
@ijjk
Copy link
Member

ijjk commented Mar 6, 2021

Hi, it looks like /404 is being added to the build export when it doesn't need to be since we aren't using this result when you have a custom getInitialProps. We do always export /500 now although it will not be used with next start unless you have a pages/500 which seems to need documentation explaining this behavior.

If you are encountering errors from /404 or /500 being exported here it seems there is likely a bug in your /_error as these pages should be able to be exported without errors.

@ijjk ijjk added kind: bug and removed bug Issue was opened via the bug report template. labels Mar 6, 2021
@ijjk ijjk self-assigned this Mar 6, 2021
@kikoanis
Copy link

kikoanis commented Mar 6, 2021

For me the issue is more than /404 page. The issue is that if you have _error, 404 or 500 pages, the build will try to run getInitialProps

Her is a simple project that tries to produce the issue
https://github.com/kikoanis/nextjs-getInitialProps-bug

@merrywhether
Copy link
Author

merrywhether commented Mar 6, 2021

We have a custom server and it is responsible for:

  • processing the request and generating the correct i18n payload so that our strings can be in the correct language
  • periodically pulling our latest dynamic configuration settings from a remote and sending that into the app handler
  • other stuff based on request headers, filtering out bad requests, etc

All of the Express-to-Next transport is accomplished by appending data to the req object in Express land before sending things into the Next app handler. At runtime, this process is always guaranteed to happen safely and this is reflected in our non-nullable types upon entering Next land. We're consciously avoiding any static generation because it does not support our requirements (i18n at the same routes for instance). From Next 5 when we started and through Next 10.0.7, this was achievable: basically we're here for the SSR and have no need for SSG (in this particular app, we happily use SSG in other contexts).

I understand why we have this issue with the build changes in 10.0.8 - Next's static build can't possibly know about the details of our custom server - but I wouldn't necessarily call this a bug, especially since Next explicitly supports custom servers. The errors are actually thrown in the _app code where we process our custom req additions and feed them into context providers. The _error code is actually pretty straightforward but relies on our i18n context, for instance. I realize that I could work around this particular bug by adding ? or | undefined to all of the relevant types and then do more fallback in _app (and/or its sub-components) at the relevant locations all to be able to generate assets that we don't want and would probably add a post-build step to delete, but that would feel pretty bad.

It's possible that we could transition some of this to an _app-based gSSP approach, but that's not currently supported. Something like our dynamic configuration is basically only valid for its 15 minute window, so a statically generated version would be weird anyway. And I imagine that if _app supported gSSP, then that could again opt things out of SSG anyway.

I may have been too proscriptive in the initial bug description. Basically the problem is that we want to always avoid SSG for this app. That was previously possible through our current configuration. Supporting that combination going forward would be one solution. Another would be simply exposing a configuration flag that explicitly disables all SSG, which would probably be easier to support long-term without regressions. If I had to resort to using patch-package to eliminate this behavior for us on my own, this is certainly the path I would take (making the SSG availability always false somehow).

@kikoanis
Copy link

kikoanis commented Mar 6, 2021

I agree. I'd go with opt-in or opt-out next config setting instead of going through the rabbit hole of guarding all SSG requests.

@jondcallahan
Copy link

I followed the with-sentry example in this repo and it is now failing to build due to the _error.jsx file.

@OlehDutchenko
Copy link
Contributor

I have only /_error.tsx with getServerSideProps. There no /404.tsx or /500.tsx.
And I faced with build error

...
...
...
> Build error occurred
Error: Export encountered errors on following paths:
	/404
	/404.html
	/500
...
...
...

At the errors stack I found your's sugestion message

Error occurred prerendering page "/404". Read more: https://err.sh/next.js/prerender-error
Error: Error for page /_error: pages with `getServerSideProps` can not be exported. See more info here: https://err.sh/next.js/gssp-export

Following this url https://err.sh/next.js/gssp-export I must remove next export but I do not use it.

Before 10.0.8 - all worked fine

@OlehDutchenko
Copy link
Contributor

10.0.7 - works

kodiakhq bot pushed a commit that referenced this issue Mar 8, 2021
This ensures we don't export `/404` during the automatic static optimization during build when the `/404` isn't static and won't be used/copied to the final output. 

x-ref: #22815
@prarabdhb
Copy link

prarabdhb commented Mar 8, 2021

The latest commit pushed in 10.0.9-canary.1 fixes the 404 issue, but I still have a similar issue with respect to /500

Error occurred prerendering page "/500". Read more: https://err.sh/next.js/prerender-error
TypeError: res.redirect is not a function
    at Function.module.exports.1TCz.app_default.a.getInitialProps (/Users/prarabdhb*****/Projects/*****/build/server/pages/_app.js:9753:9)
    at Function._callee$ (/Users/prarabdhbharti/Projects/bharat11-web/node_modules/next-redux-wrapper/lib/index.js:183:44)
    at tryCatch (/Users/prarabdhbharti/Projects/bharat11-web/node_modules/regenerator-runtime/runtime.js:63:40)
    at Generator.invoke [as _invoke] (/Users/prarabdhbharti/Projects/bharat11-web/node_modules/regenerator-runtime/runtime.js:293:22)
    at Generator.next (/Users/prarabdhbharti/Projects/bharat11-web/node_modules/regenerator-runtime/runtime.js:118:21)
    at asyncGeneratorStep (/Users/prarabdhbharti/Projects/bharat11-web/node_modules/next-redux-wrapper/lib/index.js:18:103)
    at _next (/Users/prarabdhbharti/Projects/bharat11-web/node_modules/next-redux-wrapper/lib/index.js:20:194)
    at /Users/prarabdhbharti/Projects/bharat11-web/node_modules/next-redux-wrapper/lib/index.js:20:364
    at new Promise (<anonymous>)
    at Function.<anonymous> (/Users/prarabdhbharti/Projects/bharat11-web/node_modules/next-redux-wrapper/lib/index.js:20:97)

The response object in _app.js's getInitialProps doesn't contain redirect for some reason. Used to work fine with v10.0.7

@matheusalcuri
Copy link

matheusalcuri commented Mar 8, 2021

I am facing the same problem. Upgrading to 10.0.9-canary.2, the error with a /500 still remains as @prarabdhb showed.

Due to some incompatibilities with Webpack 5, in our application it is not possible to go back to v10.0.7. Has anyone found a workaround? Or it's just wait for a commit to resolve this issue?

@timneutkens
Copy link
Member

There's a PR open to fix this: #22887

@timneutkens timneutkens added this to the iteration 17 milestone Mar 9, 2021
@kodiakhq kodiakhq bot closed this as completed in #22887 Mar 9, 2021
kodiakhq bot pushed a commit to ijjk/next.js that referenced this issue Mar 9, 2021
This updates to not automatically export `/500` from `_error` if a custom `getInitialProps` is used since logic may be used inside of this method that causes the export to fail. Users can still opt-in to the static `/500` by adding a `pages/500.js` file. 

This also refactors checking `_app` for custom `getInitialProps` to outside of the static check loop to prevent a potential race condition where we could run this check multiple times un-necessarily.  

Fixes: vercel#22815
@matheusalcuri
Copy link

There's a PR open to fix this: #22887

Great news! Now we just have to wait for a new version

@ijjk
Copy link
Member

ijjk commented Mar 9, 2021

This should be updated in v10.0.9-canary.3 of Next.js, please update and give it a try!

@matheusalcuri
Copy link

This should be updated in v10.0.9-canary.3 of Next.js, please update and give it a try!

I upgraded to this version and now everything is working as it should. My task will not need to be blocked, thank you very much!

@tm1000
Copy link
Contributor

tm1000 commented Mar 11, 2021

Still seems to be somewhat of an issue at least in v10.0.9-canary.6. Except it just says [ =] info - Generating static pages (0/0) and never finishes

@merrywhether
Copy link
Author

Verified this fix worked for us. Thanks for the patch!

@balazsorban44
Copy link
Member

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@vercel vercel locked as resolved and limited conversation to collaborators Jan 28, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants