An Astro project structure has one required folder, src/pages/, where each file becomes a route, plus public/ for files served as-is and astro.config.mjs for configuration. Everything else (src/components/, src/layouts/, src/content/, src/styles/, src/assets/) is a convention, not a rule: Astro only gives special meaning to a short list of reserved paths.
This guide goes through each folder and file youβll meet in an Astro 7 project, what Astro does with it, and where your own code should go.
Key Takeaways
src/pages/is the only folder that creates URLs. Files elsewhere are just modules you import.public/files are copied untouched; files insrc/are processed, bundled and optimized.src/content.config.tsdefines content collections;src/middleware.tsandsrc/actions/are other reserved paths..astro/anddist/are generated. Donβt edit them, and keep them out of git.- Folder names like
components/andlayouts/are conventions you can rename freely.
A typical Astro project structure
Hereβs what a real site looks like once it has a blog, a few components and some images:
my-site/
βββ public/
β βββ favicon.svg
β βββ robots.txt
βββ src/
β βββ assets/
β β βββ hero.jpg
β βββ components/
β β βββ Header.astro
β β βββ Card.astro
β βββ content/
β β βββ blog/
β β βββ first-post.md
β βββ layouts/
β β βββ BaseLayout.astro
β βββ pages/
β β βββ index.astro
β β βββ about.astro
β β βββ blog/
β β βββ index.astro
β β βββ [id].astro
β βββ styles/
β β βββ global.css
β βββ content.config.ts
βββ .astro/ # generated
βββ dist/ # generated by astro build
βββ astro.config.mjs
βββ package.json
βββ tsconfig.json
A fresh project from the minimal template has only public/, src/pages/index.astro and the three root config files. The rest gets added as the site grows.
src/pages: routes
Every .astro, .md, .mdx (with the MDX integration), .html, .js or .ts file in src/pages/ becomes a route, and its path is its URL:
| File | URL |
|---|---|
src/pages/index.astro |
/ |
src/pages/about.astro |
/about/ |
src/pages/blog/index.astro |
/blog/ |
src/pages/blog/[id].astro |
/blog/first-post/, /blog/second-post/ |
src/pages/docs/[...path].astro |
/docs/a/, /docs/a/b/c/ |
src/pages/rss.xml.js |
/rss.xml |
src/pages/404.astro |
the not-found page |
Square brackets mark dynamic segments; [...rest] matches any depth. .js and .ts files in src/pages/ are endpoints that return a Response, which is how you build RSS feeds, JSON files or API routes. Files and folders whose names start with an underscore, like _draft.astro or _utils/, are ignored by the router, so you can keep helpers next to the pages that use them.
The Astro routing guide covers priority rules, pagination and redirects in detail.
src/components and src/layouts
These two folders are conventions. Astro doesnβt treat them differently from any other folder; they just make the project readable.
src/components/holds reusable pieces: headers, cards, buttons, and framework components (.jsx,.tsx,.vue,.svelte) if you use any.src/layouts/holds components that wrap whole pages: the<html>,<head>and shared chrome, with a<slot />for page content.
A layout is just a component that a page renders at the top level:
---
// src/layouts/BaseLayout.astro
import Header from '../components/Header.astro';
import '../styles/global.css';
interface Props {
title: string;
}
const { title } = Astro.props;
---
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>{title}</title>
</head>
<body>
<Header />
<slot />
</body>
</html>
On larger sites, group components by feature (components/blog/, components/pricing/) rather than by type. Astro doesnβt care, and it keeps related files together.
src/content and src/content.config.ts
Content collections are how Astro manages Markdown, MDX, JSON and YAML content with type-checked frontmatter. Since Astro 5, collections are defined in src/content.config.ts, and each one names a loader:
// src/content.config.ts
import { defineCollection } from 'astro:content';
import { glob, file } from 'astro/loaders';
import { z } from 'astro/zod';
const blog = defineCollection({
loader: glob({ pattern: '**/*.{md,mdx}', base: './src/content/blog' }),
schema: z.object({
title: z.string(),
pubDate: z.coerce.date(),
draft: z.boolean().default(false),
}),
});
const authors = defineCollection({
loader: file('src/data/authors.json'),
schema: z.object({ name: z.string(), url: z.string().url() }),
});
export const collections = { blog, authors };
Because the base path is explicit, the content folder can live anywhere. src/content/ is the common choice, but src/data/ or a folder outside src works too. The old location, src/content/config.ts without loaders, only works in Astro 7 behind the temporary legacy.collectionsBackwardsCompat flag. Our Content Collections guide covers queries and rendering.
src/assets and src/styles
src/assets/ is the conventional home for images you want Astro to optimize. Import them and pass them to <Image />:
---
// src/pages/index.astro
import { Image } from 'astro:assets';
import hero from '../assets/hero.jpg';
---
<Image src={hero} alt="Studio desk with a laptop" width={1200} />
Astro reads the dimensions, converts the image to a modern format and adds width, height, loading and decoding attributes, which prevents layout shift.
src/styles/ holds global CSS. Import a stylesheet in a layout and Astro bundles it into every page that uses that layout. Styles inside a componentβs <style> tag are scoped to that component, so most CSS can live next to the markup it styles.
public: files served as-is
Everything in public/ is copied to the root of the build output without processing:
public/favicon.svgis served at/favicon.svg.public/robots.txtis served at/robots.txt.public/fonts/inter.woff2is served at/fonts/inter.woff2.
Use public/ for files that need a fixed URL or must not be renamed: favicons, robots.txt, verification files, _headers or _redirects for your host, and downloads. Donβt put images there that you want optimized; images in public/ get no resizing, format conversion or hashed file names. The robots.txt tip shows both the static and generated approach.
Other reserved paths in src
A few file names in src/ have meaning to Astro. You only create them when you need the feature:
| Path | Purpose |
|---|---|
src/middleware.ts |
Middleware that runs on every request (or every prerendered page at build time) |
src/actions/index.ts |
Type-safe server functions for forms and client calls |
src/live.config.ts |
Live content collections fetched at request time |
src/fetch.ts |
New in Astro 7: a custom fetch entrypoint for the request pipeline, renamable with the fetchFile option |
The src/ folder itself can be renamed with the srcDir config option, and public/ with publicDir, but thereβs rarely a reason to.
Root config files
astro.config.mjs
The projectβs configuration: site, output, trailingSlash, integrations, adapters, image and Markdown options. Add // @ts-check on the first line for editor autocompletion:
// astro.config.mjs
// @ts-check
import { defineConfig } from 'astro/config';
import sitemap from '@astrojs/sitemap';
export default defineConfig({
site: 'https://www.example.com',
integrations: [sitemap()],
});
astro.config.ts and .mts also work. The astro.config.mjs cheatsheet lists every option youβre likely to set, and our astro.config docs page shows the settings Komal themes use.
tsconfig.json
Astro projects extend one of the presets shipped with Astro (base, strict or strictest). The include array pulls in .astro/types.d.ts, which is how editors learn the types of astro:content and your collections.
{
"extends": "astro/tsconfigs/strict",
"include": [".astro/types.d.ts", "**/*"],
"exclude": ["dist"]
}
package.json
Scripts for astro dev, astro build and astro preview, plus your dependencies. Integrations added with npx astro add show up here and in astro.config.mjs at the same time.
Generated folders: .astro, dist, node_modules
.astro/is written byastro dev,astro buildandastro sync. It holds generated types and the content layerβs data store. Delete it if types look stale; it will be regenerated.dist/is the build output: HTML, CSS, JavaScript and assets. This is what you deploy for a static site.node_modules/holds installed packages.
All three belong in .gitignore. The starter templates add them for you.
How Komal themes organise an Astro project
Our themes follow the same structure, with content in collections, shared chrome in layouts and page sections as components, so anything you learn here applies directly. The folder structure docs page maps each folder in a theme to what youβd edit, and you can browse Astro themes to see the structure in a finished site. For background on how pages are rendered, read What is Astro, and for the official reference, see the Astro project structure docs.
FAQ
What folders are required in an Astro project?
Only src/pages/ is required, because it is where routes come from. A working project also has package.json and usually astro.config.mjs, but public/, components/, layouts/ and content/ are all optional.
What is the difference between public and src/assets in Astro?
Files in public/ are copied to the build output unchanged and keep their exact URL. Images in src/assets/ are imported, which lets Astro resize them, convert formats, add dimensions and give them hashed file names for long-term caching.
Where do I put utility functions in an Astro project?
Anywhere outside src/pages/. A common choice is src/lib/ or src/utils/. Inside src/pages/, prefix the folder with an underscore, like _utils/, so the router ignores it.
Should I commit the .astro folder to git?
No. .astro/ is regenerated on every astro dev, astro build or astro sync run. Add it to .gitignore along with dist/ and node_modules/.
Can I rename the src folder in Astro?
Yes. Set srcDir in astro.config.mjs, for example srcDir: './app'. Pages then live in app/pages/. Most projects keep the default so tutorials and integrations match.