diff --git a/src/content/docs/en/reference/integrations-reference.mdx b/src/content/docs/en/reference/integrations-reference.mdx index 9e5580fbc305a..26d079398978a 100644 --- a/src/content/docs/en/reference/integrations-reference.mdx +++ b/src/content/docs/en/reference/integrations-reference.mdx @@ -54,12 +54,18 @@ interface AstroIntegration { logger: AstroIntegrationLogger; }) => void | Promise; 'astro:build:done'?: (options: { dir: URL; routes: RouteData[]; logger: AstroIntegrationLogger; }) => void | Promise; + + // ... any custom hooks from integrations }; } ``` ## Hooks +Astro provides hooks that integrations can implement to execute during certain parts of Astro's lifecycle. Astro hooks are defined in the `IntegrationHooks` interface, which is part of the global `Astro` namespace. + +The following hooks are built-in to Astro: + ### `astro:config:setup` **Next hook:** [`astro:config:done`](#astroconfigdone) @@ -613,6 +619,22 @@ A list of all generated pages. It is an object with one property. - `pathname` - the finalized path of the page. +### Custom hooks + +Custom hooks can be added to integrations by extending the `IntegrationHooks` interface through [global augmentation](https://www.typescriptlang.org/docs/handbook/declaration-merging.html#global-augmentation). + +```ts +declare global { + namespace Astro { + export interface IntegrationHook { + 'your:hook': (params: YourHookParameters) => Promise + } + } +} +``` + +Astro reserves the `astro:` prefix for future built-in hooks. Please choose a different prefix when naming your custom hook. + ## Allow installation with `astro add` [The `astro add` command](/en/reference/cli-reference/#astro-add) allows users to easily add integrations and adapters to their project. If you want _your_ integration to be installable with this tool, **add `astro-integration` to the `keywords` field in your `package.json`**: