Skip to content

Commit

Permalink
fix(nextjs): Check for existence of default export when wrapping pages (
Browse files Browse the repository at this point in the history
  • Loading branch information
lforst committed Aug 11, 2023
1 parent e259aa7 commit 7bba7eb
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions packages/nextjs/src/config/templates/pageWrapperTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@ import * as Sentry from '@sentry/nextjs';
import type { GetServerSideProps, GetStaticProps, NextPage as NextPageComponent } from 'next';

type NextPageModule = {
default: { getInitialProps?: NextPageComponent['getInitialProps'] };
default?: { getInitialProps?: NextPageComponent['getInitialProps'] };
getStaticProps?: GetStaticProps;
getServerSideProps?: GetServerSideProps;
};

const userPageModule = wrapee as NextPageModule;

const pageComponent = userPageModule.default;
const pageComponent = userPageModule ? userPageModule.default : undefined;

const origGetInitialProps = pageComponent.getInitialProps;
const origGetStaticProps = userPageModule.getStaticProps;
const origGetServerSideProps = userPageModule.getServerSideProps;
const origGetInitialProps = pageComponent ? pageComponent.getInitialProps : undefined;
const origGetStaticProps = userPageModule ? userPageModule.getStaticProps : undefined;
const origGetServerSideProps = userPageModule ? userPageModule.getServerSideProps : undefined;

const getInitialPropsWrappers: Record<string, any> = {
'/_app': Sentry.wrapAppGetInitialPropsWithSentry,
Expand All @@ -35,7 +35,7 @@ const getInitialPropsWrappers: Record<string, any> = {

const getInitialPropsWrapper = getInitialPropsWrappers['__ROUTE__'] || Sentry.wrapGetInitialPropsWithSentry;

if (typeof origGetInitialProps === 'function') {
if (pageComponent && typeof origGetInitialProps === 'function') {
pageComponent.getInitialProps = getInitialPropsWrapper(origGetInitialProps) as NextPageComponent['getInitialProps'];
}

Expand Down

0 comments on commit 7bba7eb

Please sign in to comment.