Astro Site Builder
Build production-ready static websites with Astro 5. Covers portfolios, ebook platforms, blogs, marketing sites, and documentation.
When To Use
- User asks to build an Astro site, portfolio, or blog
- User wants to deploy to Cloudflare Pages
- User needs an ebook/documentation site with sidebar navigation
- User wants SEO-optimized static site with sitemap, meta tags, structured data
- User mentions "Astro", "Cloudflare Pages", "JAMstack static site"
Quick Start
# Create project
npm create astro@latest my-site -- --template minimal
cd my-site
# Core dependencies
npm install @astrojs/sitemap
# Build & preview
npm run build # output to dist/
npm run preview # local preview server
Project Structure
project/
├── astro.config.mjs
├── package.json
├── tsconfig.json
├── public/
│ ├── robots.txt
│ └── _headers # Cloudflare security headers
├── src/
│ ├── layouts/
│ │ ├── BaseLayout.astro # Site-wide shell (header, footer, SEO)
│ │ └── EbookLayout.astro # Content layout (sidebar, progress bar)
│ └── pages/
│ ├── index.astro # Homepage
│ ├── blog.astro
│ └── ebook/
│ ├── index.astro # Ebook listing
│ └── video-editing/
│ └── index.astro # Single ebook page
└── dist/ # Build output (deploy this)
Astro Config (Cloudflare-optimized)
// astro.config.mjs
import { defineConfig } from 'astro/config';
import sitemap from '@astrojs/sitemap';
export default defineConfig({
site: 'https://yourdomain.com',
integrations: [sitemap()],
prefetch: true,
build: {
inlineStylesheets: 'auto', // Inline critical CSS for faster FCP
},
});
SEO Pattern (BaseLayout)
Every page gets full SEO via props:
---
export interface Props {
title: string;
description?: string;
canonical?: string;
image?: string;
type?: string; // 'website' | 'article'
}
---
Include in <head>:
<title>(format:Page Title | Site Name)<meta name="description"><link rel="canonical">- Open Graph tags (og:type, og:title, og:description, og:image, og:url)
- Twitter card meta tags
- JSON-LD structured data (Person, Organization, Article as appropriate)
<meta name="robots" content="index, follow">
EbookLayout Pattern
For long-form content with sidebar navigation. See references/ebook-layout-css.md for complete CSS.
Features
- Sidebar nav — fixed left sidebar with chapter links, active state tracking via scroll position
- Progress bar — fixed top bar showing reading progress (0-100%), gradient fill (teal to indigo)
- Back to top button — fixed bottom-right, shows after 400px scroll
- Author credit — avatar circle + name/role at bottom of content
- Code blocks — dark background (
#1e293b), gradient top accent bar, monospace font, horizontal scroll - Tables — gradient header, alternating row colors, hover effects, rounded border
- Callout boxes — 4 types: info (blue), warning (amber), tip (green), danger (red), each with SVG icon
- Print styles — hides sidebar/progress/nav, expands content, page-break rules
- Responsive — 3 breakpoints (960px sidebar hide, 600px compact, mobile stack)
CSS Strategy: is:global on <style slot="head">
EbookLayout CSS must use is:global so styles apply to slotted content from page components:
<style slot="head" is:global>
/* All classes here apply to slotted content */
.ebook-content h1 { ... }
.callout { ... }
.callout-info { ... }
</style>
Without is:global, classes like .callout-info defined in EbookLayout won't apply to elements in the page component that uses <EbookLayout>.
EbookLayout Props Interface
export interface Props {
title: string;
description?: string;
chapters: { slug: string; title: string; number: number }[];
currentSlug: string;
ebookTitle: string;
ebookDescription?: string;
author?: string; // defaults to 'Ahmad Asri Zalmi'
}
Content Page Pattern
Each ebook page defines chapters array and passes to EbookLayout:
---
import EbookLayout from '../../../layouts/EbookLayout.astro';
const chapters = [
{ slug: 'index', title: 'Pendahuluan', number: 1 },
{ slug: 'topic-a', title: 'Topic A', number: 2 },
];
---
<EbookLayout title="Page Title" description="SEO desc" chapters={chapters}
currentSlug="ebook-slug" ebookTitle="Ebook Title" ebookDescription="Subtitle" author="Author Name">
<h1 id="index">Page Title</h1>
<p class="ebook-subtitle">Subtitle text</p>
<div class="ebook-meta">
<!-- SVG user icon + author name + SVG calendar icon + year -->
</div>
<!-- chapters with h2 id matching slug -->
</EbookLayout>
Title/Header Block (MANDATORY)
<h1 id="index">Title</h1>
<p class="ebook-subtitle">Subtitle</p>
<div class="ebook-meta">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M20 21v-2a4 4 0 0 0-4-4H8a4 4 0 0 0-4 4v2"/><circle cx="12" cy="7" r="4"/></svg>
<span>Author Name</span>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" style="margin-left:0.5rem"><rect x="3" y="4" width="18" height="18" rx="2" ry="2"/><line x1="16" y1="2" x2="16" y2="6"/><line x1="8" y1="2" x2="8" y2="6"/><line x1="3" y1="10" x2="21" y2="10"/></svg>
<span>2026</span>
</div>
Callout Box HTML (4 types)
<!-- Info (blue #3b82f6) -->
<div class="callout callout-info">
<svg class="callout-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="10"/><line x1="12" y1="16" x2="12" y2="12"/><line x1="12" y1="8" x2="12.01" y2="8"/></svg>
<div class="callout-content"><strong>Title</strong><p>Content.</p></div>
</div>
<!-- Warning (orange #f59e0b) -->
<div class="callout callout-warning">
<svg class="callout-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M10.29 3.86L1.82 18a2 2 0 0 0 1.71 3h16.94a2 2 0 0 0 1.71-3L13.71 3.86a2 2 0 0 0-3.42 0z"/><line x1="12" y1="9" x2="12" y2="13"/><line x1="12" y1="17" x2="12.01" y2="17"/></svg>
<div class="callout-content"><strong>Title</strong><p>Content.</p></div>
</div>
<!-- Tip (green #22c55e) -->
<div class="callout callout-tip">
<svg class="callout-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M22 11.08V12a10 10 0 1 1-5.93-9.14"/><polyline points="22 4 12 14.01 9 11.01"/></svg>
<div class="callout-content"><strong>Title</strong><p>Content.</p></div>
</div>
<!-- Danger (red #ef4444) -->
<div class="callout callout-danger">
<svg class="callout-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="10"/><line x1="15" y1="9" x2="9" y2="15"/><line x1="9" y1="9" x2="15" y2="15"/></svg>
<div class="callout-content"><strong>Title</strong><p>Content.</p></div>
</div>
Resource Links Section (end of ebook)
<div class="resource-links">
<h3>Resources & Links</h3>
<ul>
<li>
<a href="https://..." target="_blank" rel="noopener">Link Text</a>
<span class="desc">Short description</span>
</li>
</ul>
</div>
Code Blocks with set:html
Define code strings in frontmatter, render with set:html:
---
const code_example = `# shell command
npm run build`;
---
<pre><code set:html={code_example}></code></pre>
Sidebar Active State (JS)
const navLinks = document.querySelectorAll('[data-nav-link]');
const headings = [];
navLinks.forEach(link => {
const id = link.getAttribute('href').slice(1);
const el = document.getElementById(id);
if (el) headings.push({ el, link });
});
const updateActive = () => {
let active = headings[0];
for (const h of headings) {
if (h.el.getBoundingClientRect().top <= 100) active = h;
}
navLinks.forEach(l => l.classList.remove('active'));
if (active) active.link.classList.add('active');
};
window.addEventListener('scroll', updateActive, { passive: true });
Callout Box HTML
<div class="callout callout-info">
<svg class="callout-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<circle cx="12" cy="12" r="10"/><line x1="12" y1="16" x2="12" y2="12"/><line x1="12" y1="8" x2="12.01" y2="8"/>
</svg>
<div class="callout-content">
<strong>Title</strong>
<p>Content text here.</p>
</div>
</div>
Import Path Rules
Astro uses Vite's file-based routing. Import paths are relative to the importing file:
| File location | Import layouts from |
|---|---|
src/pages/index.astro | ../layouts/BaseLayout.astro |
src/pages/blog.astro | ../layouts/BaseLayout.astro |
src/pages/ebook/index.astro | ../../layouts/BaseLayout.astro |
src/pages/ebook/video-editing/index.astro | ../../../layouts/BaseLayout.astro |
Count the directory depth from src/ to determine ../ segments.
Cloudflare Pages Deployment
Build Settings
| Setting | Value |
|---|---|
| Framework Preset | Astro |
| Build Command | npm run build |
| Build Output Directory | dist |
| Node.js Version | 18+ (set via env var NODE_VERSION) |
Security Headers (public/_headers)
/*
X-Frame-Options: DENY
X-Content-Type-Options: nosniff
Referrer-Policy: strict-origin-when-cross-origin
Permissions-Policy: camera=(), microphone=(), geolocation=()
/assets/*
Cache-Control: public, max-age=31536000, immutable
Redirects (public/_redirects)
/old-path /new-path 301
Wrangler CLI Deploy (direct upload)
# Build first
npm run build
# Deploy to Cloudflare Pages
CLOUDFLARE_API_TOKEN=cfut_xxx CLOUDFLARE_ACCOUNT_ID=xxx \
npx wrangler pages deploy dist --project-name=my-site --branch=main
# Preview deploy (non-main branch)
npx wrangler pages deploy dist --project-name=my-site --branch=staging
Standalone HTML Consistency
When standalone HTML ebook files live in public/ebook/ (bypassing Astro layouts), they need manual injection of shared headers, sidebars, color schemes, and author descriptions. See references/standalone-html-consistency.md for the full pattern including:
- Injected sticky header with logo + nav (inline CSS to avoid conflicts)
- Sidebar
top/heightadjustment when adding a header above fixed sidebars - Color scheme unification (CSS vars, rgba, gradients — all must be updated)
- Back-to-top button injection (CSS + HTML + JS)
- Patch tool JS nesting pitfall (always verify closing braces after patching)
- Author description consistency checklist
Pitfalls
See references/astro5-pitfalls.md for detailed gotchas. Summary:
- Curly braces in HTML —
{and}are JSX expressions. Useset:htmlfor code blocks. lightningcssnot bundled — Install separately or don't usecssMinify: 'lightningcss'.- Import path depth — Must match directory structure exactly.
- No emoji — Use inline SVG icons (user preference from ebook-generator skill).
set:htmlonly on void/self-closing or with slot — Cannot have children when usingset:html.- Astro scoped CSS — Classes in
<style>are scoped per component. Use<style is:global>or:global(.class)for shared classes. EbookLayout MUST use<style slot="head" is:global>for styles to apply to slotted content. data-astro-cidscope mismatch — A.containerclass in BaseLayout won't apply to elements in page components. Global classes needed:.container,.btn,.section,.grid-*, etc.- JS class toggling + scoped CSS = silent failure — JavaScript that adds/removes classes dynamically (e.g., IntersectionObserver adding
.fade-inon scroll) will NOT match Astro's scoped selectors. Astro scopes.fade-into.fade-in[data-astro-cid-xxxx], but JS adds the plain.fade-inclass. Symptom: sections stay invisible (opacity:0) forever. Fix: Either (a) use<style is:global>for animation classes, (b) use:global(.fade-in)in scoped styles, or (c) remove CSS animation classes entirely and let content render immediately. Option (c) is simplest for SSR sites where animations aren't critical. Seereferences/astro5-pitfalls.md#9 for full details. - Standalone HTML sidebar offset — When injecting a sticky header into standalone ebook HTML files, always adjust fixed sidebar
topfrom0to header height (~56px) and setheight: calc(100vh - 56px). Forgetting this causes sidebar content to hide behind the header.
Anti-Patterns
- Don't use
cssMinify: 'lightningcss'withoutnpm install lightningcss - Don't put
{variable}inside<pre><code>blocks — Astro parses as JSX - Don't use emoji for icons — use inline SVG
- Don't skip
robots.txtand sitemap for SEO - Don't forget
<meta name="viewport">for mobile - Don't hardcode the site URL — use
Astro.sitefrom config