Skip to content

Astro Build Hooks TIL

Quick insight about using build hooks in Astro

TIL: Astro Build Hooks#

Today I learned that Astro v5 has a powerful integration hook system that allows you to tap into different stages of the build process.

The Hook#

The astro:build:done hook fires after the static build completes:

export default {
name: "my-integration",
hooks: {
"astro:build:done": async ({ dir, routes }) => {
console.log(`Build complete! Output: ${dir}`);
console.log(`Generated ${routes.length} routes`);
},
},
};

Why It’s Useful#

This is perfect for:

  • Generating search indexes after build
  • Creating sitemaps
  • Uploading assets to CDN
  • Running post-build validation

Example: Search Index#

Here’s a real example generating a search index:

"astro:build:done": async ({ dir }) => {
const search_index = await build_search_index(dir);
await fs.writeFile(
path.join(dir, "search.json"),
JSON.stringify(search_index)
);
}

Simple but powerful! 🚀