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

fix(nuxt): don't append new route for redirect if one exists #26368

Merged
merged 3 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/nuxt/src/pages/module.ts
Expand Up @@ -12,7 +12,7 @@ import type { EditableTreeNode, Options as TypedRouterOptions } from 'unplugin-v
import type { NitroRouteConfig } from 'nitropack'
import { defu } from 'defu'
import { distDir } from '../dirs'
import { normalizeRoutes, resolvePagesRoutes } from './utils'
import { normalizeRoutes, resolvePagesRoutes, resolveRoutePaths } from './utils'
import { extractRouteRules, getMappedPages } from './route-rules'
import type { PageMetaPluginOptions } from './plugins/page-meta'
import { PageMetaPlugin } from './plugins/page-meta'
Expand Down Expand Up @@ -371,9 +371,13 @@ export default defineNuxtModule({
// when the app manifest is enabled.
nuxt.hook('pages:extend', (routes) => {
const nitro = useNitro()
let resolvedRoutes: string[]
for (const path in nitro.options.routeRules) {
const rule = nitro.options.routeRules[path]
if (!rule.redirect) { continue }
resolvedRoutes ||= routes.flatMap(route => resolveRoutePaths(route))
// skip if there's already a route matching this path
if (resolvedRoutes.includes(path)) { continue }
routes.push({
_sync: true,
path: path.replace(/\/[^/]*\*\*/, '/:pathMatch(.*)'),
Expand Down
7 changes: 7 additions & 0 deletions packages/nuxt/src/pages/utils.ts
Expand Up @@ -197,7 +197,7 @@
try {
extractedMeta[key] = JSON.parse(runInNewContext(`JSON.stringify(${valueString})`, {}))
} catch {
console.debug(`[nuxt] Skipping extraction of \`${key}\` metadata as it is not JSON-serializable (reading \`${absolutePath}\`).`)

Check warning on line 200 in packages/nuxt/src/pages/utils.ts

View workflow job for this annotation

GitHub Actions / code

Unexpected console statement
dynamicProperties.add(key)
continue
}
Expand All @@ -210,7 +210,7 @@
continue
}
if (element.type !== 'Literal' || typeof element.value !== 'string') {
console.debug(`[nuxt] Skipping extraction of \`${key}\` metadata as it is not an array of string literals (reading \`${absolutePath}\`).`)

Check warning on line 213 in packages/nuxt/src/pages/utils.ts

View workflow job for this annotation

GitHub Actions / code

Unexpected console statement
dynamicProperties.add(key)
continue
}
Expand All @@ -221,7 +221,7 @@
}

if (property.value.type !== 'Literal' || typeof property.value.value !== 'string') {
console.debug(`[nuxt] Skipping extraction of \`${key}\` metadata as it is not a string literal or array of string literals (reading \`${absolutePath}\`).`)

Check warning on line 224 in packages/nuxt/src/pages/utils.ts

View workflow job for this annotation

GitHub Actions / code

Unexpected console statement
dynamicProperties.add(key)
continue
}
Expand Down Expand Up @@ -539,3 +539,10 @@

return path.replace(/\/(?:[^:/]+)?:\w+.*$/, '/**')
}

export function resolveRoutePaths (page: NuxtPage, parent = '/'): string[] {
return [
joinURL(parent, page.path),
...page.children?.flatMap(child => resolveRoutePaths(child, joinURL(parent, page.path))) || []
]
}
5 changes: 5 additions & 0 deletions test/fixtures/basic-types/nuxt.config.ts
Expand Up @@ -30,6 +30,11 @@ export default defineNuxtConfig({
val: 1
}
},
routeRules: {
'/param': {
redirect: '/param/1'
}
},
modules: [
function () {
addTypeTemplate({
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/basic-types/pages/param.vue
@@ -0,0 +1,5 @@
<template>
<div>
<!-- -->
</div>
</template>