Astro SSG
In the larger context of the project's tech stack architecture, Astro operates as "The Builder"—the underlying engine that converts raw component files and content collections into a production-ready website.
Beyond simple layout rendering, Astro’s static-site architecture influences everything from local development to multi-stage containerization, reverse-proxy routing, and CI/CD pipelines.
-
Architectural Role: "The Builder"
-
Static Compiler: Developers write code in
.astrotemplate files (components containing a server-side JavaScript "code fence" and HTML markup). Astro processes these templates, evaluates the server JavaScript backstage, and compiles the code down into static HTML, CSS, and client-side JavaScript assets. - Framework-Free Delivery: Unlike heavy client-side JavaScript frameworks, Astro strips out runtime framework overhead during compilation, generating plain HTML files that browsers can render natively.
- Development vs. Production Paradigm
Astro exhibits two distinct behaviors depending on the execution environment:
- Development Mode (npm run dev): Includes a lightweight, built-in development server (running at
localhost:4321) that watches source files, compiles components on the fly, and provides hot-module reloading during local development. - Production Mode (npm run build): Astro strips away its internal development server entirely. It acts purely as a factory/compiler, spitting out compiled static assets into the
/app/distfolder and exiting.
- Containerization Architecture (Multi-Stage Docker Builds)
Because Astro outputs pure static files without a built-in production HTTP server—unlike dynamic SSR frameworks like Next.js in standalone mode [41–43, 284, 285]—its container architecture requires an external web server to deliver assets:
- Stage 1 (builder): Uses a Node.js environment (
node:20-alpine) to install dependencies viapnpm/npmand executeastro build(npm run build). This produces static assets in/app/dist. - Stage 2 (runner): Copies only the compiled
/app/distdirectory into a lightweight web server container likecaddy:alpineornginx:alpine. - Resource Efficiency: All heavy Node.js tools,
node_modules, and source files are discarded. The final production container image remains small (often under 20MB) and secure.
- Infrastructure & Network Integration
Astro's static container integrates directly into the broader homelab DevOps architecture:
- Isolated Network Bridging: The production Caddy/Nginx container exposes port 80 internally and attaches to a shared
frontendDocker bridge network without exposing host ports directly to the host machine. - Reverse Proxy & DNS Resolution: A central Caddy reverse proxy receives incoming traffic (routed safely through Cloudflare Tunnels) and proxies domain requests (
http://ritikkumar.dev) directly toastro_portfolio:80using Docker DNS. - Automated CI/CD Pipeline: A GitHub Actions workflow (
.github/workflows/deploy.yml) uses cloud runners (ubuntu-latest) to build the Docker image and push it to GitHub Container Registry (GHCR). A self-hosted runner on the homelab server then executesdocker compose pullanddocker compose up -dto achieve zero-downtime deployments.
-
Internal Component Architecture
-
Layout Wrappers & Slots: Astro avoids repeating HTML boilerplate across pages using
BaseLayout.astroas a master wrapper. Pages wrap their markup in<BaseLayout>, and Astro injects that page-specific content into a<slot />placeholder. - Component Communication (Props): Components pass properties (props) down the tree—for instance, passing
sideBarActiveItemID="home"fromindex.astrothroughBaseLayoutdown toSideBarMenu.astro, where client-side JavaScript highlights the matching active menu tab. - Content Collections API: Markdown files stored in
src/content/(such as blog posts) are managed via Astro's strongly typed Content Collections API (getCollection), which automatically compiles articles into formatted web pages and RSS feeds.