Knowing the project structure helps you find files easily and understand how your Astro app works.
Project structure overview in Astro
astro-project/ ├── public/ │ └── images/ ├── src/ │ ├── components/ │ ├── layouts/ │ ├── pages/ │ └── styles/ ├── astro.config.mjs └── package.json
src/ holds your main code like pages and components.
public/ stores static files like images that browsers can access directly.
src/pages/index.astro
src/components/Header.astro
public/favicon.ico
This is a simple Astro page inside src/pages. It shows a heading and uses a favicon from public/.
--- // src/pages/index.astro --- <html lang="en"> <head> <title>Welcome to Astro</title> <link rel="icon" href="/favicon.ico" /> </head> <body> <h1>Hello from Astro!</h1> </body> </html>
Keep your code organized by placing pages, components, and styles in their own folders.
Files in public/ are served as-is and should be used for images, fonts, and icons.
Astro automatically turns files in src/pages into website pages.
Astro projects have a clear folder structure to keep code and assets organized.
src/ is for your code; public/ is for static files.
Understanding this helps you build and maintain your Astro site easily.
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
