Integration Structure
An Astro integration is an object with aname and hooks property:
Basic Example
Integration Hooks
Integrations can define hook functions that run at specific times during Astro’s lifecycle. All hooks are optional.astro:config:setup
Called during Astro initialization, before the Vite or Astro config have been resolved. This hook can be used to extend project configuration.Parameters:
config- A read-only copy of the user-supplied Astro configcommand- The command used to run Astro:'dev','build','preview', or'sync'isRestart- Whether the dev server is restartingupdateConfig- Function to update the user configaddRenderer- Add a framework renderer (React, Vue, etc.)addWatchFile- Add a file to watch for changesinjectScript- Inject a client script into pagesinjectRoute- Add a new route to the Astro projectaddClientDirective- Add a custom client directiveaddDevToolbarApp- Add a custom dev toolbar appaddMiddleware- Add middlewarecreateCodegenDir- Create a directory for generated codelogger- Integration logger instance
astro:config:done
Called after the Astro config has been fully resolved and other integrations have run their
astro:config:setup hooks.Parameters:config- The resolved Astro configsetAdapter- Set the adapter for SSRinjectTypes- Inject TypeScript type definitionslogger- Integration logger instancebuildOutput- The build output mode:'static'or'server'
astro:server:setup
Called during the dev server setup, before the server starts. Allows access to the Vite dev server instance.Parameters:
server- The Vite dev server instancelogger- Integration logger instancetoolbar- Dev toolbar communication helpersrefreshContent- Function to refresh content collections (optional)
astro:server:start
Called after the dev server has started.Parameters:
address- The server address information (host, port, etc.)logger- Integration logger instance
astro:server:done
Called when the dev server is closed.Parameters:
logger- Integration logger instance
astro:build:start
Called when the build process starts.Parameters:
logger- Integration logger instancesetPrerenderer- Set a custom prerenderer (advanced)
astro:build:setup
Called during the build, allows you to extend the Vite configuration used during the build.Parameters:
vite- Vite config for the buildpages- Map of page paths to their build datatarget- Build target:'client'or'server'updateConfig- Function to update the Vite configlogger- Integration logger instance
astro:build:generated
Called after a static build has generated all routes and assets.Parameters:
dir- The build output directorylogger- Integration logger instancerouteToHeaders- Map of routes to their HTTP headers (when adapter supports static headers)
astro:build:ssr
Called after an SSR build has completed.Parameters:
manifest- The serialized SSR manifestmiddlewareEntryPoint- File path of the emitted middlewarelogger- Integration logger instance
astro:build:done
Called after the build has completed and all output files have been written to disk.Parameters:
pages- Array of all generated pages with pathname infodir- The build output directoryassets- Map of asset file names to their output URLslogger- Integration logger instance
astro:route:setup
Called for each route during routing setup. Allows you to modify route options.Parameters:
route- Route options including component path and prerender settinglogger- Integration logger instance
astro:routes:resolved
Called after all routes have been resolved.Parameters:
routes- Array of all resolved routes in the projectlogger- Integration logger instance
Adding Framework Renderers
Use theaddRenderer function in the astro:config:setup hook to add support for UI frameworks:
Injecting Scripts
Use theinjectScript function to add JavaScript to pages:
'before-hydration'- Imported client-side, before hydration'head-inline'- Injected into a script tag in the<head>'page'- Injected into the JavaScript bundle of every page'page-ssr'- Injected into the frontmatter of every Astro page
Adding Routes
Use theinjectRoute function to add new routes:
Adding Middleware
Use theaddMiddleware function to add middleware:
Client Directives
Add custom client directives for controlling component hydration:Dev Toolbar Apps
Add custom apps to the Astro dev toolbar:TypeScript Types
Inject TypeScript type definitions into the user’s project:Integration Logger
All hooks receive alogger instance for consistent logging:
Building Adapters
Adapters are a special type of integration that set theadapter configuration. Use the setAdapter function in astro:config:done:
Adapter Features
supportedAstroFeatures
Defines which Astro features the adapter supports:
staticOutput- Serving static pagesserverOutput- On-demand rendered pageshybridOutput- Mix of static and on-demand pagesi18nDomains- i18n domain routingenvGetSecret- Retrieving secrets fromastro:env/serversharpImageService- Image transformation with Sharp
'stable'- Fully supported'experimental'- Experimental support'deprecated'- Deprecated support'unsupported'- Not supported{ support: 'limited', message: 'Custom message' }- Limited support with explanation
adapterFeatures
Controls adapter-specific build features:
buildOutput- Force'static'or'server'outputmiddlewareMode-'classic'or'edge'middleware executionstaticHeaders- Support for setting headers on static pages