-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(nextjs): Auto-wrap edge-routes and middleware (#6746)
- Loading branch information
Showing
8 changed files
with
173 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
packages/nextjs/src/config/templates/middlewareWrapperTemplate.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* This file is a template for the code which will be substituted when our webpack loader handles middleware files. | ||
* | ||
* We use `__SENTRY_WRAPPING_TARGET_FILE__` as a placeholder for the path to the file being wrapped. Because it's not a real package, | ||
* this causes both TS and ESLint to complain, hence the pragma comments below. | ||
*/ | ||
|
||
// @ts-ignore See above | ||
// eslint-disable-next-line import/no-unresolved | ||
import * as origModule from '__SENTRY_WRAPPING_TARGET_FILE__'; | ||
// eslint-disable-next-line import/no-extraneous-dependencies | ||
import * as Sentry from '@sentry/nextjs'; | ||
|
||
import type { EdgeRouteHandler } from '../../edge/types'; | ||
|
||
type NextApiModule = | ||
| { | ||
// ESM export | ||
default?: EdgeRouteHandler; | ||
middleware?: EdgeRouteHandler; | ||
} | ||
// CJS export | ||
| EdgeRouteHandler; | ||
|
||
const userApiModule = origModule as NextApiModule; | ||
|
||
// Default to undefined. It's possible for Next.js users to not define any exports/handlers in an API route. If that is | ||
// the case Next.js wil crash during runtime but the Sentry SDK should definitely not crash so we need tohandle it. | ||
let userProvidedNamedHandler: EdgeRouteHandler | undefined = undefined; | ||
let userProvidedDefaultHandler: EdgeRouteHandler | undefined = undefined; | ||
|
||
if ('middleware' in userApiModule && typeof userApiModule.middleware === 'function') { | ||
// Handle when user defines via named ESM export: `export { middleware };` | ||
userProvidedNamedHandler = userApiModule.middleware; | ||
} else if ('default' in userApiModule && typeof userApiModule.default === 'function') { | ||
// Handle when user defines via ESM export: `export default myFunction;` | ||
userProvidedDefaultHandler = userApiModule.default; | ||
} else if (typeof userApiModule === 'function') { | ||
// Handle when user defines via CJS export: "module.exports = myFunction;" | ||
userProvidedDefaultHandler = userApiModule; | ||
} | ||
|
||
export const middleware = userProvidedNamedHandler ? Sentry.withSentryMiddleware(userProvidedNamedHandler) : undefined; | ||
export default userProvidedDefaultHandler ? Sentry.withSentryMiddleware(userProvidedDefaultHandler) : undefined; | ||
|
||
// Re-export anything exported by the page module we're wrapping. When processing this code, Rollup is smart enough to | ||
// not include anything whose name matchs something we've explicitly exported above. | ||
// @ts-ignore See above | ||
// eslint-disable-next-line import/no-unresolved | ||
export * from '__SENTRY_WRAPPING_TARGET_FILE__'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
// We cannot make any assumptions about what users define as their handler except maybe that it is a function | ||
export interface EdgeRouteHandler { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
(req: any): any | Promise<any>; | ||
(...args: any[]): any; | ||
} |