You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Update: PR with realization of this idea available under #60616
Goals
Reduce the complexity of middleware files.
Integrate route guards into the dynamic routing system.
Avoid redundancy through guard inheritance like in layout.js.
Non-Goals
Replace existing features in the app router / next.js model
Background
When implementing a App with i.e. the following model Users "is in"/"owns" Teams "own" Projects, Teams and/or Projects may be created but not completely configured. Thus, the user would have to redirect every route from /[teamId]/... to /[teamId]/setup.
Same principle applies to auth checking etc.
This route guarding can currently be implemented through middleware.js, which is:
Not aware of dynamic route segments, resulting in manual parsing
Quickly growing as soon as route guarding does more than just "checking auth".
Another solution would be guarding through checks in RSC pages, which is:
Highly redundant (imagine the amount of unnecessary code, as soon as you have a few child pages..)
Doing route checking in layout.js is not possible, since it's rendering only happens once when entering a certain route segment.
Proposal
Extending the amount of special file names (page.js, layout.js, loading.js, etc.) with a guard.js.
guard.js would be highly similar to template.js, as in that it would have the exact same inheritance/extension model as layout.js, but be instantiated newly on every route change.
guard.js would also not require the user to return a react component tree, but instead just return undefined or never when redirecting.
Using this, we would have a file clearly dedicated to route guarding, fitting perfectly into the existing app router (including features like route groups etc.) without having to do tedious manual parsing in a central middleware.js.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Update: PR with realization of this idea available under #60616
Goals
Non-Goals
Background
When implementing a App with i.e. the following model
Users "is in"/"owns" Teams "own" Projects
, Teams and/or Projects may be created but not completely configured. Thus, the user would have to redirect every route from/[teamId]/...
to/[teamId]/setup
.Same principle applies to auth checking etc.
This route guarding can currently be implemented through
middleware.js
, which is:Another solution would be guarding through checks in RSC pages, which is:
Doing route checking in layout.js is not possible, since it's rendering only happens once when entering a certain route segment.
Proposal
Extending the amount of special file names (
page.js
,layout.js
,loading.js
, etc.) with aguard.js
.guard.js
would be highly similar totemplate.js
, as in that it would have the exact same inheritance/extension model as layout.js, but be instantiated newly on every route change.guard.js
would also not require the user to return a react component tree, but instead just return undefined ornever
when redirecting.Using this, we would have a file clearly dedicated to route guarding, fitting perfectly into the existing app router (including features like route groups etc.) without having to do tedious manual parsing in a central
middleware.js
.Beta Was this translation helpful? Give feedback.
All reactions