我在Kubernetes pod上编译Next.js前端时遇到500错误。我使用以下命令:
npm i --force
npm run build
npm run start
这就是错误:
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)
我怀疑这个问题可能与我的Sentry配置有关,因为当我不使用Sentry时,前端工作正常。下面是page/_error.js文件中的Sentry配置代码:
import * as Sentry from '@sentry/nextjs';从'next/error'导入NextErrorComponent;
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;
我将配置更改为默认的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;
并试图在运行构建过程时阻止呈现Sentry_error. js文件。
npm run build
我已经尝试过更改nextJs的版本
我的一些依赖项是:
- 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
1条答案
按热度按时间aiazj4mn1#
我认为这是一个Next.js问题。检查这个问题,他们声称这个问题已经在
v13.1.6-canary.0
中得到了解决。有些人声称恢复到3.1.1
版本解决了这个问题。尝试升级/降级版本,看看这个问题是否仍然存在。