@@ -127,6 +127,12 @@ export interface ModuleOptions {
127
127
* @default max-age=14400, must-revalidate
128
128
*/
129
129
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
130
136
/**
131
137
* Enables debug logs and a debug endpoint.
132
138
*
@@ -179,6 +185,7 @@ export default defineNuxtModule<ModuleOptions>({
179
185
robotsEnabledValue : 'index, follow, max-image-preview:large, max-snippet:-1, max-video-preview:-1' ,
180
186
robotsDisabledValue : 'noindex, nofollow' ,
181
187
disallowNonIndexableRoutes : true ,
188
+ robotsTxt : true ,
182
189
} ,
183
190
async setup ( config , nuxt ) {
184
191
const { resolve } = createResolver ( import . meta. url )
@@ -204,6 +211,11 @@ export default defineNuxtModule<ModuleOptions>({
204
211
return
205
212
}
206
213
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
+
207
219
// TODO remove with v5
208
220
if ( config . rules ) {
209
221
// warn v3 usage and convert to v4
@@ -257,7 +269,7 @@ export default defineNuxtModule<ModuleOptions>({
257
269
if ( config . metaTag )
258
270
addPlugin ( { mode : 'server' , src : resolve ( './runtime/nuxt/plugins/robot-meta.server' ) } )
259
271
260
- if ( config . mergeWithRobotsTxtPath !== false ) {
272
+ if ( config . robotsTxt && config . mergeWithRobotsTxtPath !== false ) {
261
273
let usingRobotsTxtPath = ''
262
274
let robotsTxt : boolean | string = false
263
275
const publicRobotsTxtPath = resolve ( nuxt . options . rootDir , nuxt . options . dir . public , 'robots.txt' )
@@ -268,6 +280,8 @@ export default defineNuxtModule<ModuleOptions>({
268
280
resolve ( nuxt . options . rootDir , nuxt . options . dir . assets , 'robots.txt' ) ,
269
281
// public/_robots.txt
270
282
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' ) ,
271
285
// public/_dir/robots.txt
272
286
resolve ( nuxt . options . rootDir , nuxt . options . dir . public , '_dir' , 'robots.txt' ) ,
273
287
// pages/_dir/robots.txt
@@ -478,7 +492,7 @@ declare module 'h3' {
478
492
const nitroPreset = resolveNitroPreset ( nuxt . options . nitro )
479
493
// only prerender for `nuxi generate`
480
494
const isFirebase = nitroPreset === 'firebase'
481
- if ( isNuxtGenerate ( ) || ( isFirebase && nuxt . options . _build ) ) {
495
+ if ( ( isNuxtGenerate ( ) || ( isFirebase && nuxt . options . _build ) ) && config . robotsTxt ) {
482
496
nuxt . options . generate = nuxt . options . generate || { }
483
497
nuxt . options . generate . routes = asArray ( nuxt . options . generate . routes || [ ] )
484
498
nuxt . options . generate . routes . push ( '/robots.txt' )
@@ -504,11 +518,13 @@ declare module 'h3' {
504
518
filePath : resolve ( './runtime/nuxt/components/RobotMeta' ) ,
505
519
} )
506
520
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
+ }
512
528
// add robots HTTP header handler
513
529
addServerHandler ( {
514
530
handler : resolve ( './runtime/nitro/server/middleware' ) ,
0 commit comments