-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
feat(nextjs): Auto-wrap edge-routes and middleware #6746
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
6f4b028
feat(nextjs): Add edge route and middleware wrappers
lforst 416a7c8
feat(nextjs): Auto-wrap edge-routes and middleware
lforst a7c103d
Fix tests on node 10
lforst 487eee2
Merge branch 'lforst-edge-wrappers' into lforst-edge-auto-wrapping
lforst 918e831
Cosmetics
lforst 2791323
Add js doc
lforst a1bf405
Add option to disable auto wrapping of middleware
lforst b7f5511
Rename wrapping target
lforst 7889dbc
Merge remote-tracking branch 'origin/master' into lforst-edge-auto-wr…
lforst 26f154d
Remove unused loader option
lforst File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why this change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We looked at this really obscure field with a typecast here, and I am not sure if it is even part of the Next.js public API. Behavior around the
pages
andpages/src
directory is pretty well defined here and it felt safer in the long run to just do the lookup ourselves. I guess it is not really required to be in this PR but snuck it in as a little cleanup.