Course contentsShow
← Web Development
Lesson 1770 of 2,98233. Static Site Generation and JamstackFree lesson

Data Fetching at Build Time

Fetching content from APIs, CMSs, or markdown files during the build to embed data directly in pre-rendered pages.

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:

  1. Your build script runs (npm run build or similar)
  2. The generator identifies which data sources it needs (API endpoints, markdown files, database queries)
  3. It fetches all that content once
  4. It processes the data and injects it into your page templates
  5. Complete HTML files are generated and saved to disk
  6. 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.