|
1 | 1 | import type { HookRobotsConfigContext, HookRobotsTxtContext } from '../../types'
|
| 2 | +import { logger } from '#robots/server/logger' |
2 | 3 | import { withSiteUrl } from '#site-config/server/composables/utils'
|
3 | 4 | import { defineEventHandler, setHeader } from 'h3'
|
4 | 5 | import { useNitroApp, useRuntimeConfig } from 'nitropack/runtime'
|
@@ -32,17 +33,23 @@ export default defineEventHandler(async (e) => {
|
32 | 33 | .map(s => !s.startsWith('http') ? withSiteUrl(e, s, { withBase: true, absolute: true }) : s),
|
33 | 34 | )]
|
34 | 35 | if (usingNuxtContent) {
|
35 |
| - const contentWithRobotRules = await e.$fetch<string[]>('/__robots__/nuxt-content.json', { |
| 36 | + const contentWithRobotRules = await e.$fetch<string[]>('/__robots__/3nuxt-content.json', { |
36 | 37 | headers: {
|
37 | 38 | Accept: 'application/json',
|
38 | 39 | },
|
39 | 40 | })
|
40 |
| - // add to first '*' group |
41 |
| - for (const group of robotsTxtCtx.groups) { |
42 |
| - if (group.userAgent.includes('*')) { |
43 |
| - group.disallow.push(...contentWithRobotRules) |
44 |
| - // need to filter out empty strings since we now have some content |
45 |
| - group.disallow = group.disallow.filter(Boolean) |
| 41 | + // ensure it's valid json |
| 42 | + if (String(contentWithRobotRules).trim().startsWith('<!DOCTYPE')) { |
| 43 | + logger.error('Invalid HTML returned from /__robots__/nuxt-content.json, skipping.') |
| 44 | + } |
| 45 | + else { |
| 46 | + // add to first '*' group |
| 47 | + for (const group of robotsTxtCtx.groups) { |
| 48 | + if (group.userAgent.includes('*')) { |
| 49 | + group.disallow.push(...contentWithRobotRules) |
| 50 | + // need to filter out empty strings since we now have some content |
| 51 | + group.disallow = group.disallow.filter(Boolean) |
| 52 | + } |
46 | 53 | }
|
47 | 54 | }
|
48 | 55 | }
|
|
0 commit comments