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

Error occurred prerendering page "/500" #23128

Closed
vendramini opened this issue Mar 16, 2021 · 11 comments · Fixed by #23586
Closed

Error occurred prerendering page "/500" #23128

vendramini opened this issue Mar 16, 2021 · 11 comments · Fixed by #23586
Labels
please add a complete reproduction The issue lacks information for further investigation

Comments

@vendramini
Copy link

vendramini commented Mar 16, 2021

What version of Next.js are you using?

10.0.9

What version of Node.js are you using?

12.16.3

What browser are you using?

Chrome

What operating system are you using?

Linux Mint

How are you deploying your application?

next start

Describe the Bug

After upgrading to next 10.0.9 (I was using 10.0.5) during the build phase it is trying to create a 500 page I suppose. In my application it's not possible, I've a complex _app.getInitialProps so I tried to create a 500.js (#22887). It's still trying to create a 500 page, my static 500.js is ignored.

Expected Behavior

Use my static 500.js.

To Reproduce

I've some logic in my _getInitialProps. Everything works in next 10.0.5, but in 10.0.9 I can't compile anymore.

@vendramini vendramini added the bug Issue was opened via the bug report template. label Mar 16, 2021
@timneutkens timneutkens added please add a complete reproduction The issue lacks information for further investigation and removed bug Issue was opened via the bug report template. labels Mar 17, 2021
@scottlet
Copy link

We have the same issue when building in our CI environment, although it works locally when running next build

@Doaa-Ismael
Copy link

guys @scottbert , @vendramini , it happens to me also. To get around this you should make a new file at pages/_error.js and add getInitialProps to it like the following example. let me know if it helped.

function Error({ statusCode }) {
  return (
    <p>
      {statusCode
        ? `An error ${statusCode} occurred on server`
        : 'An error occurred on client'}
    </p>
  )
}

Error.getInitialProps = ({ res, err }) => {
  const statusCode = res ? res.statusCode : err ? err.statusCode : 404
  return { statusCode }
}

export default Error

@vendramini Next try to generate a static 500 in build time, not a dynamic one. So I think you need to edit the description.

@vendramini
Copy link
Author

@Doaa-Ismael hey! Thank you for the answer, but it didn't solve the problem :( I'm getting the same error.

[=== ] info  - Generating static pages (0/1)
Error occurred prerendering page "/500". Read more: https://err.sh/next.js/prerender-error
TypeError: Cannot read property 'isAndroid' of undefined

isAndroid is a property which is coming from a req from express server.

@lapa182
Copy link

lapa182 commented Mar 19, 2021

I think the issue it's pretty much the same as mine, so the steps are quite simple, it's happening when running next build. It was working fine with 10.0.6.

-- | --
18:26:01.395 | Error: Minified React error #31; visit https://reactjs.org/docs/error-decoder.html?invariant=31&args[]=%5Bobject%20Promise%5D for the full message or use the non-minified dev environment for full errors and additional helpful warnings.
18:26:01.395 | at O (/vercel/workpath0/node_modules/react/cjs/react.production.min.js:17:207)
18:26:01.396 | at P (/vercel/workpath0/node_modules/react/cjs/react.production.min.js:17:355)
18:26:01.396 | at toArray (/vercel/workpath0/node_modules/react/cjs/react.production.min.js:19:177)
18:26:01.396 | at a.b.render (/vercel/workpath0/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:42:249)
18:26:01.396 | at a.b.read (/vercel/workpath0/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:41:83)
18:26:01.396 | at exports.renderToString (/vercel/workpath0/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:52:138)
18:26:01.396 | at Object.renderPage (/vercel/workpath0/node_modules/next/dist/next-server/server/render.js:54:851)
18:26:01.396 | at Function.getInitialProps (/vercel/workpath0/.next/server/pages/_document.js:741:19)
18:26:01.396 | at loadGetInitialProps (/vercel/workpath0/node_modules/next/dist/next-server/lib/utils.js:5:101)
18:26:01.397 | at renderToHTML (/vercel/workpath0/node_modules/next/dist/next-server/server/render.js:54:1142)
18:26:01.397 | > Build error occurred
18:26:01.398 | info  - Generating static pages (1/1)
18:26:01.408 | Error: Export encountered errors on following paths:
18:26:01.408 | /500
18:26:01.408 | at /vercel/workpath0/node_modules/next/dist/export/index.js:31:1103
18:26:01.408 | at async Span.traceAsyncFn (/vercel/workpath0/node_modules/next/dist/telemetry/trace/trace.js:5:584)
18:26:01.409 | at async /vercel/workpath0/node_modules/next/dist/build/index.js:41:49
18:26:01.409 | at async Span.traceAsyncFn (/vercel/workpath0/node_modules/next/dist/telemetry/trace/trace.js:5:584)
18:26:01.409 | at async /vercel/workpath0/node_modules/next/dist/build/index.js:23:1472
18:26:01.409 | at async Span.traceAsyncFn (/vercel/workpath0/node_modules/next/dist/telemetry/trace/trace.js:5:584)
18:26:01.568 | error Command failed with exit code 1.
18:26:01.568 | info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
18:26:01.586 | Error: Command "yarn run build" exited with 1```

@rblakejohnson
Copy link

Saw the same issue and went to 10.0.6 as @lapa182 suggests and it works fine.

@scottlet
Copy link

scottlet commented Mar 25, 2021

We have the same issue when building in our CI environment, although it works locally when running next build

Sorry for the lateness to respond. I also "fixed" it by doing pretty much what @Doaa-Ismael suggested.
I think this has something to do with the way we're building our code, "opting out" of automatic static optimization, leading to the warning "Warning: You have opted-out of Automatic Static Optimization due to getInitialProps in pages/_app. This does not opt-out pages with getStaticProps" when building. It'd be interesting to know if others are using the same functionality as maybe it's this that causes the issue?

@Psychosynthesis
Copy link

Psychosynthesis commented Mar 25, 2021

Got the same error after upgrading from 10.0.7 to 10.0.9.

Our old code of pages/_error/index.tsx, cases this error:

import React from 'react';
import { isSerializable } from '@Helpers';
import { ErrorRenderer } from '@Components';

const Error = (errorData: any) => {
	let error = 'Unknown error!';
	if (Object.keys(errorData).length) {
		error = errorData;
	}
	// console.error('Error data: ', error);

	return(
		<div style={{
			display: 'flex',
			justifyContent: 'center',
			paddingTop: '100px',
			width: '100%',
			background: '#222',
			color: '#eee'
		}}>
			<ErrorRenderer error={error} />
		</div>
	);
}

export async function getStaticProps(errorData:any) {
	let errors: any;
	if (errorData) {
		if (isSerializable(errorData)) { errors = errorData; }
		else { errors = 'Error is not serializable!'; }
	} else {
		errors = 'Unknown error!';
		console.error('Unknown error!');
	}
	console.error('Error received at getStaticProps on _error page: ', errors);
	if (errors) { return { props: { errors }, } } // will be passed to the page component as props
	else { return { props: { errors: "Can't handle this error!" }, } };
}

export default Error;

The variation from @Doaa-Ismael helped, although I don't understand why the logic is like this now.

  1. It is not clear why the error reports the generation of the page 500, while in our pages folder there is no 500 at all.

  2. It is not clear why getInitialProps is used in the solution, although the official documentation suggests not to use it anymore.

@laSinteZ
Copy link

laSinteZ commented Apr 18, 2021

Since my issue was closed and this one requires reproduction, it is here: https://github.com/laSinteZ/nextjs-10-1-error-500
Also, according to tests, this is intended behaviour (or at least it seems like so):
https://github.com/vercel/next.js/blob/canary/test/integration/500-page/test/index.test.js#L114

timhabermaas added a commit to timhabermaas/cubemania that referenced this issue May 19, 2021
@j-lee8

This comment has been minimized.

@niksajanjic
Copy link

niksajanjic commented Jun 8, 2021

Similar issue on our end too:

info  - Collecting page data
[ ===] info  - Generating static pages (0/1)
Error occurred prerendering page "/500". Read more: https://nextjs.org/docs/messages/prerender-error
TypeError: res.writeHead is not a function
    at Function.module.exports.IlR1.MyApp.getInitialProps (/Users/nickyjay/Projects/test/test/.next/server/pages/_app.js:731:9)
    at loadGetInitialProps (/Users/nickyjay/Projects/test/test/node_modules/next/dist/next-server/lib/utils.js:5:101)
    at renderToHTML (/Users/nickyjay/Projects/test/test/node_modules/next/dist/next-server/server/render.js:28:1572)
    at async /Users/nickyjay/Projects/test/test/node_modules/next/dist/export/worker.js:26:6
    at async Span.traceAsyncFn (/Users/nickyjay/Projects/test/test/node_modules/next/dist/telemetry/trace/trace.js:6:584)
info  - Generating static pages (1/1)

> Build error occurred
Error: Export encountered errors on following paths:
	/500
    at /Users/nickyjay/Projects/test/test/node_modules/next/dist/export/index.js:31:1106
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (node:internal/process/task_queues:94:5)
    at async Span.traceAsyncFn (/Users/nickyjay/Projects/test/test/node_modules/next/dist/telemetry/trace/trace.js:6:584)
    at async /Users/nickyjay/Projects/test/test/node_modules/next/dist/build/index.js:45:49
    at async Span.traceAsyncFn (/Users/nickyjay/Projects/test/test/node_modules/next/dist/telemetry/trace/trace.js:6:584)
    at async /Users/nickyjay/Projects/test/test/node_modules/next/dist/build/index.js:27:1475
    at async Span.traceAsyncFn (/Users/nickyjay/Projects/test/test/node_modules/next/dist/telemetry/trace/trace.js:6:584)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

We copied the exact code from here to make sure it is not failing because of us and we still get the same error.

@kodiakhq kodiakhq bot closed this as completed in #23586 Jun 11, 2021
kodiakhq bot pushed a commit that referenced this issue Jun 11, 2021
This prevents unexpected errors occurring from users leveraging `getServerSideProps` in `_error` with the new `/500` prerendering as we are currently only checking for a custom `getInitialProps` in `_error` since it opts out of the static optimization although `getServerSideProps` also opts out of the optimization. 

Fixes: #23541
Fixes: #23128
Fixes: #23541
Fixes: #24206

## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
janicklas-ralph pushed a commit to janicklas-ralph/next.js that referenced this issue Jun 11, 2021
…23586)

This prevents unexpected errors occurring from users leveraging `getServerSideProps` in `_error` with the new `/500` prerendering as we are currently only checking for a custom `getInitialProps` in `_error` since it opts out of the static optimization although `getServerSideProps` also opts out of the optimization. 

Fixes: vercel#23541
Fixes: vercel#23128
Fixes: vercel#23541
Fixes: vercel#24206

## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
flybayer pushed a commit to blitz-js/next.js that referenced this issue Jun 16, 2021
…23586)

This prevents unexpected errors occurring from users leveraging `getServerSideProps` in `_error` with the new `/500` prerendering as we are currently only checking for a custom `getInitialProps` in `_error` since it opts out of the static optimization although `getServerSideProps` also opts out of the optimization. 

Fixes: vercel#23541
Fixes: vercel#23128
Fixes: vercel#23541
Fixes: vercel#24206

## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
@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
please add a complete reproduction The issue lacks information for further investigation
Projects
None yet
Development

Successfully merging a pull request may close this issue.