What is the purpose of the public directory in Astro projects?
The public directory holds static files like images, fonts, or icons that are served directly without processing. These files are accessible via the root URL in the browser.
Click to reveal answer
beginner
How do you reference a file named logo.png inside the public directory in your Astro component?
You reference it with a root-relative path like /logo.png. For example, <img src="/logo.png" alt="Logo" />.
Click to reveal answer
beginner
True or False: Files inside the public directory are processed by Astro's build pipeline.
False. Files in the public directory are served as-is without any build processing or bundling.
Click to reveal answer
intermediate
Why should you use the public directory for static assets instead of importing them in Astro components?
Using the public directory is simpler for assets that don't need processing or bundling. It keeps them separate and ensures they are served quickly and directly.
Click to reveal answer
intermediate
What happens if you place a file outside the public directory and try to access it via a URL in Astro?
Files outside the public directory are not served directly. They need to be imported or processed by Astro. Trying to access them via URL will result in a 404 error.
Click to reveal answer
Where should you put images that you want to serve directly in an Astro project?
AInside the <code>node_modules</code> directory
BInside the <code>src/components</code> directory
CInside the <code>src/pages</code> directory
DInside the <code>public</code> directory
✗ Incorrect
Static assets like images should be placed in the public directory to be served directly.
How do you reference a static file named icon.svg inside the public directory in your HTML?
A<code>./icon.svg</code>
B<code>/icon.svg</code>
C<code>src/icon.svg</code>
D<code>public/icon.svg</code>
✗ Incorrect
Files in the public directory are referenced with a root slash, like /icon.svg.
What happens to files inside the public directory during Astro's build process?
AThey are copied as-is to the output folder
BThey are converted to JavaScript modules
CThey are bundled and minified
DThey are ignored and not included
✗ Incorrect
Files in public are copied directly without changes.
If you want to serve a font file directly, where should you place it in an Astro project?
AIn the <code>src/assets</code> folder
BIn the <code>src/components</code> folder
CIn the <code>public</code> directory
DIn the <code>node_modules</code> folder
✗ Incorrect
Fonts to be served directly should go in the public directory.
True or False: You can import images from the public directory using JavaScript import statements in Astro.
AFalse
BOnly if you configure Astro
COnly for SVG files
DTrue
✗ Incorrect
Files in public are not imported; they are referenced by URL.
Explain how the public directory works in Astro and why it is important for static assets.
Think about how you want your images or fonts to be available in the browser.
You got /4 concepts.
Describe how to reference a static asset from the public directory inside an Astro component.
How do you write the URL in HTML to access a file in public?
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of the public directory in an Astro project?
easy
A. To store static files that are served directly without processing
B. To write server-side code for API routes
C. To keep component files for UI rendering
D. To store configuration files for the project
Solution
Step 1: Understand the role of the public directory
The public directory is designed to hold static assets like images, fonts, and icons that do not need processing.
Step 2: Differentiate from other directories
Unlike source or config folders, public files are served as-is with root-relative URLs.
Final Answer:
To store static files that are served directly without processing -> Option A
Quick Check:
Public directory = static files served as-is [OK]
Hint: Public folder holds files served directly at root URL [OK]
Common Mistakes:
Confusing public with source code folders
Thinking public is for server code
Assuming public files are processed or compiled
2. Which of the following is the correct way to reference an image named logo.png stored in the public directory inside an Astro component?
easy
A. <img src="./logo.png" alt="Logo" />
B. <img src="/logo.png" alt="Logo" />
C. <img src="public/logo.png" alt="Logo" />
D. <img src="../logo.png" alt="Logo" />
Solution
Step 1: Understand URL referencing for public assets
Files in the public directory are accessed with root-relative URLs starting with a slash (/).
Step 2: Apply correct path for image source
Using /logo.png correctly points to the file in the public folder.
Final Answer:
<img src="/logo.png" alt="Logo" /> -> Option B
Quick Check:
Public assets use root-relative paths starting with / [OK]
Hint: Use root-relative path starting with / for public files [OK]
Common Mistakes:
Using relative paths like ./ or ../ for public assets
Including 'public/' in the URL path
Forgetting the leading slash in the path
3. Given a file icon.svg inside the public/icons folder, what will be the URL to access it in the browser?
medium
A. /src/icons/icon.svg
B. /public/icons/icon.svg
C. ./icons/icon.svg
D. /icons/icon.svg
Solution
Step 1: Identify the public folder URL mapping
Files inside the public folder are served at the root URL, so the folder name after public is part of the URL.
Step 2: Construct the URL path
The file inside public/icons is accessed as /icons/icon.svg.
Final Answer:
/icons/icon.svg -> Option D
Quick Check:
Public folder maps to root URL, so path = /icons/icon.svg [OK]
Hint: Public subfolders appear directly after root slash in URL [OK]
Common Mistakes:
Including 'public' in the URL path
Using relative paths like ./icons/icon.svg
Referencing source folders instead of public
4. You placed a font file custom-font.woff2 inside the public/fonts folder but your CSS @font-face rule is not loading it. Which of the following is the most likely cause?
medium
A. The font file should be inside the src folder, not public
B. Astro does not support font files in the public directory
C. The CSS URL is missing the leading slash, e.g., url('fonts/custom-font.woff2')
D. The font file must be imported in the component script
Solution
Step 1: Check CSS URL path for public assets
URLs for files in public must start with a slash to be root-relative.
Step 2: Identify the missing slash issue
Using url('fonts/custom-font.woff2') is relative and incorrect; it should be url('/fonts/custom-font.woff2').
Final Answer:
The CSS URL is missing the leading slash, e.g., url('fonts/custom-font.woff2') -> Option C
Quick Check:
Public assets need root-relative URLs starting with / [OK]
Hint: Always start public asset URLs with / in CSS [OK]
Common Mistakes:
Omitting the leading slash in CSS URLs
Moving static assets to wrong folders
Expecting Astro to process font files automatically
5. You want to add a downloadable PDF named guide.pdf to your Astro site. You placed it inside the public/docs folder. How should you create a link in your component so users can download it when clicked?
hard
A. <a href="/docs/guide.pdf" download>Download Guide</a>
B. <a href="docs/guide.pdf" download>Download Guide</a>
C. <a href="/public/docs/guide.pdf" download>Download Guide</a>
D. <a href="./docs/guide.pdf" download>Download Guide</a>
Solution
Step 1: Understand public folder URL mapping
Files inside public are served at root URL, so the path must start with a slash and exclude 'public'.
Step 2: Use correct href with download attribute
The link should use /docs/guide.pdf as href and include the download attribute for direct download.
Final Answer:
<a href="/docs/guide.pdf" download>Download Guide</a> -> Option A
Quick Check:
Public files use root-relative URLs with leading / and download attribute triggers download [OK]
Hint: Use root-relative URL with download attribute for static files [OK]
Common Mistakes:
Omitting the leading slash in href
Including 'public' in the URL path
Using relative paths like ./docs/guide.pdf
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.