Skip to content

Commit 1253cca

Browse files
authoredDec 17, 2024··
feat: set noindex header for build asset dir /_nuxt (#174)
1 parent 539285a commit 1253cca

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed
 

Diff for: ‎src/module.ts

+13-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { defu } from 'defu'
1515
import { installNuxtSiteConfig, updateSiteConfig } from 'nuxt-site-config/kit'
1616
import { relative } from 'pathe'
1717
import { readPackageJSON } from 'pkg-types'
18-
import { withTrailingSlash } from 'ufo'
18+
import { withoutTrailingSlash, withTrailingSlash } from 'ufo'
1919
import { AiBots, NonHelpfulBots } from './const'
2020
import { setupDevToolsUI } from './devtools'
2121
import { resolveI18nConfig, splitPathForI18nLocales } from './i18n'
@@ -375,6 +375,12 @@ export default defineNuxtModule<ModuleOptions>({
375375
nuxt.options.routeRules = nuxt.options.routeRules || {}
376376
// convert robot routeRules to header routeRules for static hosting
377377
if (config.header) {
378+
const noIndexPaths = [withoutTrailingSlash(nuxt.options.app.buildAssetsDir), `${nuxt.options.app.buildAssetsDir}**`]
379+
for (const path of noIndexPaths) {
380+
nuxt.options.routeRules[path] = defu({
381+
robots: 'noindex',
382+
}, nuxt.options.routeRules[path])
383+
}
378384
Object.entries(nuxt.options.routeRules).forEach(([route, rules]) => {
379385
const robotRule = normaliseRobotsRouteRule(rules)
380386
// only if a rule has been specified as robots.txt will cover disallows
@@ -415,7 +421,12 @@ export default defineNuxtModule<ModuleOptions>({
415421
}
416422

417423
const groups = config.groups.map(normalizeGroup)
418-
const pathsToCheck = ['/_nuxt', '/_nuxt/', '/api', '/api/']
424+
const pathsToCheck = [
425+
withoutTrailingSlash(nuxt.options.app.buildAssetsDir),
426+
nuxt.options.app.buildAssetsDir,
427+
'/api',
428+
'/api/',
429+
]
419430
for (const p of pathsToCheck) {
420431
if (groups.some(g => g.disallow.includes(p))) {
421432
logger.warn(`You have disallowed robots accessing \`${withTrailingSlash(p)}**\`, this may prevent your site from being indexed correctly.`)

Diff for: ‎test/routeRules.test.ts

+2
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ describe('route rule merging', () => {
5555
Disallow: /robots-rule/*
5656
Disallow: /secret/*
5757
Disallow: /excluded/*
58+
Disallow: /_nuxt
59+
Disallow: /_nuxt/*
5860
5961
Sitemap: https://nuxtseo.com/sitemap.xml
6062
# END nuxt-robots"

0 commit comments

Comments
 (0)
Please sign in to comment.