Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8fea805

Browse files
committed
fix(i18n): respect inline route rules
Fixes harlan-zw/nuxt-seo#404
1 parent 98cffc4 commit 8fea805

File tree

4 files changed

+30
-2
lines changed

4 files changed

+30
-2
lines changed

src/runtime/server/composables/getPathRobotConfig.ts

+12-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ import { createNitroRouteRuleMatcher } from '../kit'
88
import { getSiteRobotConfig } from './getSiteRobotConfig'
99

1010
export function getPathRobotConfig(e: H3Event, options?: { userAgent?: string, skipSiteIndexable?: boolean, path?: string }): RobotsContext {
11+
const runtimeConfig = useRuntimeConfig(e)
1112
// has already been resolved
12-
const { robotsDisabledValue, robotsEnabledValue, isNuxtContentV2 } = useRuntimeConfig()['nuxt-robots']
13+
const { robotsDisabledValue, robotsEnabledValue, isNuxtContentV2 } = runtimeConfig['nuxt-robots']
1314
if (!options?.skipSiteIndexable) {
1415
if (!getSiteRobotConfig(e).indexable) {
1516
return {
@@ -85,7 +86,16 @@ export function getPathRobotConfig(e: H3Event, options?: { userAgent?: string, s
8586

8687
// 3. nitro route rules
8788
nitroApp._robotsRuleMactcher = nitroApp._robotsRuleMactcher || createNitroRouteRuleMatcher()
88-
const routeRules = normaliseRobotsRouteRule(nitroApp._robotsRuleMactcher(path))
89+
let routeRulesPath = path
90+
// if we're using i18n we need to strip leading prefixes so the rule will match
91+
if (runtimeConfig.public?.i18n?.locales) {
92+
const { locales } = runtimeConfig.public.i18n
93+
const locale = locales.find(l => routeRulesPath.startsWith(`/${l.code}`))
94+
if (locale) {
95+
routeRulesPath = routeRulesPath.replace(`/${locale.code}`, '')
96+
}
97+
}
98+
const routeRules = normaliseRobotsRouteRule(nitroApp._robotsRuleMactcher(routeRulesPath))
8999
if (routeRules && (typeof routeRules.allow !== 'undefined' || typeof routeRules.rule !== 'undefined')) {
90100
return {
91101
indexable: routeRules.allow,

test/fixtures/i18n/nuxt.config.ts

+4
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,9 @@ export default defineNuxtConfig({
3737
],
3838
},
3939

40+
experimental: {
41+
inlineRouteRules: true,
42+
},
43+
4044
compatibilityDate: '2025-03-13',
4145
})
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<script lang="ts" setup>
2+
import { defineRouteRules } from '#imports'
3+
4+
defineRouteRules({
5+
robots: false,
6+
})
7+
</script>
8+
9+
<template>
10+
<div>route rules</div>
11+
</template>

test/i18n.test.ts

+3
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,7 @@ describe('i18n', () => {
3737
# END nuxt-robots"
3838
`)
3939
})
40+
it('respects route rules', async () => {
41+
expect((await $fetch('/en/route-rules/?mockProductionEnv=true')).match(/<meta name="robots" content="([^"]+)">/)[1]).toMatchInlineSnapshot(`"noindex, nofollow"`)
42+
})
4043
})

0 commit comments

Comments
 (0)