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

feat: set noindex header for build asset dir /_nuxt #174

Merged
merged 3 commits into from
Dec 17, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { defu } from 'defu'
import { installNuxtSiteConfig, updateSiteConfig } from 'nuxt-site-config/kit'
import { relative } from 'pathe'
import { readPackageJSON } from 'pkg-types'
import { withTrailingSlash } from 'ufo'
import { withoutTrailingSlash, withTrailingSlash } from 'ufo'
import { AiBots, NonHelpfulBots } from './const'
import { setupDevToolsUI } from './devtools'
import { resolveI18nConfig, splitPathForI18nLocales } from './i18n'
Expand Down Expand Up @@ -375,6 +375,12 @@ export default defineNuxtModule<ModuleOptions>({
nuxt.options.routeRules = nuxt.options.routeRules || {}
// convert robot routeRules to header routeRules for static hosting
if (config.header) {
const noIndexPaths = [withoutTrailingSlash(nuxt.options.app.buildAssetsDir), `${nuxt.options.app.buildAssetsDir}**`]
for (const path of noIndexPaths) {
nuxt.options.routeRules[path] = defu({
robots: 'noindex',
}, nuxt.options.routeRules[path])
}
Object.entries(nuxt.options.routeRules).forEach(([route, rules]) => {
const robotRule = normaliseRobotsRouteRule(rules)
// only if a rule has been specified as robots.txt will cover disallows
Expand Down Expand Up @@ -415,7 +421,12 @@ export default defineNuxtModule<ModuleOptions>({
}

const groups = config.groups.map(normalizeGroup)
const pathsToCheck = ['/_nuxt', '/_nuxt/', '/api', '/api/']
const pathsToCheck = [
withoutTrailingSlash(nuxt.options.app.buildAssetsDir),
nuxt.options.app.buildAssetsDir,
'/api',
'/api/',
]
for (const p of pathsToCheck) {
if (groups.some(g => g.disallow.includes(p))) {
logger.warn(`You have disallowed robots accessing \`${withTrailingSlash(p)}**\`, this may prevent your site from being indexed correctly.`)
Expand Down
2 changes: 2 additions & 0 deletions test/routeRules.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ describe('route rule merging', () => {
Disallow: /robots-rule/*
Disallow: /secret/*
Disallow: /excluded/*
Disallow: /_nuxt
Disallow: /_nuxt/*

Sitemap: https://nuxtseo.com/sitemap.xml
# END nuxt-robots"
Expand Down
Loading