Skip to content

Commit 511a821

Browse files
authored
fix: Nuxt 4 type compatibility
1 parent 27b6b94 commit 511a821

File tree

6 files changed

+76
-63
lines changed

6 files changed

+76
-63
lines changed

docs/content/4.releases/4.v5.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ The v4 of Nuxt Robots provided a backward compatibility `rules` config. As it wa
2222

2323
```diff
2424
export default defineNuxtConfig({
25-
robots: {
26-
- rules: {},
27-
+ groups: {}
28-
}
25+
robots: {
26+
- rules: {},
27+
+ groups: {}
28+
}
2929
})
3030
```
3131

src/kit.ts

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,9 @@
11
import type { Nuxt } from '@nuxt/schema'
22
import type { NitroConfig } from 'nitropack'
33
import type { NuxtModule } from 'nuxt/schema'
4-
import { addTemplate, createResolver, loadNuxtModuleInstance, useNuxt } from '@nuxt/kit'
5-
import { relative } from 'pathe'
4+
import { loadNuxtModuleInstance, useNuxt } from '@nuxt/kit'
65
import { env, provider } from 'std-env'
76

8-
export function extendTypes(module: string, template: (options: { typesPath: string }) => string | Promise<string>) {
9-
const nuxt = useNuxt()
10-
const { resolve } = createResolver(import.meta.url)
11-
// paths.d.ts
12-
addTemplate({
13-
filename: `module/${module}.d.ts`,
14-
getContents: async () => {
15-
const typesPath = relative(resolve(nuxt!.options.rootDir, nuxt!.options.buildDir, 'module'), resolve('runtime/types'))
16-
const s = await template({ typesPath })
17-
return `// Generated by ${module}
18-
${s}
19-
export {}
20-
`
21-
},
22-
})
23-
24-
nuxt.hooks.hook('prepare:types', ({ references }) => {
25-
references.push({ path: resolve(nuxt.options.buildDir, `module/${module}.d.ts`) })
26-
})
27-
nuxt.hooks.hook('nitro:config', (config) => {
28-
config.typescript = config.typescript || {}
29-
config.typescript.tsConfig = config.typescript.tsConfig || {}
30-
config.typescript.tsConfig.include = config.typescript.tsConfig.include || []
31-
config.typescript.tsConfig.include.push(`./module/${module}.d.ts`)
32-
})
33-
}
34-
357
const autodetectableProviders = {
368
azure_static: 'azure',
379
cloudflare_pages: 'cloudflare-pages',

src/module.ts

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
addServerHandler,
77
addServerImportsDir,
88
addServerPlugin,
9+
addTypeTemplate,
910
createResolver,
1011
defineNuxtModule,
1112
hasNuxtModule,
@@ -19,11 +20,13 @@ import { withoutTrailingSlash, withTrailingSlash } from 'ufo'
1920
import { AiBots, NonHelpfulBots } from './const'
2021
import { setupDevToolsUI } from './devtools'
2122
import { resolveI18nConfig, splitPathForI18nLocales } from './i18n'
22-
import { extendTypes, isNuxtGenerate, resolveNitroPreset } from './kit'
23+
import { isNuxtGenerate, resolveNitroPreset } from './kit'
2324
import { logger } from './logger'
2425
import {
25-
asArray,
2626
normaliseRobotsRouteRule,
27+
} from './runtime/server/nitro'
28+
import {
29+
asArray,
2730
normalizeGroup,
2831
parseRobotsTxt,
2932
validateRobots,
@@ -461,8 +464,40 @@ export default defineNuxtModule<ModuleOptions>({
461464
}
462465
})
463466

464-
extendTypes('nuxt-robots', ({ typesPath }) => {
465-
return `
467+
addTypeTemplate({
468+
filename: 'module/nuxt-robots.d.ts',
469+
getContents: (data) => {
470+
const typesPath = relative(resolve(data.nuxt!.options.rootDir, data.nuxt!.options.buildDir, 'module'), resolve('runtime/types'))
471+
return `// Generated by nuxt-robots
472+
473+
import type { RobotsContext } from '#robots/types'
474+
475+
declare module 'nitropack/types' {
476+
interface NitroApp {
477+
_robots: {
478+
ctx: import('${typesPath}').HookRobotsConfigContext
479+
nuxtContentUrls?: Set<string>
480+
},
481+
_robotsRuleMatcher: (url: string) => string
482+
}
483+
interface NitroRouteRules {
484+
robots?: boolean | string | {
485+
indexable: boolean
486+
rule: string
487+
}
488+
}
489+
interface NitroRouteConfig {
490+
robots?: boolean | string | {
491+
indexable: boolean
492+
rule: string
493+
}
494+
}
495+
interface NitroRuntimeHooks {
496+
'robots:config': (ctx: import('${typesPath}').HookRobotsConfigContext) => void | Promise<void>
497+
'robots:robots-txt': (ctx: import('${typesPath}').HookRobotsTxtContext) => void | Promise<void>
498+
}
499+
}
500+
466501
declare module 'nitropack' {
467502
interface NitroApp {
468503
_robots: {
@@ -488,13 +523,18 @@ declare module 'nitropack' {
488523
'robots:robots-txt': (ctx: import('${typesPath}').HookRobotsTxtContext) => void | Promise<void>
489524
}
490525
}
526+
491527
declare module 'h3' {
492-
import type { RobotsContext } from '#robots/types'
493528
interface H3EventContext {
494529
robots: RobotsContext
495530
}
496531
}
532+
533+
export {}
497534
`
535+
},
536+
}, {
537+
nitro: true,
498538
})
499539

500540
// only prerender for `nuxi generate`

src/runtime/server/composables/getPathRobotConfig.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ import type { RobotsContext } from '../../types'
33
import { getRequestHeader } from 'h3'
44
import { useNitroApp, useRuntimeConfig } from 'nitropack/runtime'
55
import { withoutTrailingSlash } from 'ufo'
6-
import { matchPathToRule, normaliseRobotsRouteRule } from '../../util'
6+
import { matchPathToRule } from '../../util'
77
import { createNitroRouteRuleMatcher } from '../kit'
8+
import { normaliseRobotsRouteRule } from '../nitro'
89
import { getSiteRobotConfig } from './getSiteRobotConfig'
910

1011
export function getPathRobotConfig(e: H3Event, options?: { userAgent?: string, skipSiteIndexable?: boolean, path?: string }): RobotsContext {

src/runtime/server/nitro.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import type { NitroRouteConfig } from 'nitropack/types'
2+
3+
export function normaliseRobotsRouteRule(config: NitroRouteConfig) {
4+
// parse allow
5+
let allow: boolean | undefined
6+
if (typeof config.robots === 'boolean')
7+
allow = config.robots
8+
else if (typeof config.robots === 'object' && typeof config.robots.indexable !== 'undefined')
9+
allow = config.robots.indexable
10+
// parse rule
11+
let rule: string | undefined
12+
if (typeof config.robots === 'object' && typeof config.robots.rule !== 'undefined')
13+
rule = config.robots.rule
14+
else if (typeof config.robots === 'string')
15+
rule = config.robots
16+
if (rule && !allow)
17+
allow = rule !== 'none' && !rule.includes('noindex')
18+
if (typeof allow === 'undefined' && typeof rule === 'undefined')
19+
return
20+
return {
21+
allow,
22+
rule,
23+
}
24+
}

src/util.ts

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import type { NitroRouteConfig } from 'nitropack'
21
import type { ParsedRobotsTxt, RobotsGroupInput, RobotsGroupResolved } from './runtime/types'
32
import { createDefu } from 'defu'
43
import { withoutLeadingSlash } from 'ufo'
@@ -284,26 +283,3 @@ export function isInternalRoute(_path: string) {
284283
const lastSegment = path.split('/').pop() || path
285284
return lastSegment.includes('.') || path.startsWith('@')
286285
}
287-
288-
export function normaliseRobotsRouteRule(config: NitroRouteConfig) {
289-
// parse allow
290-
let allow: boolean | undefined
291-
if (typeof config.robots === 'boolean')
292-
allow = config.robots
293-
else if (typeof config.robots === 'object' && typeof config.robots.indexable !== 'undefined')
294-
allow = config.robots.indexable
295-
// parse rule
296-
let rule: string | undefined
297-
if (typeof config.robots === 'object' && typeof config.robots.rule !== 'undefined')
298-
rule = config.robots.rule
299-
else if (typeof config.robots === 'string')
300-
rule = config.robots
301-
if (rule && !allow)
302-
allow = rule !== 'none' && !rule.includes('noindex')
303-
if (typeof allow === 'undefined' && typeof rule === 'undefined')
304-
return
305-
return {
306-
allow,
307-
rule,
308-
}
309-
}

0 commit comments

Comments
 (0)