Astro Version โ
HaloLight Astro version is built on Astro 5, featuring Islands architecture with zero JS initial load and ultimate performance.
Live Preview: https://halolight-astro.h7ml.cn/
GitHub: https://github.com/halolight/halolight-astro
Tech Stack โ
| Technology | Version | Description |
|---|---|---|
| Astro | 5.x | Islands architecture framework |
| TypeScript | 5.x | Type safety |
| Tailwind CSS | 3.x | Atomic CSS |
| Vite | Built-in | Build tool |
| @astrojs/node | 9.x | Node.js adapter |
| Vitest | 4.x | Unit testing |
Core Features โ
- Islands Architecture: Zero JS by default, hydrate interactive components on demand
- Multi-framework Support: Use React, Vue, Svelte components in the same project
- Content-first: Static-first, ultimate initial load performance
- SSR Support: Server-side rendering via @astrojs/node adapter
- File-based Routing: Automatic routing based on file system
- API Endpoints: Native support for REST API endpoints
Directory Structure โ
halolight-astro/
โโโ src/
โ โโโ pages/ # File-based routing
โ โ โโโ index.astro # Home
โ โ โโโ privacy.astro # Privacy Policy
โ โ โโโ terms.astro # Terms of Service
โ โ โโโ auth/ # Auth pages
โ โ โ โโโ login.astro
โ โ โ โโโ register.astro
โ โ โ โโโ forgot-password.astro
โ โ โ โโโ reset-password.astro
โ โ โโโ dashboard/ # Dashboard pages
โ โ โ โโโ index.astro # Dashboard home
โ โ โ โโโ analytics.astro # Analytics
โ โ โ โโโ users.astro # User management
โ โ โ โโโ accounts.astro # Account management
โ โ โ โโโ documents.astro # Document management
โ โ โ โโโ files.astro # File management
โ โ โ โโโ messages.astro # Message center
โ โ โ โโโ notifications.astro
โ โ โ โโโ calendar.astro # Calendar
โ โ โ โโโ profile.astro # Profile
โ โ โ โโโ settings/ # Settings
โ โ โโโ api/ # API endpoints
โ โ โโโ auth/
โ โ โโโ login.ts
โ โ โโโ register.ts
โ โ โโโ forgot-password.ts
โ โ โโโ reset-password.ts
โ โโโ layouts/ # Layout components
โ โ โโโ Layout.astro # Base layout
โ โ โโโ AuthLayout.astro # Auth layout
โ โ โโโ DashboardLayout.astro # Dashboard layout
โ โ โโโ LegalLayout.astro # Legal pages layout
โ โโโ components/ # UI components
โ โ โโโ dashboard/
โ โ โโโ Sidebar.astro # Sidebar
โ โ โโโ Header.astro # Top navigation
โ โโโ styles/ # Global styles
โ โ โโโ globals.css
โ โโโ assets/ # Static assets
โโโ public/ # Public assets
โโโ tests/ # Test files
โโโ astro.config.mjs # Astro config
โโโ tailwind.config.mjs # Tailwind config
โโโ vitest.config.ts # Test config
โโโ package.jsonQuick Start โ
Installation โ
bash
git clone https://github.com/halolight/halolight-astro.git
cd halolight-astro
pnpm installEnvironment Variables โ
bash
cp .env.example .env.localenv
# .env.local example
PUBLIC_API_URL=/api
PUBLIC_MOCK=true
PUBLIC_DEMO_EMAIL=admin@halolight.h7ml.cn
PUBLIC_DEMO_PASSWORD=123456
PUBLIC_SHOW_DEMO_HINT=true
PUBLIC_APP_TITLE=Admin Pro
PUBLIC_BRAND_NAME=HalolightStart Development โ
bash
pnpm devVisit http://localhost:4321
Build for Production โ
bash
pnpm build
pnpm previewCore Features โ
Islands Architecture โ
Astro's Islands architecture allows pages to be static HTML by default, with JavaScript only added to interactive components:
astro
---
// Static import, no JS
import StaticCard from '../components/StaticCard.astro';
// Interactive component (can be from React/Vue/Svelte)
import Counter from '../components/Counter.tsx';
---
<!-- Pure static, zero JS -->
<StaticCard title="Statistics" />
<!-- Hydrate on page load -->
<Counter client:load />
<!-- Hydrate when visible (lazy load) -->
<Counter client:visible />
<!-- Hydrate when browser is idle -->
<Counter client:idle />Client Directives โ
| Directive | Behavior | Use Case |
|---|---|---|
client:load | Hydrate immediately on page load | Critical interactions |
client:idle | Hydrate when browser is idle | Non-critical interactions |
client:visible | Hydrate when element is visible | Lazy-loaded components |
client:only | Client-side rendering only | Browser API dependent |
client:media | Hydrate when media query matches | Responsive components |
Layout System โ
astro
---
// layouts/DashboardLayout.astro
import Layout from './Layout.astro';
import Sidebar from '../components/dashboard/Sidebar.astro';
import Header from '../components/dashboard/Header.astro';
interface Props {
title: string;
description?: string;
}
const { title, description } = Astro.props;
const currentPath = Astro.url.pathname;
---
<Layout title={title} description={description}>
<div class="min-h-screen bg-gray-50 dark:bg-gray-900">
<Sidebar currentPath={currentPath} />
<div class="lg:pl-64">
<Header title={title} />
<main class="p-4 lg:p-6">
<slot />
</main>
</div>
</div>
</Layout>API Endpoints โ
Astro natively supports creating API endpoints:
typescript
// src/pages/api/auth/login.ts
import type { APIRoute } from 'astro';
export const POST: APIRoute = async ({ request }) => {
const body = await request.json();
const { email, password } = body;
// Validation logic
if (!email || !password) {
return new Response(
JSON.stringify({ success: false, message: 'Email and password are required' }),
{ status: 400, headers: { 'Content-Type': 'application/json' } }
);
}
// Authentication logic...
return new Response(
JSON.stringify({
success: true,
message: 'Login successful',
user: { id: 1, name: 'Admin', role: 'admin' },
token: 'mock_token',
}),
{ status: 200, headers: { 'Content-Type': 'application/json' } }
);
};File-based Routing โ
| File Path | URL | Description |
|---|---|---|
src/pages/index.astro | / | Home |
src/pages/auth/login.astro | /auth/login | Login |
src/pages/dashboard/index.astro | /dashboard | Dashboard |
src/pages/dashboard/[id].astro | /dashboard/:id | Dynamic route |
src/pages/api/auth/login.ts | /api/auth/login | API endpoint |
Page Routes โ
| Path | Page | Description |
|---|---|---|
/ | Home | Landing page |
/auth/login | Login | User login |
/auth/register | Register | User registration |
/auth/forgot-password | Forgot Password | Password recovery |
/auth/reset-password | Reset Password | Set new password |
/dashboard | Dashboard | Data overview |
/dashboard/analytics | Analytics | Chart statistics |
/dashboard/users | User Management | User list |
/dashboard/accounts | Account Management | Account list |
/dashboard/documents | Document Management | Document list |
/dashboard/files | File Management | File list |
/dashboard/messages | Message Center | Message list |
/dashboard/notifications | Notifications | Notification list |
/dashboard/calendar | Calendar | Schedule management |
/dashboard/profile | Profile | Personal information |
/dashboard/settings | Settings | System settings |
/privacy | Privacy Policy | Legal page |
/terms | Terms of Service | Legal page |
Configuration โ
Astro Configuration โ
javascript
// astro.config.mjs
import { defineConfig } from 'astro/config';
import tailwind from '@astrojs/tailwind';
import node from '@astrojs/node';
export default defineConfig({
integrations: [tailwind()],
output: 'server', // SSR mode
adapter: node({
mode: 'standalone',
}),
server: {
port: 4321,
host: true,
},
});Output Modes โ
| Mode | Description | Use Case |
|---|---|---|
static | Static site generation (SSG) | Blogs, documentation |
server | Server-side rendering (SSR) | Dynamic applications |
hybrid | Hybrid mode | Partially dynamic |
Deployment โ
Node.js Server โ
bash
pnpm build
node ./dist/server/entry.mjsDocker โ
dockerfile
FROM node:20-alpine
WORKDIR /app
COPY package.json pnpm-lock.yaml ./
RUN npm install -g pnpm && pnpm install --frozen-lockfile
COPY . .
RUN pnpm build
EXPOSE 4321
CMD ["node", "./dist/server/entry.mjs"]Vercel โ
bash
# Install Vercel adapter
pnpm add @astrojs/vercel
# astro.config.mjs
import vercel from '@astrojs/vercel/serverless';
export default defineConfig({
adapter: vercel(),
});Cloudflare Pages โ
bash
# Install Cloudflare adapter
pnpm add @astrojs/cloudflare
# astro.config.mjs
import cloudflare from '@astrojs/cloudflare';
export default defineConfig({
adapter: cloudflare(),
});Testing โ
bash
# Run tests
pnpm test
# Generate coverage report
pnpm test --coverageComparison with Other Frameworks โ
| Feature | Astro | Next.js | Nuxt |
|---|---|---|---|
| Default JS Size | 0 KB | ~80 KB | ~70 KB |
| Islands Architecture | Native support | Not supported | Not supported |
| Multi-framework Components | Supported | Not supported | Not supported |
| SSG/SSR | Supported | Supported | Supported |
| Learning Curve | Low | Medium | Medium |