Skip to content

Commit

Permalink
perf(no-ssr): improve the performance
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Nov 29, 2023
1 parent 0a86ef3 commit fc80b21
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/no-ssr/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,33 @@
const stlProp = Object.getOwnPropertyDescriptor(
Error,
'stackTraceLimit'
);
const hasSTL = stlProp?.writable && typeof stlProp.value === 'number';

/** @private */
export const noSSRError = (errorMessage?: string) => {
const originalStackTraceLimit = Error.stackTraceLimit;

/**
* This is *only* safe to do when we know that nothing at any point in the
* stack relies on the `Error.stack` property of the noSSRError. By removing
* the strack trace of the error, we can improve the performance of object
* creation by a lot.
*/
if (hasSTL) {
Error.stackTraceLimit = 0;
}

const error = new Error(errorMessage);

/**
* Restore the stack trace limit to its original value after the error has
* been created.
*/
if (hasSTL) {
Error.stackTraceLimit = originalStackTraceLimit;
}

// Next.js marks errors with `NEXT_DYNAMIC_NO_SSR_CODE` digest as recoverable:
// https://github.com/vercel/next.js/blob/ded28edeae16f8f8b4b9b117a83b5232e3623029/packages/next/src/client/on-recoverable-error.ts#L3
(error as any).digest = 'NEXT_DYNAMIC_NO_SSR_CODE';
Expand Down

0 comments on commit fc80b21

Please sign in to comment.