Skip to content

Commit 960f9a5

Browse files
Luca ForstnerLms24
andauthored
ref(nextjs): Don't assert existance of pageProps in _app (#5945)
Co-authored-by: Lukas Stracke <[email protected]>
1 parent 07990ac commit 960f9a5

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed

packages/nextjs/src/config/wrappers/withSentryServerSideAppGetInitialProps.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@ export function withSentryServerSideAppGetInitialProps(origAppGetInitialProps: A
4545
});
4646

4747
const requestTransaction = getTransactionFromRequest(req);
48+
49+
// Per definition, `pageProps` is not optional, however an increased amount of users doesn't seem to call
50+
// `App.getInitialProps(appContext)` in their custom `_app` pages which is required as per
51+
// https://nextjs.org/docs/advanced-features/custom-app - resulting in missing `pageProps`.
52+
// For this reason, we just handle the case where `pageProps` doesn't exist explicitly.
53+
if (!appGetInitialProps.pageProps) {
54+
appGetInitialProps.pageProps = {};
55+
}
56+
4857
if (requestTransaction) {
4958
appGetInitialProps.pageProps._sentryTraceData = requestTransaction.toTraceparent();
5059

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import App, { AppContext, AppProps } from 'next/app';
2+
3+
const MyApp = ({ Component, pageProps }: AppProps) => {
4+
return <Component {...pageProps} />;
5+
};
6+
7+
MyApp.getInitialProps = async (appContext: AppContext) => {
8+
// This simulates user misconfiguration. Users should always call `App.getInitialProps(appContext)`, but they don't,
9+
// so we have a test for this so we don't break their apps.
10+
if (appContext.ctx.pathname === '/faultyAppGetInitialProps') {
11+
return {};
12+
}
13+
14+
const appProps = await App.getInitialProps(appContext);
15+
return { ...appProps };
16+
};
17+
18+
export default MyApp;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// See _app.tsx for more information why this file exists.
2+
const Page = (): JSX.Element => <h1>Hello World!</h1>;
3+
4+
export default Page;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const expect = require('expect');
2+
3+
// This test verifies that a faulty configuration of `getInitialProps` in `_app` will not cause our
4+
// auto - wrapping / instrumentation to throw an error.
5+
// See `_app.tsx` for more information.
6+
7+
module.exports = async ({ page, url }) => {
8+
await page.goto(`${url}/faultyAppGetInitialProps`);
9+
const serverErrorText = await page.$x('//*[contains(text(), "Internal Server Error")]');
10+
expect(serverErrorText).toHaveLength(0);
11+
};

0 commit comments

Comments
 (0)