Skip to content

Commit c895897

Browse files
committedOct 16, 2024·
fix: disallow /robots.txt when using baseURL
1 parent 44a9b45 commit c895897

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed
 

Diff for: ‎docs/content/3.api/1.config.md

+7
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,13 @@ export default defineNuxtConfig({
130130
})
131131
```
132132

133+
## `robotsTxt`
134+
135+
- Type: `boolean`
136+
- Default: `true`
137+
138+
Whether to generate a `robots.txt` file. Useful for disabling when using a base URL.
139+
133140
## `debug`
134141

135142
- Type: `boolean`

Diff for: ‎src/module.ts

+23-7
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,12 @@ export interface ModuleOptions {
127127
* @default max-age=14400, must-revalidate
128128
*/
129129
cacheControl?: string | false
130+
/**
131+
* Whether the robots.txt file should be generated. Useful to disable when running your app with a base URL.
132+
*
133+
* @default false
134+
*/
135+
robotsTxt?: boolean
130136
/**
131137
* Enables debug logs and a debug endpoint.
132138
*
@@ -179,6 +185,7 @@ export default defineNuxtModule<ModuleOptions>({
179185
robotsEnabledValue: 'index, follow, max-image-preview:large, max-snippet:-1, max-video-preview:-1',
180186
robotsDisabledValue: 'noindex, nofollow',
181187
disallowNonIndexableRoutes: true,
188+
robotsTxt: true,
182189
},
183190
async setup(config, nuxt) {
184191
const { resolve } = createResolver(import.meta.url)
@@ -204,6 +211,11 @@ export default defineNuxtModule<ModuleOptions>({
204211
return
205212
}
206213

214+
if (nuxt.options.app.baseURL?.length > 1 && config.robotsTxt) {
215+
logger.error(`You are not allowed to generate a robots.txt with a base URL, please set \`{ robots: { robotsTxt: false } }\` in your nuxt.config.`)
216+
config.robotsTxt = false
217+
}
218+
207219
// TODO remove with v5
208220
if (config.rules) {
209221
// warn v3 usage and convert to v4
@@ -257,7 +269,7 @@ export default defineNuxtModule<ModuleOptions>({
257269
if (config.metaTag)
258270
addPlugin({ mode: 'server', src: resolve('./runtime/nuxt/plugins/robot-meta.server') })
259271

260-
if (config.mergeWithRobotsTxtPath !== false) {
272+
if (config.robotsTxt && config.mergeWithRobotsTxtPath !== false) {
261273
let usingRobotsTxtPath = ''
262274
let robotsTxt: boolean | string = false
263275
const publicRobotsTxtPath = resolve(nuxt.options.rootDir, nuxt.options.dir.public, 'robots.txt')
@@ -268,6 +280,8 @@ export default defineNuxtModule<ModuleOptions>({
268280
resolve(nuxt.options.rootDir, nuxt.options.dir.assets, 'robots.txt'),
269281
// public/_robots.txt
270282
resolve(nuxt.options.rootDir, nuxt.options.dir.public, '_robots.txt'),
283+
// public/_robots.txt
284+
resolve(nuxt.options.rootDir, nuxt.options.dir.public, '_robots.txt'),
271285
// public/_dir/robots.txt
272286
resolve(nuxt.options.rootDir, nuxt.options.dir.public, '_dir', 'robots.txt'),
273287
// pages/_dir/robots.txt
@@ -478,7 +492,7 @@ declare module 'h3' {
478492
const nitroPreset = resolveNitroPreset(nuxt.options.nitro)
479493
// only prerender for `nuxi generate`
480494
const isFirebase = nitroPreset === 'firebase'
481-
if (isNuxtGenerate() || (isFirebase && nuxt.options._build)) {
495+
if ((isNuxtGenerate() || (isFirebase && nuxt.options._build)) && config.robotsTxt) {
482496
nuxt.options.generate = nuxt.options.generate || {}
483497
nuxt.options.generate.routes = asArray(nuxt.options.generate.routes || [])
484498
nuxt.options.generate.routes.push('/robots.txt')
@@ -504,11 +518,13 @@ declare module 'h3' {
504518
filePath: resolve('./runtime/nuxt/components/RobotMeta'),
505519
})
506520

507-
// add robots.txt server handler
508-
addServerHandler({
509-
route: '/robots.txt',
510-
handler: resolve('./runtime/nitro/server/robots-txt'),
511-
})
521+
if (config.robotsTxt) {
522+
// add robots.txt server handler
523+
addServerHandler({
524+
route: '/robots.txt',
525+
handler: resolve('./runtime/nitro/server/robots-txt'),
526+
})
527+
}
512528
// add robots HTTP header handler
513529
addServerHandler({
514530
handler: resolve('./runtime/nitro/server/middleware'),

0 commit comments

Comments
 (0)
Please sign in to comment.