diff --git a/docs/3.api/4.advanced/2.kit.md b/docs/3.api/4.advanced/2.kit.md index 7260bd5923..3e112439ec 100644 --- a/docs/3.api/4.advanced/2.kit.md +++ b/docs/3.api/4.advanced/2.kit.md @@ -1960,7 +1960,69 @@ A template object or a string with the path to the template. If a string is prov A function that will be called with the `options` object. It should return a string or a promise that resolves to a string. If `src` is provided, this function will be ignored. -### `updateTemplates({ filter?: ResolvedNuxtTemplate => boolean })` +### `updateTemplates` + +Regenerate templates that match the filter. If no filter is provided, all templates will be regenerated. + +#### Type + +```ts +async function updateTemplates (options: UpdateTemplatesOptions): void + +interface UpdateTemplatesOptions { + filter?: (template: ResolvedNuxtTemplate) => boolean +} + +interface ResolvedNuxtTemplate { + src: string + filename: string + dst: string + options: Record + getContents: (data: Record) => string | Promise + write: boolean + filename: string + dst: string +} +``` + +#### Parameters + +##### `options` + +**Type**: `UpdateTemplatesOptions` + +**Default**: `{}` + +Options to pass to the template. This object can have the following property: + +- `filter` (optional) + + **Type**: `(template: ResolvedNuxtTemplate) => boolean` + + A function that will be called with the `template` object. It should return a boolean indicating whether the template should be regenerated. If `filter` is not provided, all templates will be regenerated. + +#### Example + +```ts +// https://github.com/nuxt/nuxt +import { defineNuxtModule, updateTemplates } from '@nuxt/kit' + +export default defineNuxtModule({ + setup(options, nuxt) { + // watch and rebuild routes template list when one of the pages changes + nuxt.hook('builder:watch', async (event, relativePath) => { + if (event === 'change') { return } + + const path = resolve(nuxt.options.srcDir, relativePath) + if (updateTemplatePaths.some(dir => path.startsWith(dir))) { + await updateTemplates({ + filter: template => template.filename === 'routes.mjs' + }) + } + }) + } +}) +``` ## Nitro