Project Structure and Configuration
By Syncfusion®, Inc. · Last updated
Every folder and file in an Astro project, public/ vs src/, astro.config.mjs and defineConfig(), common options, integrations and astro add, using JavaScript and TypeScript together, astro check, editor setup and the dev toolbar.
Project Structure and Configuration
- Every Astro project has the same basic folders and files.
- Each one has a clear job.
- When you know the jobs, you always know where a new file should go.
- In this lesson, you will meet every part of a project and change its settings.
Simple lesson: Astro decides a lot from where a file lives, so the right folder matters.
A Real-World Example
- Asha's Dev Journal is growing.
- She now has a logo, a header used on every page and a small helper that formats dates.
- She wants to set her real web address, so links shared online are correct.
- Each of these things belongs in a different place in the project.
Press ▶ Run. Then open the files in the explorer and see where each one lives.
What happened:
- The page is in
src/pages, so it became the home page. - The header is in
src/components, so you can reuse it on any page. - The helper is a plain
.tsfile insrc/lib. - The logo is in
public, and the page loads it as/logo.svg.
What You Will Learn
In this lesson, you will learn:
- The job of every folder in an Astro project.
- The difference between
public/andsrc/. - What
package.json,tsconfig.jsonand the.astrofolder do. - How to change settings in
astro.config.mjs. - What integrations are and how to add one.
- How to use JavaScript and TypeScript files together.
- How to set up your editor and use the dev toolbar.
The Folders of a Project
src/pages
- Pages folder =
src/pages. Every.astrofile here becomes a page. - It is the only folder Astro needs.
- Sub-folders make longer addresses:
src/pages/blog/first.astrobecomes/blog/first.
Press ▶ Run, then open the page list in the address bar. Each file became one address.
index.astroin a folder is that folder's main page:src/pages/blog/index.astrois/blog.
src/components
- Component = a reusable piece of a page, such as a header, a card or a button.
- Put components in
src/components. - Files here do not become pages. You import them into pages.
src/layouts
- Layout = a component that gives pages a shared frame, such as the
<html>,<head>and header. - Put them in
src/layouts. You will build one in Layouts and the Document Head.
Other Folders in src
src/stylesis a common home for global CSS files.src/liborsrc/utilsis a common home for helper.tsor.jsfiles.src/content.config.tsdescribes your content collections. You will add it in Content Collections.- Only
src/pagesandsrc/content.config.tshave a fixed name. The other folder names are just good habits.
Simple lesson: pages go in src/pages, reusable pieces in src/components, frames in src/layouts.
public vs src
Watch the animation. Files in src/ go through Astro's build. Files in public/ are copied as they are.
Animation: files from src go through astro build and come out changed. Files from public skip the build and are copied unchanged to dist.
src/ .astro, .ts, .css
public/ logo.svg, robots.txt
astro build compiles and shrinks copied as they are
dist/ the finished site
src files are built. public files are copied unchanged.
What Goes in public/
- public/ = files copied to the finished site without any change.
- Use it for
favicon.svg,robots.txt, fonts and files that must keep their exact name. - A file at
public/logo.svgis served at/logo.svg. - Do not write
/publicin the path. The wordpublicis not part of the address.
What Goes in src/
- src/ = your source code. Astro processes every file here.
.astroand.tsfiles are compiled.- CSS is combined and made smaller.
- Images in
src/can be resized and optimised. You will learn this in Images and Fonts.
Ask one question: "Must this file stay exactly as it is?" If yes, put it in public/. If Astro should process it, put it in src/.
Simple lesson: public/ is copied, src/ is built.
The Files at the Top
package.json
- package.json = the project's ID card for npm.
- It lists the packages the project needs, such as
astro. - Its
scriptsgive short names to commands:dev,buildandpreview. npm run devlooks updevhere and runsastro dev.
tsconfig.json
- tsconfig.json = settings for TypeScript and for your editor.
- Every Astro project has one, even a JavaScript project.
- It starts with
"extends": "astro/tsconfigs/strict". That means "use Astro's strict settings". - Your editor reads it to give you hints and warnings.
The .astro Folder
- Astro creates a hidden
.astrofolder when you run it. - It holds generated types: files Astro writes for your editor.
- Never edit it. Astro writes it again whenever it needs to.
- It is listed in
.gitignore, so Git does not save it.
node_modules and dist
node_modules/holds the downloaded packages.npm installfills it.dist/holds the built site.npm run buildfills it.- Never edit either one by hand. Both are made by commands.
Simple lesson: you edit src/, public/ and the config files. Commands make everything else.
Configuring Astro
astro.config.mjs and defineConfig()
- astro.config.mjs = Astro's settings file. It sits at the top of the project.
- A new project's file is almost empty.
- You list your settings inside
defineConfig({ ... }). defineConfig()changes nothing by itself. It lets your editor suggest the right option names.
// @ts-check
import { defineConfig } from "astro/config";
export default defineConfig({
site: "https://asha.dev",
server: { port: 3000 },
});
// @ts-checkasks your editor to check this file, even though it is JavaScript.
Common Options
site= your site's real web address. Astro uses it to build full links.base= a sub-folder the site lives in, such as/journal, when it is not at the root of the domain.integrations= a list of add-ons. More on these below.server= dev server settings, such asport.vite= settings passed to Vite, the build tool Astro uses inside.
Change site in the config below and press ▶ Run. The page reads it with Astro.site.
- Without
site, Astro cannot know your domain. Full links, sitemaps and RSS feeds need it. Astro.siteis aURLobject, orundefinedwhensiteis not set.
TypeScript Config File
- [TS] You can name the file
astro.config.tsinstead. It works the same way. - [JS] Keep
astro.config.mjs. The// @ts-checkline still gives you hints.
Finding Every Option
- The Configuration Reference in the Astro docs lists every option.
- Each entry shows the type, the default value and an example.
- Look there before you search a blog. The docs match your Astro version.
Simple lesson: settings live in defineConfig(). Set site early, because many features need it.
Working with Integrations
What Integrations Are
- Integration = an add-on package that gives Astro a new skill.
Examples:
@astrojs/reactlets you use React components.@astrojs/mdxlets you write pages in MDX.@astrojs/sitemapmakes a sitemap file for search engines.
- An integration is added to the
integrationslist in the config.
Adding an Integration with astro add
The easy way is one command:
npx astro add sitemap
It does three things for you:
- Installs the package with npm.
- Imports it at the top of
astro.config.mjs. - Adds
sitemap()to theintegrationslist.
It shows you the change and asks before it saves it.
Manual Installation
You can do the same steps by hand:
npm install @astrojs/sitemap
Then edit the config:
import { defineConfig } from "astro/config";
import sitemap from "@astrojs/sitemap";
export default defineConfig({
site: "https://asha.dev",
integrations: [sitemap()],
});
Integration Options
- Many integrations take options inside the brackets.
- Example:
sitemap({ filter: (page) => !page.includes("/drafts/") })leaves drafts out of the sitemap. - Each integration's page in the docs lists its options.
Press ▶ Run. The terminal lists the integrations that the config adds.
- On your computer,
npm run buildwould now also writesitemap-index.xmltodist/. - In this lab, integrations are named in the terminal, but they do not run. The lab cannot install packages.
Official Integrations
The Astro team keeps these, and many more:
- UI frameworks: React, Preact, Vue, Svelte, Solid, Alpine.js.
- Adapters: Node, Vercel, Netlify, Cloudflare. You meet them in On-Demand and Mixed Rendering.
- Other: MDX, Markdoc, sitemap, partytown.
Tailwind CSS is no longer an integration. npx astro add tailwind adds it as a Vite plugin instead. You will add it in Styling.
Simple lesson: use npx astro add <name>. It installs the package and edits the config for you.
Using JavaScript and TypeScript Together
Mixing .js and .ts Files
- One project can hold both
.jsand.tsfiles. - An
.astrofile can import either one. - A
.tsfile can import a.jsfile, and the other way round. - You can start in JavaScript and add types later, one file at a time.
The lab below has one .js helper and one .ts helper. The page uses both.
greet.jsis plain JavaScript.readingTime.tssays its input is astringand its result is anumber.- Astro removes the types when it builds. The browser never sees them.
[JS] Editor Hints Without TypeScript
- Your editor still reads
tsconfig.json, so it can warn about some mistakes in.jsfiles. - You can add JSDoc comments to describe values without writing TypeScript:
/**
* @param {string} name
* @returns {string}
*/
export function greet(name) {
return "Welcome back, " + name + "!";
}
- JSDoc = comments that start with
/**and describe a function's inputs and output. - It is optional. Add it when a function is used in many places.
[TS] Checking Types with astro check
astro devandastro builddo not check your types. They only remove them.- A type mistake can still reach your site.
- Run
npx astro checkto check every.astroand.tsfile. - The first time, it offers to install
@astrojs/checkandtypescript. Say yes. - Add it to your build script so mistakes stop the build:
"build": "astro check && astro build"
A site that builds is not proof that its types are right. Only astro check checks them.
Simple lesson: mix .js and .ts freely. If you use types, run astro check.
Editor Setup and the Dev Toolbar
The Astro VS Code Extension
- Install Astro from the VS Code Extensions panel. It is published by the Astro team.
- It adds colours (syntax highlighting) for
.astrofiles. - It adds IntelliSense: suggestions, errors and "go to definition" as you type.
- Other editors, such as WebStorm, Zed and Neovim, have Astro support too.
The Prettier Plugin
- Prettier = a tool that formats your code neatly.
- It needs the
prettier-plugin-astropackage to understand.astrofiles.
npm install --save-dev prettier prettier-plugin-astro
The Dev Toolbar
- When
npm run devis running, a small toolbar appears at the bottom of your page. - It only shows in development. Visitors never see it.
Its built-in apps:
- Inspect highlights the interactive islands on the page.
- Audit checks for common accessibility and speed problems.
- Settings lets you hide or move the toolbar.
- Turn it off for the whole project in the config:
devToolbar: { enabled: false }.
Simple lesson: install the Astro extension first. Use the dev toolbar's Audit to catch problems early.
TL;DR
- src/pages = every
.astrofile here becomes a page. - src/components = reusable pieces. src/layouts = page frames.
- public/ = copied as it is. Served from the site root, without
/public. - package.json = packages and scripts. tsconfig.json = editor and TypeScript settings.
- .astro/, node_modules/, dist/ = made by commands. Never edit them.
astro.config.mjs = settings inside
defineConfig():sitefor full links,basefor a sub-folder,integrationsfor add-ons,server.portfor the dev port.
- Integration = add-on. Add it with
npx astro add <name>. - astro check = the only command that checks your types.
Common mistakes
| Mistake | Wrong idea | Correct idea | What goes wrong |
|---|---|---|---|
Writing /public/logo.svg | The folder name is part of the path | Use /logo.svg | The image does not load |
Putting a component in src/pages | Any folder works for components | Components go in src/components | It becomes a page of its own |
Editing files in .astro/ or dist/ | They are normal source files | Edit src/, then run the command again | Your change is lost on the next run |
Forgetting site | Astro knows the domain | Set site in the config | Full links, sitemap and RSS are wrong or missing |
| Adding an integration to the config only | Importing is enough | Install the package too, or use astro add | The build says it cannot find the package |
Trusting astro build for types | A build checks types | Run astro check | Type mistakes reach the live site |
Check Yourself
Exercises
What to Remember
- Pages in
src/pages, components insrc/components, layouts insrc/layouts. public/is copied as it is and served from the root.- Settings go in
astro.config.mjsinsidedefineConfig(). - Set
siteearly. Add integrations withnpx astro add. .jsand.tsfiles work together. Onlyastro checkchecks types.
Next: Component Basics, where you build Asha's Header, Footer and PostCard.