Most Astro config files only need a handful of options, but a few that people skip, such as site, trailingSlash, build.format, image.layout, prefetch and env.schema, change SEO, performance and reliability more than any integration you could add. This list covers the astro.config.mjs options worth knowing in Astro 7, what each one does, and when to change the default.
Every option below was checked against the Astro 7 configuration types. For the full reference, keep the official configuration reference open alongside this page.
Key Takeaways
- Set
sitein every project; sitemaps, canonicals, RSS andAstro.sitedepend on it.- Make
trailingSlashandbuild.formatagree so dev, build and links all use one URL shape.image.layout,prefetchandfontsgive real performance wins for a few lines of config.env.schemavalidates environment variables at startup and types them.- Astro 7 changed the
compressHTMLdefault to'jsx'; check inline formatting after upgrading.
The Astro config file in one minute
astro.config.mjs sits in the project root and exports defineConfig(). defineConfig adds no behavior; it gives your editor types and autocompletion. The file runs in Node.js at startup, so you can import packages and read process.env, but you cannot use import.meta.env here.
// astro.config.mjs
import { defineConfig } from 'astro/config';
export default defineConfig({
site: 'https://www.example.com',
integrations: [],
});
If you are new to where this file sits, the Astro config docs and the Astro config cheatsheet cover the basics. The rest of this article is about the options worth knowing beyond them.
URL and routing options
1. site
The production origin, such as https://www.example.com. Without it, @astrojs/sitemap writes nothing, RSS feeds cannot build absolute links, and Astro.site is undefined. Set it on day one, with the exact www or non-www form you want indexed.
2. base
A path prefix for sites served from a subfolder, like /docs on GitHub Pages project sites. When you set it, every internal link and asset path must account for it; use import.meta.env.BASE_URL when building URLs by hand.
3. trailingSlash
'ignore' (default), 'always' or 'never'. It controls how the dev server and on-demand routes match URLs. Choosing 'always' or 'never' stops two URLs serving the same page, which is a common duplicate-content problem. The trailing slash tip explains how it interacts with hosts.
4. build.format
'directory' (default) writes about/index.html; 'file' writes about.html; 'preserve' mirrors your source files. Pair it with trailingSlash: directory with 'always', file with 'never'. That way Astro.url and your links look the same in dev and in the build.
// astro.config.mjs
export default defineConfig({
site: 'https://www.example.com',
trailingSlash: 'always',
build: { format: 'directory' },
});
5. redirects
A map of old paths to new ones, including dynamic routes:
// astro.config.mjs
export default defineConfig({
redirects: {
'/old-about': '/about/',
'/blog/[...slug]': '/articles/[...slug]',
'/news': { status: 302, destination: 'https://example.com/news' },
},
});
On a static site without an adapter, Astro writes a small HTML page with a meta refresh tag. With an adapter, it sends real 301 or 308 responses. More in the Astro redirects tip.
6. prerenderConflictBehavior
When two routes produce the same URL, Astro warns and builds the higher-priority route. Set this to 'error' to fail the build instead. On a large site with dynamic routes from a CMS, silent conflicts are worth catching.
Output and server options
7. output and adapter
output defaults to 'static'. Pages that need a server opt in with export const prerender = false, which requires an adapter. Switch to output: 'server' only when most pages render on demand. The adapters guide covers which adapter fits each host.
8. server.port, server.host and server.allowedHosts
server.port changes the dev port from 4321. server.host: true exposes the dev server on your network, which is handy for testing on a phone. If you reach the dev server through a tunnel or a custom hostname, list it in server.allowedHosts, or set it to true to accept any host.
9. security.checkOrigin
On by default. For on-demand pages, Astro rejects POST, PUT, PATCH and DELETE form submissions whose Origin header does not match the site, with a 403. That is cross-site request forgery protection for free. Leave it on unless a trusted external service posts form-encoded data to your routes.
10. security.csp
Off by default. When enabled, Astro hashes its own scripts and styles and adds a Content Security Policy <meta> tag to each page. It does not work in astro dev, and it does not yet support <ClientRouter /> or Shiki’s inline styles, so test with astro build and astro preview. For sites without those features, it is one of the cheapest security upgrades available.
Performance options
11. image.layout and image.responsiveStyles
Set a default responsive layout once instead of on every <Image />:
// astro.config.mjs
export default defineConfig({
image: {
layout: 'constrained',
responsiveStyles: true,
domains: ['images.example-cms.com'],
},
});
Every image then gets srcset and sizes automatically. image.domains and image.remotePatterns list the remote hosts Astro may optimize. See the Astro image component guide for the rest.
12. prefetch
prefetch: true adds Astro’s prefetch script, and links with data-astro-prefetch start loading on hover. Set prefetchAll: true to prefetch every link, and defaultStrategy to 'tap', 'hover', 'viewport' or 'load'. Prefetching is on by default for pages that use <ClientRouter />. Details in the prefetch tip.
13. fonts
The Fonts API, stable since Astro 6, downloads and self-hosts fonts from Google, Fontsource and other providers, and generates metric-matched fallbacks. A five-line entry replaces a third-party font link and a lot of hand-written @font-face rules. The Astro fonts guide shows the setup.
14. build.inlineStylesheets
'auto' (default) inlines small stylesheets into the HTML and links larger ones. 'always' removes render-blocking CSS requests at the cost of caching; 'never' does the opposite. Most sites should keep 'auto'.
15. compressHTML
Since Astro 7, the default is 'jsx', which applies JSX-style whitespace rules: whitespace and line breaks around elements are removed, as in React. Set true for the older lossless whitespace removal, or false to keep all whitespace. If inline elements such as links in a sentence lost their spaces after upgrading, this is the setting to look at.
Developer experience options
16. env.schema
Declare your environment variables, their types and where they may be used:
// astro.config.mjs
import { defineConfig, envField } from 'astro/config';
export default defineConfig({
env: {
schema: {
PUBLIC_SITE_NAME: envField.string({ context: 'client', access: 'public', default: 'Example' }),
NEWSLETTER_API_KEY: envField.string({ context: 'server', access: 'secret' }),
RESULTS_PER_PAGE: envField.number({ context: 'server', access: 'public', default: 10 }),
},
},
});
Astro validates them when the dev server or build starts, types them, and stops secret values leaking into client code. Import them from astro:env/client or astro:env/server. The environment variables guide covers it in full.
17. devToolbar
devToolbar.enabled and devToolbar.placement control the toolbar that appears in astro dev. Before disabling it for the whole team, consider npx astro preferences disable devToolbar, which only affects you. The Astro dev toolbar guide covers its audits.
18. markdown.processor
Astro 7 renders .md files with Sätteri, Astro’s native Markdown pipeline, by default. The older top-level markdown.remarkPlugins, markdown.rehypePlugins, gfm and smartypants options are deprecated. To keep using remark and rehype plugins, install @astrojs/markdown-remark and pass them through unified():
// astro.config.mjs
import { defineConfig } from 'astro/config';
import { unified } from '@astrojs/markdown-remark';
import remarkToc from 'remark-toc';
export default defineConfig({
markdown: {
processor: unified({ remarkPlugins: [remarkToc] }),
},
});
MDX inherits plugins configured this way. If you are upgrading an older project, move plugins here before the deprecated options are removed.
Experimental flags worth watching
Experimental options can change in any minor release, so read their docs before turning them on in production. Three are worth knowing about in Astro 7:
experimental.incrementalBuild: skips unchanged pages on later builds when routes return acacheKeyfromgetStaticPaths(). Large content sites gain the most.experimental.clientPrerender: uses the browser’s Speculation Rules API to fully prerender prefetched pages, not just download them.experimental.svgOptimizer: optimizes imported SVG files at build time.
Astro 7.4 added more experimental work, including parallel prerendering and custom redirect pages, plus an astro build --timings flag that prints where build time goes. Check the Astro 7 release notes before enabling any of them.
A sensible starting config
Put together, a production-ready config for a static content site is still short:
// astro.config.mjs
import { defineConfig } from 'astro/config';
import sitemap from '@astrojs/sitemap';
export default defineConfig({
site: 'https://www.example.com',
trailingSlash: 'always',
build: { format: 'directory' },
prerenderConflictBehavior: 'error',
image: { layout: 'constrained', responsiveStyles: true },
prefetch: true,
integrations: [sitemap()],
});
That covers URLs, images, prefetching and a sitemap. Add fonts, env.schema or an adapter when the project needs them, not before. If you want to see config like this in a working site, every one of the Astro themes in the catalog ships a complete astro.config.mjs you can read and adapt.
FAQ
Where is the Astro config file?
In the project root, named astro.config.mjs. Astro also accepts astro.config.js, astro.config.ts and astro.config.mts.
Can I use environment variables in astro.config.mjs?
Yes, through process.env or Vite’s loadEnv() helper. import.meta.env is not available in the config file, because the config loads before Vite processes your project.
What is the most important Astro config option?
site. Sitemaps, RSS feeds, canonical URLs, Open Graph tags and Astro.site all need the production origin, and several integrations silently skip their work without it.
What changed in the Astro 7 config?
compressHTML now defaults to 'jsx', Sätteri is the default Markdown processor, and the top-level remark and rehype options are deprecated in favor of markdown.processor. Astro 7 also added options such as fetchFile and routeRules.
Should I use TypeScript for the Astro config?
You can name it astro.config.ts and Astro will load it. Most projects keep .mjs, which still gets full type checking in the editor through defineConfig().