0
0
Astroframework~10 mins

Why data fetching happens at build time in Astro - Visual Breakdown

Choose your learning style9 modes available
Concept Flow - Why data fetching happens at build time
Start Build Process
Fetch Data from API or Files
Generate Static Pages with Data
Save Pages as Static HTML
Deploy Static Site
User Requests Page
Serve Pre-built Static Page Instantly
End
This flow shows how Astro fetches data once during build, creates static pages, and serves them fast to users.
Execution Sample
Astro
export async function getStaticPaths() {
  const response = await fetch('https://api.example.com/posts');
  const data = await response.json();
  return data.map(post => ({ params: { id: post.id.toString() } }));
}
This code fetches data at build time to generate paths for static pages.
Execution Table
StepActionData StateResult
1Start buildNo data fetchedBuild process begins
2Fetch data from APIData fetched: list of postsData ready for page generation
3Generate static pagesPages created with fetched dataStatic HTML files created
4Save pagesStatic files savedPages ready for deployment
5Deploy siteStatic site deployedSite live and ready
6User requests pageNo new fetch neededServe static page instantly
7EndBuild complete, user servedFast page load without delay
💡 Build ends after static pages are generated and saved; no runtime data fetching needed.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
dataundefinedlist of posts from APIused to create pagesnot needed at runtime
pagesnonenonestatic HTML pages generatedserved to users
Key Moments - 2 Insights
Why does Astro fetch data only once during build and not on every user request?
Because the execution_table shows data is fetched at Step 2 during build, so pages are pre-made and served instantly without delay at Step 6.
What happens if the data changes after build time?
Since pages are static after Step 4, changes in data require a new build to update pages, as shown by the variable_tracker where 'data' is not fetched again at runtime.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step is the data fetched from the API?
AStep 4
BStep 2
CStep 6
DStep 7
💡 Hint
Check the 'Action' column for 'Fetch data from API' in execution_table row 2.
According to variable_tracker, what happens to the 'data' variable after Step 3?
AIt is used to create pages
BIt is deleted
CIt is fetched again
DIt changes dynamically at runtime
💡 Hint
Look at the 'After Step 3' column for 'data' in variable_tracker.
If data changes frequently, what must happen to update the site content?
AUsers refresh the page to get new data
BData updates automatically without rebuild
CRebuild the site to fetch new data
DNo update is possible
💡 Hint
Refer to key_moments about data changes after build time.
Concept Snapshot
Astro fetches data once at build time.
It generates static HTML pages using that data.
Pages are saved and deployed as static files.
Users get fast page loads with no runtime fetching.
To update data, rebuild the site is needed.
Full Transcript
In Astro, data fetching happens during the build process. First, the build starts, then Astro fetches data from APIs or files. Using this data, it generates static HTML pages. These pages are saved and deployed as a static site. When users request pages, Astro serves these pre-built static pages instantly without fetching data again. This approach makes pages load very fast. However, if the data changes, the site must be rebuilt to update the pages. This method avoids delays caused by fetching data on every user request.