Skip to content
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

Deprecate nested Middleware in favor of root middleware #36772

Merged
merged 22 commits into from
May 19, 2022

Conversation

javivelasco
Copy link
Member

@javivelasco javivelasco commented May 9, 2022

This PR deprecates declaring a middleware under pages in favour of the project root naming it after middleware instead of _middleware. This is in the context of having a simpler execution model for middleware and also ships some refactor work. There is a ton of a code to be simplified after this deprecation but I think it is best to do it progressively.

With this PR, when in development, we will fail whenever we find a nested middleware but we do not include it in the compiler so if the project is using it, it will no longer work. For production we will fail too so it will not be possible to build and deploy a deprecated middleware. The error points to a page that should also be reviewed as part of documentation.

Aside from the deprecation, this migrates all middleware tests to work with a single middleware. It also splits tests into multiple folders to make them easier to isolate and work with. Finally it ships some small code refactor and simplifications.

@javivelasco javivelasco changed the title Root middleware handling Deprecate nested Middleware in favour of root middleware May 9, 2022
@ijjk

This comment was marked as outdated.

@ijjk

This comment was marked as outdated.

@javivelasco javivelasco force-pushed the root-middleware-handling branch 3 times, most recently from 3abe43f to 38f006b Compare May 9, 2022 15:44
Copy link
Member

@timneutkens timneutkens left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me but @ijjk should review the routing parts of the server 👍

errors/nested-middleware.md Outdated Show resolved Hide resolved
errors/nested-middleware.md Outdated Show resolved Hide resolved
errors/nested-middleware.md Outdated Show resolved Hide resolved
errors/nested-middleware.md Outdated Show resolved Hide resolved
errors/nested-middleware.md Outdated Show resolved Hide resolved
errors/nested-middleware.md Outdated Show resolved Hide resolved
errors/nested-middleware.md Outdated Show resolved Hide resolved
errors/nested-middleware.md Outdated Show resolved Hide resolved
errors/nested-middleware.md Outdated Show resolved Hide resolved
errors/nested-middleware.md Outdated Show resolved Hide resolved
getMiddlewareRegex(middleware, {
catchAll: !isSSR,
})
)(cleanedAs)
})

if (!requiresPreflight) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can the above code/checks all be removed in favor a single flag like process.env.__NEXT_HAS_ROOT_MIDDLEWARE which is set via the webpack config here?

'process.env.__NEXT_HAS_REWRITES': JSON.stringify(hasRewrites),

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is a quite big change already I'd love to do that in a follow-up PR if that's ok for you @ijjk WDYT?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah we can do that in a follow-up 👍

javivelasco and others added 17 commits May 19, 2022 16:21
Co-authored-by: Delba de Oliveira <32464864+delbaoliveira@users.noreply.github.com>

Update errors/nested-middleware.md

Co-authored-by: Delba de Oliveira <32464864+delbaoliveira@users.noreply.github.com>

Update errors/nested-middleware.md

Co-authored-by: Delba de Oliveira <32464864+delbaoliveira@users.noreply.github.com>

Update errors/nested-middleware.md

Co-authored-by: Delba de Oliveira <32464864+delbaoliveira@users.noreply.github.com>

Update errors/nested-middleware.md

Co-authored-by: Delba de Oliveira <32464864+delbaoliveira@users.noreply.github.com>
Co-authored-by: JJ Kasper <jj@jjsweb.site>
@Chastrlove
Copy link
Contributor

@javivelasco the filename not always starts with “middleware“. Actually, it starts with root directory.
e.g. /Users/****/next-demo-new/middleware.js

image

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 7, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants