I'm encountering a 500 error when attempting to compile a Next.js frontend on a Kubernetes pod. I'm using the following commands:
npm i --force
npm run build
npm run start
this is the error:
Page changed from static to dynamic at runtime /story/0d3778b0-9e86-4b0a-a043-ed892f2417a0, reason: headers see more here https://nextjs.org/docs/messages/app-static-to-dynamic-error at Object.staticGenerationBailout (/app/.next/server/chunks/827.js:26985:21) at headers (/app/.next/server/chunks/827.js:26854:39) at Object.apply (/app/.next/server/chunks/500.js:78:82) at T (/app/.next/server/chunks/827.js:27630:25) at Ma (/app/.next/server/chunks/827.js:27793:33) at Array.toJSON (/app/.next/server/chunks/827.js:27586:32) at stringify (<anonymous>) at V (/app/.next/server/chunks/827.js:27885:53) at AsyncLocalStorage.run (node:async_hooks:341:14) at Object.start (/app/.next/server/chunks/827.js:27997:32)
I suspect that the issue may be related to my Sentry configuration, as the front-end works fine when I don't use Sentry. Here is the code for my Sentry configuration in the page/_error.js file:
import * as Sentry from '@sentry/nextjs'; import NextErrorComponent from 'next/error';
const SENTRY_DSN = process.env.NEXT_SENTRY_DSN || process.env.NEXT_PUBLIC_SENTRY_DSN; const isSentryEnabled = !!SENTRY_DSN;
if (isSentryEnabled) {
Sentry.init({
dsn: SENTRY_DSN,
});
}
const CustomErrorComponent = ({ statusCode, hasGetInitialPropsRun, err }) => {
if (isSentryEnabled) {
Sentry.captureException(err);
if (!hasGetInitialPropsRun && err) {
Sentry.captureException(err);
return <NextErrorComponent statusCode={500} />;
}
// Render the default Next.js error page with the status code
return <NextErrorComponent statusCode={statusCode} />;
}
// Render the default Next.js error page with the status code
return <NextErrorComponent statusCode={statusCode} />;
};
CustomErrorComponent.getInitialProps = async (context) => {
if (isSentryEnabled) {
if (context.err) {
Sentry.captureException(context.err);
}
const { res, err, asPath } = context;
if (res && res.statusCode === 404) {
return { statusCode: 404 };
}
const hasGetInitialPropsRun = Boolean(err);
if (err) {
Sentry.captureException(err);
}
const initialProps = await NextErrorComponent.getInitialProps(context);
return { ...initialProps, hasGetInitialPropsRun, err, asPath };
} else {
const initialProps = await NextErrorComponent.getInitialProps(context);
return { ...initialProps };
}
};
export default CustomErrorComponent;
I changed the configuration to a default sentry nextjs
import * as Sentry from "@sentry/nextjs";
import NextErrorComponent from "next/error";
const CustomErrorComponent = props => {
Sentry.captureException(props.err); // Capture the error in Sentry
return <NextErrorComponent statusCode={props.statusCode} />;
};
CustomErrorComponent.getInitialProps = async contextData => {
Sentry.captureException(contextData.err); // Capture the error in Sentry
return {
statusCode: contextData.res?.statusCode ?? 500,
// Set the following to null to disable "Static Generation" optimization
// for this page
getStaticProps: null,
getStaticPaths: null,
};
};
export default CustomErrorComponent;
and attempting to prevent the rendering of the Sentry _error.js file when I run the build process.
npm run build
I already tried change the versions of nextJs
Some of my dependencies are:
- next-auth@4.20.1
- next@13.1.6
- @next/font@13.1.6
- @sentry/nextjs@7.43.0
- @next-auth/sequelize-adapter@1.0.7
- @next-auth/typeorm-legacy-adapter@2.0.1
I think this is a Next.js problem. Check out this issue where they claim that the issue has been fixed in v13.1.6-canary.0
. Some people claim that reverting back to version 3.1.1
fixed the issue. Try upgrading/downgrading the version and see if the issue still persists.