The most common Astro mistakes beginners make are hydrating everything with client:load, expecting frontmatter variables to exist in browser scripts, being surprised that scoped styles don’t reach child components, putting images in public/ and expecting optimization, and following outdated tutorials that use removed APIs like Astro.glob(). Each has a short fix once you know how Astro’s server and browser halves are split.
Below are fifteen mistakes, grouped by area, with the symptom you’ll see and the fix for Astro 7.
Key Takeaways
- Frontmatter runs on the server at build time. Nothing in it exists in the browser unless you render it into HTML.
- Use the lightest
client:*directive that works, or a plain<script>, rather thanclient:loadeverywhere.- Scoped styles apply only to the component’s own markup. Use
:global()oris:globalfor slotted or child content.- Images you want optimized go in
src/and are imported; files inpublic/are copied as-is.- Check the Astro version of any tutorial. Content collections,
Astro.glob()and Zod imports changed in recent majors.
Server vs browser mistakes
1. Using frontmatter variables in a script
Symptom: ReferenceError: x is not defined in the browser console.
Frontmatter runs once on the server. A <script> runs later in the browser. They don’t share variables. Pass values through a data-* attribute instead:
---
// src/components/Greeting.astro
const name = 'Ana';
---
<p id="greeting" data-name={name}></p>
<script>
const el = document.getElementById('greeting')!;
el.textContent = `Hello, ${el.dataset.name}`;
</script>
The Astro script tag guide covers this and define:vars.
2. Touching window or document in frontmatter
Symptom: window is not defined during astro build.
The frontmatter runs in Node.js, where there’s no window. Move browser code into a <script> tag, or into a framework component’s mount hook.
3. Expecting fetch in frontmatter to run per visitor
On a static site, a fetch() in frontmatter runs once, at build time. Visitors see whatever the API returned then. If data must be fresh per request, render that route on demand with an adapter and export const prerender = false, or fetch from the browser. The rendering mode guide helps you choose.
4. Accessing component variables inside getStaticPaths
Symptom: a variable is undefined inside getStaticPaths(), or an error about Astro not being available.
getStaticPaths() runs in its own scope before the component. Put data inside the function or import it from a module. The getStaticPaths explainer shows both.
Islands and JavaScript mistakes
5. Forgetting a client directive
Symptom: a React, Vue or Svelte component renders but buttons do nothing.
Without a client:* directive, framework components render to static HTML only. Add one:
<Counter client:visible />
6. Using client:load for everything
The opposite mistake. client:load hydrates immediately, competing with the page’s own loading. Use client:visible for anything below the fold and client:idle for low-priority widgets. Better still, write simple toggles and menus as plain <script> tags, which ship no framework runtime at all. The client directive chooser has a decision table.
7. Passing functions as props to islands
Symptom: the prop is missing or the build warns about serialization.
Props to hydrated islands are serialized into the HTML. Functions and class instances can’t be. Move the handler inside the island, and pass data, not behavior.
8. Reaching for a framework for a single toggle
A dark mode switch, accordion or mobile menu doesn’t need React. A <button>, a few lines in a <script>, and a CSS class do the job with a fraction of the JavaScript. Native <details> and <dialog> cover even more without any script.
Styling mistakes
9. Scoped styles not reaching child components
Symptom: a style in a parent component doesn’t apply to markup rendered by a child component or passed through a slot.
Astro scopes <style> to the component’s own elements. To style content from elsewhere, scope it under one of your elements with :global():
---
// src/components/Prose.astro
---
<div class="prose"><slot /></div>
<style>
.prose :global(h2) {
margin-top: 2rem;
}
.prose :global(a) {
text-decoration: underline;
}
</style>
Use <style is:global> only for genuinely site-wide rules. The scoped vs global styles guide covers the details.
10. Losing the caller’s class
Symptom: <Card class="mt-4" /> has no effect.
A component has to accept and apply class itself. Destructure it as class: className and merge it with class:list={['card', className]}.
Content and assets mistakes
11. Following tutorials with removed APIs
Symptom: errors about Astro.glob, legacy content config, or getEntryBySlug.
Astro has changed in recent major versions:
| Old pattern | Current pattern |
|---|---|
Astro.glob('./posts/*.md') |
import.meta.glob() or a content collection |
src/content/config.ts without loaders |
src/content.config.ts with glob() or file() |
entry.slug, entry.render() |
entry.id, render(entry) |
getEntryBySlug() |
getEntry() |
import { z } from 'astro:content' |
import { z } from 'astro/zod' |
The Astro.glob removed fix and the legacy content config fix show the migrations.
12. Putting images in public and expecting optimization
Files in public/ are copied to the build untouched: no resizing, no WebP or AVIF, no width and height. For optimization, put images in src/ and import them:
---
// src/pages/index.astro
import { Image } from 'astro:assets';
import hero from '../assets/hero.jpg';
---
<Image src={hero} alt="Studio workspace with two monitors" widths={[640, 1280]} sizes="100vw" />
Keep public/ for files that need exact URLs: favicons, robots.txt, downloadable PDFs.
13. Skipping alt text
Symptom: a build error saying the image is missing a required alt property.
Astro’s <Image /> requires alt. Describe what the image shows, or use alt="" for purely decorative images. The missing alt fix has examples.
Config and deployment mistakes
14. Not setting site
Without site in astro.config.mjs, the sitemap integration writes nothing, Astro.site is undefined, and canonical and Open Graph URLs can’t be absolute. Set it to your production URL on day one.
15. Exposing secrets with PUBLIC_
Environment variables prefixed with PUBLIC_ are embedded in client code where anyone can read them. API keys and tokens must not use that prefix. Better, declare them with astro:env and mark them as secret server variables:
// astro.config.mjs
import { defineConfig, envField } from 'astro/config';
export default defineConfig({
env: {
schema: {
API_TOKEN: envField.string({ context: 'server', access: 'secret' }),
PUBLIC_SITE_NAME: envField.string({ context: 'client', access: 'public' }),
},
},
});
Astro will refuse to import a server secret into client code. The environment variables guide covers the rest.
A bonus mistake: judging speed in dev
astro dev serves unbundled code and the dev toolbar. Lighthouse scores there are always worse than production. Measure with npm run build && npm run preview or on the deployed site.
Another bonus: mixing trailing slash styles
Linking to /about in one place and /about/ in another creates duplicate URLs, redirects on some hosts and confusing canonicals. Pick one style, set trailingSlash in the config to match, and write every internal link the same way. The trailing slash tip shows which setting fits which host.
Avoiding mistakes from the start
Many of these mistakes come from starting with an empty folder and guessing at structure. Starting from a well-organized project, whether an official starter, a free community theme, or one of the Komal Astro themes, shows the patterns in working code: imported images, typed props, scoped styles, content collections and site already set. The folder structure docs explain where each piece lives, and the official Astro docs are the reference to check any tutorial against.
FAQ
Why is my Astro component not interactive?
Most likely it has no client:* directive. Framework components in Astro render to static HTML unless you add client:load, client:idle, client:visible or another directive. For .astro components, interactivity comes from a <script> tag instead.
Why don’t my styles apply to child components in Astro?
Astro scopes <style> blocks to the elements in that component. Markup from child components or slots doesn’t get the scope attribute. Wrap selectors with :global() under a local class, or move the styles into the child component.
Why are my images not optimized in Astro?
They’re probably in public/, which Astro copies without processing. Move them into src/, import them, and render them with <Image /> or <Picture /> from astro:assets to get resizing, modern formats and dimensions.
What replaced Astro.glob?
Astro.glob() was removed. Use content collections with the glob() loader for Markdown and MDX content, or Vite’s import.meta.glob() for importing groups of files directly.
Is Astro hard for beginners?
Not usually. Most beginner problems come from one idea: frontmatter runs on the server, scripts run in the browser. Once that clicks, the rest of the mistakes on this list are easy to spot and fix.