Discover how a simple folder setup can save hours of frustration!
Why Project structure overview in Astro? - Purpose & Use Cases
Imagine building a website by placing all your files--HTML, CSS, JavaScript, images--into one big folder without any order.
As your site grows, finding and updating files becomes a confusing mess.
Without a clear project structure, you waste time searching for files.
It's easy to overwrite or lose track of important parts.
Collaboration with others becomes chaotic and error-prone.
A well-organized project structure groups related files together.
This makes your code easier to find, update, and share.
Astro's project structure guides you to keep your site clean and scalable.
index.html, style.css, script.js all in one foldersrc/components/, src/pages/, public/assets/ folders organized
Clear project structure lets you build bigger, maintainable websites without confusion.
Think of it like organizing your desk: papers in folders, pens in holders, so you quickly find what you need.
Messy files slow you down and cause mistakes.
Organizing files by purpose saves time and effort.
Astro's structure helps keep your project neat and easy to grow.
Practice
Solution
Step 1: Understand folder roles in Astro
Thesrc/folder is where you put your source code like components, pages, and styles.Step 2: Identify the correct folder for code
Thepublic/folder is for static files like images, not code.Final Answer:
src/-> Option AQuick Check:
Source code folder =src/[OK]
src/, static files in public/ [OK]- Confusing
public/withsrc/ - Thinking
assets/is default code folder - Assuming
dist/holds source code
public/ folder inside an Astro component?Solution
Step 1: Understand how
Files inpublic/files are servedpublic/are served from the root URL, so you reference them with a leading slash.Step 2: Choose the correct path format
Using/images/photo.jpgcorrectly points topublic/images/photo.jpg.Final Answer:
<img src="/images/photo.jpg" /> -> Option CQuick Check:
Static files use root-relative paths [OK]
- Using relative paths like 'src/images' for public files
- Including 'public/' in the path
- Using relative paths that don't work in deployed site
project/ ├─ public/ │ └─ logo.png ├─ src/ │ └─ components/ │ └─ Header.astro └─ src/pages/index.astro
What will be the correct way to display
logo.png inside Header.astro?Solution
Step 1: Recall how
Files inpublic/files are servedpublic/are served from the root URL, so you use a root-relative path starting with '/'.Step 2: Apply correct path in component
Using/logo.pngcorrectly points to the image regardless of component location.Final Answer:
<img src="/logo.png" alt="Logo" /> -> Option DQuick Check:
Public files use root-relative paths [OK]
- Trying to use relative paths to
public/folder - Including 'public' in the path
- Using relative paths that break on deployment
Cannot find module './public/image.png'What is the likely cause based on Astro's project structure?
Solution
Step 1: Understand how static files are accessed
Static files inpublic/are not imported via relative paths in code; they are referenced by URL paths.Step 2: Identify the error cause
Trying to import a file frompublic/using a relative import causes this error.Final Answer:
Trying to import a static file frompublic/using a relative import -> Option BQuick Check:
Static files are referenced by URL, not imported [OK]
public/; use URL paths [OK]- Importing images or static files as modules
- Placing
public/insidesrc/ - Assuming
public/files are bundled like code
/about. Where should you place the new file in the project structure?Solution
Step 1: Understand Astro's page routing
Files insidesrc/pages/map directly to site URLs.Step 2: Choose correct location for new page
Placingabout.astroinsidesrc/pages/creates the/aboutroute.Final Answer:
src/pages/about.astro-> Option AQuick Check:
Pages go insrc/pages/folder [OK]
src/pages/ for routing [OK]- Placing pages in
src/components/ - Adding pages directly in
src/without pages folder - Putting pages in
public/folder
Ready to prove it?
Pass the test and this pattern moves unknown
