File tree 3 files changed +27
-5
lines changed
3 files changed +27
-5
lines changed Original file line number Diff line number Diff line change
1
+ import { createConsola } from 'consola'
2
+
3
+ export const logger = createConsola ( {
4
+ defaults : { tag : '@nuxtjs/robots' } ,
5
+ } )
Original file line number Diff line number Diff line change 1
1
import type { NitroApp } from 'nitropack'
2
2
import { withoutTrailingSlash } from 'ufo'
3
3
import { resolveRobotsTxtContext } from '../util'
4
+ import { logger } from '../logger'
4
5
import { defineNitroPlugin , getRouteRules , useRuntimeConfig } from '#imports'
5
6
6
7
const PRERENDER_NO_SSR_ROUTES = new Set ( [ '/index.html' , '/200.html' , '/404.html' ] )
@@ -13,8 +14,17 @@ export default defineNitroPlugin(async (nitroApp: NitroApp) => {
13
14
await resolveRobotsTxtContext ( undefined , nitroApp )
14
15
const nuxtContentUrls = new Set < string > ( )
15
16
if ( usingNuxtContent ) {
16
- const urls = await ( await nitroApp . localFetch ( '/__robots__/nuxt-content.json' , { } ) ) . json ( )
17
- urls . forEach ( ( url : string ) => nuxtContentUrls . add ( withoutTrailingSlash ( url ) ) )
17
+ let urls : string [ ] | undefined
18
+ try {
19
+ urls = await ( await nitroApp . localFetch ( '/__robots__/nuxt-content.json' , { } ) ) . json ( )
20
+ }
21
+ catch ( e ) {
22
+ logger . error ( 'Failed to read robot rules from content files.' , e )
23
+ // silently fail I suppose
24
+ }
25
+ if ( urls && Array . isArray ( urls ) && urls . length ) {
26
+ urls . forEach ( ( url : string ) => nuxtContentUrls . add ( withoutTrailingSlash ( url ) ) )
27
+ }
18
28
}
19
29
nitroApp . _robots . nuxtContentUrls = nuxtContentUrls
20
30
Original file line number Diff line number Diff line change 1
1
import { defineEventHandler } from 'h3'
2
- import type { ParsedContent } from '@nuxt/content/dist/runtime/types'
2
+ import type { ParsedContent } from '@nuxt/content'
3
+ import { logger } from '../../logger'
3
4
4
5
// @ts -expect-error alias module
5
6
import { serverQueryContent } from '#content/server'
6
7
7
8
export default defineEventHandler ( async ( e ) => {
8
- const contentList = ( await serverQueryContent ( e ) . find ( ) ) as ParsedContent [ ]
9
- return contentList . map ( ( c ) => {
9
+ const content : ParsedContent [ ] = [ ]
10
+ try {
11
+ content . push ( ...( await serverQueryContent ( e ) . find ( ) ) as ParsedContent [ ] )
12
+ }
13
+ catch ( e ) {
14
+ logger . error ( 'Error querying Nuxt content' , e )
15
+ }
16
+ return content . map ( ( c ) => {
10
17
if ( c . _draft || c . _extension !== 'md' || c . _partial )
11
18
return false
12
19
if ( c . path ) {
You can’t perform that action at this time.
0 commit comments