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
Merged
Merged
Changes from 15 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
24c5cfe
feat(nuxt): add support for routeRules defined within pages
danielroe 567cb9c
fix: add route rule hmr support using nitro v2.4
danielroe 8b39d41
Update packages/nuxt/src/pages/module.ts
danielroe 358a448
fix: reset route rules when re-transforming
danielroe 697c2b4
fix: rename plugin
danielroe a79c82a
refactor: use export from `#internal/nitro` directly
danielroe 5cdb975
refactor: extract route rules outside build pipeline
danielroe 4012c99
fix: normalise file names
danielroe d072a5f
Merge remote-tracking branch 'origin/main' into feat/inline-route-rules
danielroe 3942523
refactor: import `useNitroApp` from subpath
danielroe b30316d
test: handle 404s
danielroe 055e81f
fix: use content of transformed code
danielroe a096536
test: revert special 404 handling
danielroe 534ba0c
test: handle reload
danielroe 9648f85
Merge remote-tracking branch 'origin/main' into feat/inline-route-rules
danielroe d026c3c
fix: use routeRules from nitro options
danielroe 71dff82
refactor(nuxt): opt-in with `experimental.inlineRouteRules`
danielroe 4720f00
Merge remote-tracking branch 'origin/main' into feat/inline-route-rules
danielroe 4329798
test: ignore fetch failures
danielroe c1eb55a
test: use polling for route rule reload
danielroe File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
packages/nuxt/src/pages/route-injection.ts β ...nuxt/src/pages/plugins/route-injection.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
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,60 @@ | ||
import { runInNewContext } from 'node:vm' | ||
import type { Node } from 'estree-walker' | ||
import type { CallExpression } from 'estree' | ||
import { walk } from 'estree-walker' | ||
import { transform } from 'esbuild' | ||
import { parse } from 'acorn' | ||
import type { NuxtPage } from '@nuxt/schema' | ||
import type { NitroRouteConfig } from 'nitropack' | ||
import { normalize } from 'pathe' | ||
import { extractScriptContent, pathToNitroGlob } from './utils' | ||
|
||
const ROUTE_RULE_RE = /\bdefineRouteRules\(/ | ||
const ruleCache: Record<string, NitroRouteConfig | null> = {} | ||
|
||
export async function extractRouteRules (code: string): Promise<NitroRouteConfig | null> { | ||
if (code in ruleCache) { | ||
return ruleCache[code] | ||
} | ||
if (!ROUTE_RULE_RE.test(code)) { return null } | ||
|
||
code = extractScriptContent(code) || code | ||
|
||
let rule: NitroRouteConfig | null = null | ||
|
||
const js = await transform(code, { loader: 'ts' }) | ||
walk(parse(js.code, { | ||
sourceType: 'module', | ||
ecmaVersion: 'latest' | ||
}) as Node, { | ||
enter (_node) { | ||
if (_node.type !== 'CallExpression' || (_node as CallExpression).callee.type !== 'Identifier') { return } | ||
const node = _node as CallExpression & { start: number, end: number } | ||
const name = 'name' in node.callee && node.callee.name | ||
if (name === 'defineRouteRules') { | ||
const rulesString = js.code.slice(node.start, node.end) | ||
try { | ||
rule = JSON.parse(runInNewContext(rulesString.replace('defineRouteRules', 'JSON.stringify'), {})) | ||
} catch { | ||
throw new Error('[nuxt] Error parsing route rules. They should be JSON-serializable.') | ||
} | ||
} | ||
} | ||
}) | ||
|
||
ruleCache[code] = rule | ||
return rule | ||
} | ||
|
||
export function getMappedPages (pages: NuxtPage[], paths = {} as { [absolutePath: string]: string | null }, prefix = '') { | ||
for (const page of pages) { | ||
if (page.file) { | ||
const filename = normalize(page.file) | ||
paths[filename] = pathToNitroGlob(prefix + page.path) | ||
} | ||
if (page.children) { | ||
getMappedPages(page.children, paths, page.path + '/') | ||
} | ||
} | ||
return paths | ||
} |
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
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<script setup lang="ts"> | ||
defineRouteRules({ | ||
headers: { | ||
'x-extend': 'added in routeRules' | ||
} | ||
}) | ||
</script> | ||
|
||
<template> | ||
<div> | ||
Route rules defined inline | ||
</div> | ||
</template> |
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
Oops, something went wrong.
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.
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 levelrouteRules
, 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...