0
0
Astroframework~10 mins

Public directory for static assets in Astro - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Public directory for static assets
Start: Place files in /public
Build or Run Astro
Astro copies /public as-is
Files served at root URL
Browser requests static asset
Server delivers file from /public
Browser displays or uses asset
End
Static files placed in the /public folder are copied as-is and served from the root URL when the site runs.
Execution Sample
Astro
public/
  image.png
  styles.css

<!-- Access in Astro component: -->
<img src="/image.png" alt="Example Image" />
Files in /public are accessible directly by their path from the root URL in the browser.
Execution Table
StepActionInput/StateResult/Output
1Place image.png in /public folderFile system: /public/image.pngFile ready to be served statically
2Run Astro build or dev serverAstro project with /public folder/public contents copied to output as-is
3Browser requests /image.pngHTTP GET /image.pngServer responds with /public/image.png file content
4Browser receives image.pngImage dataImage displayed in page
5Browser requests /styles.cssHTTP GET /styles.cssServer responds with /public/styles.css file content
6Browser receives styles.cssCSS dataStyles applied to page
7Request for /nonexistent.pngHTTP GET /nonexistent.png404 Not Found error
8EndAll static assets served or 404 if missingStatic assets available at root URL
💡 Static assets are served from /public folder at root URL; missing files return 404.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
public/image.pngNot presentCopied to outputRequested by browserDelivered to browserDisplayed
public/styles.cssNot presentCopied to outputRequested by browserDelivered to browserApplied to page
browser requestNoneNone/image.png or /styles.cssReceived file dataRendered or applied
server responseNoneNoneFile content or 404Sent to browserComplete
Key Moments - 3 Insights
Why do files in /public appear at the root URL instead of /public/filename?
Astro serves the /public folder contents directly at the root URL, so /public/image.png is accessed as /image.png in the browser, as shown in execution_table step 3.
What happens if a file is missing from /public but requested by the browser?
The server returns a 404 Not Found error because the file does not exist in /public, as shown in execution_table step 7.
Are files in /public processed or transformed by Astro during build?
No, files in /public are copied as-is without changes, ready to be served statically, as shown in execution_table step 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step does the browser request the static image file?
AStep 3
BStep 2
CStep 5
DStep 7
💡 Hint
Check the 'Action' column for browser requests of /image.png in execution_table.
According to variable_tracker, what is the state of public/styles.css after Step 4?
ANot present
BDelivered to browser
CRequested by browser
DCopied to output
💡 Hint
Look at the 'public/styles.css' row and the 'After Step 4' column in variable_tracker.
If you add a new file to /public, how does it affect the execution_table steps?
AThe file is processed and transformed by Astro
BThe browser requests the file at Step 3 automatically
CThe file is copied during Step 2
DThe file is served from a different URL path
💡 Hint
Refer to execution_table Step 2 where /public contents are copied as-is.
Concept Snapshot
Place static files in /public folder
Astro copies /public files as-is during build
Files are served from root URL (e.g., /image.png)
Browser requests files by root path
Missing files return 404 error
Use /public for images, CSS, fonts, etc.
Full Transcript
In Astro, the public directory holds static files like images and stylesheets. When you put files in /public, Astro copies them exactly as they are during build or when running the dev server. These files become accessible from the root URL of your site. For example, a file /public/image.png is available at /image.png in the browser. When the browser requests these files, the server delivers them directly without processing. If a file is missing, the server returns a 404 error. This setup lets you easily serve static assets alongside your Astro components.