From c3d7f73d1393cc6f3ba1823136e42caa79c15ffc Mon Sep 17 00:00:00 2001 From: sebaholesz Date: Mon, 28 Aug 2023 21:17:45 +0200 Subject: [PATCH] added more info about error types to SF middleware - this allowed for better error filtration in _error.tsx - since errors from middleware do not contain `err` in context, the condition in _error.tsx which was logging errors for all 404s caused by the middleware is now removed --- storefront/middleware.ts | 9 ++++++--- storefront/pages/_error.tsx | 15 ++++++--------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/storefront/middleware.ts b/storefront/middleware.ts index 017d659acf..d9e6cc059f 100644 --- a/storefront/middleware.ts +++ b/storefront/middleware.ts @@ -43,7 +43,7 @@ export const middleware: NextMiddleware = async (request) => { }); if (!pageTypeResponse.ok) { - return NextResponse.rewrite(new URL(ERROR_PAGE_ROUTE, request.url)); + return NextResponse.rewrite(new URL(ERROR_PAGE_ROUTE, request.url), { status: 404 }); } const pageTypeParsedResponse: { route: FriendlyPageTypesValue; redirectTo: string } = @@ -60,7 +60,10 @@ export const middleware: NextMiddleware = async (request) => { } catch (e) { logException(e); - return NextResponse.rewrite(new URL(ERROR_PAGE_ROUTE, request.url)); + return NextResponse.rewrite(new URL(ERROR_PAGE_ROUTE, request.url), { + status: 500, + statusText: 'Middleware runtime error', + }); } }; @@ -81,7 +84,7 @@ const rewriteDynamicPages = (pageType: FriendlyPageTypesValue, rewriteUrl: strin host, ); - return NextResponse.rewrite(newUrl); + return NextResponse.rewrite(newUrl, pageTypeKey ? undefined : { status: 404 }); }; const getHostFromRequest = (request: NextRequest): string => { diff --git a/storefront/pages/_error.tsx b/storefront/pages/_error.tsx index a919eba6d7..fab0719e4d 100644 --- a/storefront/pages/_error.tsx +++ b/storefront/pages/_error.tsx @@ -26,9 +26,10 @@ const ErrorPage: NextPage = ({ hasGetInitialPropsRun, err, statu }; ErrorPage.getInitialProps = getServerSidePropsWrapper(({ redisClient, domainConfig, t }) => async (context: any) => { + const statusCode = context.res.statusCode || 500; const errorInitialProps: any = await NextErrorComponent.getInitialProps({ res: context.res, - err: context.err, + err: context.err || statusCode === 500 ? context.res.statusText : null, } as any); const serverSideProps = await initServerSideProps({ context, redisClient, domainConfig, t }); // Workaround for https://github.com/vercel/next.js/issues/8592, mark when @@ -48,22 +49,18 @@ ErrorPage.getInitialProps = getServerSidePropsWrapper(({ redisClient, domainConf // Boundary. Read more about what types of exceptions are caught by Error // Boundaries: https://reactjs.org/docs/error-boundaries.html - if (context.err) { - logException(context.err); - } else { - // If this point is reached, getInitialProps was called without any - // information about what the error might be. This is unexpected and may - // indicate a bug introduced in Next.js, so record it in Sentry - logException(new Error(`_error.js getInitialProps missing data at path: ${context.asPath}`)); + if (statusCode !== 404) { + logException(context.err || new Error(`_error.js getInitialProps missing data at path: ${context.asPath}`)); } // Flushing before returning is necessary if deploying to Vercel, see // https://vercel.com/docs/platform/limits#streaming-responses await flush(2000); + const props = 'props' in serverSideProps ? serverSideProps.props : {}; return { ...errorPageProps, - ...('props' in serverSideProps ? serverSideProps.props : {}), + ...{ ...props, statusCode }, }; });