Skip to content

Commit 5050642

Browse files
committedJul 20, 2024·
fix: safer @nuxt/content integration
Relates to harlan-zw/nuxt-seo#273
1 parent 7e0f595 commit 5050642

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed
 

‎src/runtime/nitro/logger.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { createConsola } from 'consola'
2+
3+
export const logger = createConsola({
4+
defaults: { tag: '@nuxtjs/robots' },
5+
})

‎src/runtime/nitro/plugins/initContext.ts

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { NitroApp } from 'nitropack'
22
import { withoutTrailingSlash } from 'ufo'
33
import { resolveRobotsTxtContext } from '../util'
4+
import { logger } from '../logger'
45
import { defineNitroPlugin, getRouteRules, useRuntimeConfig } from '#imports'
56

67
const PRERENDER_NO_SSR_ROUTES = new Set(['/index.html', '/200.html', '/404.html'])
@@ -13,8 +14,17 @@ export default defineNitroPlugin(async (nitroApp: NitroApp) => {
1314
await resolveRobotsTxtContext(undefined, nitroApp)
1415
const nuxtContentUrls = new Set<string>()
1516
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+
}
1828
}
1929
nitroApp._robots.nuxtContentUrls = nuxtContentUrls
2030

‎src/runtime/nitro/server/__robots__/nuxt-content.ts

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
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'
34

45
// @ts-expect-error alias module
56
import { serverQueryContent } from '#content/server'
67

78
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) => {
1017
if (c._draft || c._extension !== 'md' || c._partial)
1118
return false
1219
if (c.path) {

0 commit comments

Comments
 (0)
Please sign in to comment.