Data Fetching at Build Time
What you'll learn: How to pull content from external sources during the build process so your static pages contain real data before deployment.
The Core Idea
Data fetching at build time means retrieving content while your site is being built, not when users visit. During the build process, your static site generator reaches out to APIs, content management systems (CMS), databases, or local markdown files to collect all necessary data. This data is then embedded directly into the HTML files that get created, so every page is complete and ready to serve instantly.
Think of it like preparing a restaurant menu: instead of asking the chef about ingredients every time a customer orders (request time), you print the full menu with all details once at the start of the day (build time). Customers see everything immediately without waiting.
How It Works
When you trigger a build:
- Your build script runs (
npm run buildor similar) - The generator identifies which data sources it needs (API endpoints, markdown files, database queries)
- It fetches all that content once
- It processes the data and injects it into your page templates
- Complete HTML files are generated and saved to disk
- These pre-rendered files are deployed to your hosting
For example, a blog might fetch all posts from a headless CMS during the build, generating separate HTML files for each article. When visitors arrive, they receive fully-formed pages with no additional data fetching required.
The Advantage
Since data is fetched once during the build rather than on every request, your pages are incredibly fast to serve—they're just static files. No database queries, no API calls, no waiting.
Key Takeaway: Data fetching at build time transforms dynamic content into static HTML during the build process, eliminating request-time data fetching and maximizing delivery speed.