Components and Pages
Astro components compile to static HTML by default; only components with client directives run JS in the browser.
Astro components
- Syntax:
.astrofiles have a frontmatter script (island-local TS/JS) + template + styles. - Props: declare with
export const {prop}; supports typing. - Slots:
<slot />default,<slot name="xxx" />named slots. - Scoped styles:
<style>scopes to the component; use:globalto extend.
Page routing
- File routing:
src/pages/xxx.astro→/xxx,index.astro→/. - Dynamic routes:
[id].astroandAstro.params.id. - Nested routes: folder depth maps to path depth.
- Redirect/alias: return
Astro.redirect()ingetStaticPathsor page logic.
UI frameworks (Islands)
- Import Vue/React/Svelte/Preact components directly.
- Client directives:
client:load/client:idle/client:visible/client:media/client:only="vue"etc. - Prefer delayed/on-demand directives for interactive pieces to avoid loading all JS.
Layout system
- Create
src/layouts/BaseLayout.astro; wrap pages with<BaseLayout>. - Layouts can nest and pass props (title, description, SEO meta).
- Shared UI (nav, footer, theme switch) lives in layouts.
Best practices
- Minimal JS: default no JS; add client directives only where interaction is needed.
- Split islands: break interactions into small islands to reduce hydration cost.
- Type safety: type props and
getStaticPathsresults. - Asset optimization: use built-in
<Image />/<Picture />or@astrojs/image. - Error handling: catch async errors in pages/layouts and show friendly fallbacks.