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(nuxt): add support for routeRules
defined within pages
#20391
Conversation
Β Open in CodeSandbox Web Editor | VS Code | VS Code Insiders |
I love it! |
852e807
to
f9f6dd0
Compare
I was just thinking about this, would it be possible that we generate some kind of artifact file that reflects the route rules state ? Perhaps also add a tab to the |
/ecosystem-ci run sanity-module |
π Ran ecosystem CI: Open
|
4a729c8
to
697c2b4
Compare
@pi0 Would love your thoughts here. In dev mode, the first request to any page with inline route rules (that is, the one that triggers a nitro reload) causes a 404 response. This is also the same issue that #22494 works around - when nitro reloads its dev server it seems to return a 404 to any simultaneous request, rather than 'waiting' for the reload and then returning the response. |
8f9cc99
to
a79c82a
Compare
packages/nuxt/src/pages/module.ts
Outdated
// Allow telling Nitro to reload route rules | ||
let updateRouteConfig: () => void | Promise<void> | ||
nuxt.hook('nitro:init', (nitro) => { | ||
updateRouteConfig = () => nitro.updateConfig({ routeRules: defu(inlineRules, baseRules) }) |
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.
One concern would be if other modules are also updating the route rules at runtime, it seems like they would get overridden?
I think the scope for this happening, especially just in development, is pretty low.
Maybe worth merging in nitro.options.routeRules
though?
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.
I think we can use nitro.options._config.routeRules
to access the un-normalised rules.
cc: @pi0 - thoughts on how to avoid clobbering pre-existing route rules?
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.
I guess merging with nuxt.options.routeRules
would be safer as it contains the latest user (nuxt module) modified rules. In context of Nuxt with top level routeRules
, it should be main source of trust.
(we sync runtimeConfig from nuxt to nitro. might need to do a similar fix for route rules as well to make this possible)
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.
nitro:config hook is last place for modules to modify nitro config, so that might be safer. (Hence current and previous implementations.)
Otherwise when we update the rules we overwrite intervening changes.
Or maybe use proxy for runtimeConfig/routeRules in resolved options that automatically reloads both within nitro?
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.
Modules might need to also apply route rules afterward as well (similar to runtimeConfig). We can either sync references so that nitro and nuxt modules update same object or proxy is also good idea for auto reload ππΌ (a possible downside is that sometimes we need to batch changes, advising to prefer update config hook is safer)
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.
How about instead letting nitro keep track of current state and only merge in/reload the updated rules? That way we wouldn't overwrite anything or need to implement proxy/etc within Nuxt...
Nice π |
This is a really nice initiative! I would recommend to enable it behind an experimental flag until next minor release if want to move forward faster. Coupling with nuxi to support HMR on |
It is looking great π Agree to test with an experimental flag until Nuxt 3.8 |
routeRules
defined within pages
π Linked issue
β Type of change
π Description
This adds the ability to define
routeRules
inline, within the pages directory.Example usage:
TODO:
event.context._nitro.routeRules
but this would also miss outgetRouteRulesForPath
being called directly π€π Checklist