Skip to content

fix(nextjs): Add isomorphic versions of ErrorBoundary, withErrorBoundary and showReportDialog #6987

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
},
"peerDependencies": {
"next": "^10.0.8 || ^11.0 || ^12.0 || ^13.0",
"react": "15.x || 16.x || 17.x || 18.x",
"react": "16.x || 17.x || 18.x",
"webpack": ">= 4.0.0"
},
"peerDependenciesMeta": {
Expand Down
4 changes: 4 additions & 0 deletions packages/nextjs/src/index.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ export declare function flush(timeout?: number | undefined): PromiseLike<boolean
export declare function lastEventId(): string | undefined;
export declare function getSentryRelease(fallback?: string): string | undefined;

export declare const ErrorBoundary: typeof clientSdk.ErrorBoundary;
export declare const showReportDialog: typeof clientSdk.showReportDialog;
export declare const withErrorBoundary: typeof clientSdk.withErrorBoundary;

/**
* @deprecated Use `wrapApiHandlerWithSentry` instead
*/
Expand Down
36 changes: 33 additions & 3 deletions packages/nextjs/src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,39 @@ import { isBuild } from './utils/isBuild';
export * from '@sentry/node';
export { captureUnderscoreErrorException } from '../common/_error';

// Here we want to make sure to only include what doesn't have browser specifics
// because or SSR of next.js we can only use this.
export { ErrorBoundary, showReportDialog, withErrorBoundary } from '@sentry/react';
/**
* A passthrough error boundary for the server that doesn't depend on any react. Error boundaries don't catch SSR errors
* so they should simply be a passthrough.
*/
export const ErrorBoundary = (props: React.PropsWithChildren<unknown>): React.ReactNode => {
if (!props.children) {
return null;
}

if (typeof props.children === 'function') {
return (props.children as () => React.ReactNode)();
}

// since Next.js >= 10 requires React ^16.6.0 we are allowed to return children like this here
return props.children as React.ReactNode;
};

/**
* A passthrough error boundary wrapper for the server that doesn't depend on any react. Error boundaries don't catch
* SSR errors so they should simply be a passthrough.
*/
export function withErrorBoundary<P extends Record<string, any>>(
WrappedComponent: React.ComponentType<P>,
): React.FC<P> {
return WrappedComponent as React.FC<P>;
}

/**
* Just a passthrough since we're on the server and showing the report dialog on the server doesn't make any sense.
*/
export function showReportDialog(): void {
return;
}

const globalWithInjectedValues = global as typeof global & {
__rewriteFramesDistDir__: string;
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/errorboundary.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ReportDialogOptions, Scope} from '@sentry/browser';
import type { ReportDialogOptions, Scope } from '@sentry/browser';
import { captureException, showReportDialog, withScope } from '@sentry/browser';
import { isError, logger } from '@sentry/utils';
import hoistNonReactStatics from 'hoist-non-react-statics';
Expand Down