Skip to content

Astro 版本

HaloLight Astro 版本基于 Astro 5 构建,采用 Islands 架构,实现零 JS 首屏和极致性能。

在线预览https://halolight-astro.h7ml.cn/

GitHubhttps://github.com/halolight/halolight-astro

技术栈

技术版本说明
Astro5.xIslands 架构框架
TypeScript5.x类型安全
Tailwind CSS3.x原子化 CSS
Vite内置构建工具
@astrojs/node9.xNode.js 适配器
Vitest4.x单元测试

核心特性

  • Islands 架构:默认零 JS,按需水合交互组件
  • 多框架支持:可在同一项目中使用 React、Vue、Svelte 组件
  • 内容优先:静态优先,极致首屏性能
  • SSR 支持:通过 @astrojs/node 适配器支持服务端渲染
  • 文件路由:基于文件系统的自动路由
  • API 端点:原生支持 REST API 端点

目录结构

halolight-astro/
├── src/
│   ├── pages/                    # 文件路由
│   │   ├── index.astro          # 首页
│   │   ├── privacy.astro        # 隐私政策
│   │   ├── terms.astro          # 服务条款
│   │   ├── auth/                # 认证页面
│   │   │   ├── login.astro
│   │   │   ├── register.astro
│   │   │   ├── forgot-password.astro
│   │   │   └── reset-password.astro
│   │   ├── dashboard/           # 仪表盘页面
│   │   │   ├── index.astro      # 仪表盘首页
│   │   │   ├── analytics.astro  # 数据分析
│   │   │   ├── users.astro      # 用户管理
│   │   │   ├── accounts.astro   # 账户管理
│   │   │   ├── documents.astro  # 文档管理
│   │   │   ├── files.astro      # 文件管理
│   │   │   ├── messages.astro   # 消息中心
│   │   │   ├── notifications.astro
│   │   │   ├── calendar.astro   # 日历
│   │   │   ├── profile.astro    # 个人中心
│   │   │   └── settings/        # 设置
│   │   └── api/                 # API 端点
│   │       └── auth/
│   │           ├── login.ts
│   │           ├── register.ts
│   │           ├── forgot-password.ts
│   │           └── reset-password.ts
│   ├── layouts/                 # 布局组件
│   │   ├── Layout.astro         # 基础布局
│   │   ├── AuthLayout.astro     # 认证布局
│   │   ├── DashboardLayout.astro # 仪表盘布局
│   │   └── LegalLayout.astro    # 法律页面布局
│   ├── components/              # UI 组件
│   │   └── dashboard/
│   │       ├── Sidebar.astro    # 侧边栏
│   │       └── Header.astro     # 顶部导航
│   ├── styles/                  # 全局样式
│   │   └── globals.css
│   └── assets/                  # 静态资源
├── public/                      # 公共资源
├── tests/                       # 测试文件
├── astro.config.mjs            # Astro 配置
├── tailwind.config.mjs         # Tailwind 配置
├── vitest.config.ts            # 测试配置
└── package.json

快速开始

安装

bash
git clone https://github.com/halolight/halolight-astro.git
cd halolight-astro
pnpm install

环境变量

bash
cp .env.example .env.local
env
# .env.local 示例
PUBLIC_API_URL=/api
PUBLIC_MOCK=true
PUBLIC_DEMO_EMAIL=admin@example.com
PUBLIC_DEMO_PASSWORD=123456
PUBLIC_SHOW_DEMO_HINT=true
PUBLIC_APP_TITLE=Admin Pro
PUBLIC_BRAND_NAME=Halolight

启动开发

bash
pnpm dev

访问 http://localhost:4321

构建生产

bash
pnpm build
pnpm preview

核心功能

Islands 架构

Astro 的 Islands 架构允许页面默认为静态 HTML,仅在需要交互的组件上添加 JavaScript:

astro
---
// 静态导入,无 JS
import StaticCard from '../components/StaticCard.astro';
// 交互组件(可来自 React/Vue/Svelte)
import Counter from '../components/Counter.tsx';
---

<!-- 纯静态,零 JS -->
<StaticCard title="统计数据" />

<!-- 页面加载时水合 -->
<Counter client:load />

<!-- 可见时水合(懒加载) -->
<Counter client:visible />

<!-- 浏览器空闲时水合 -->
<Counter client:idle />

客户端指令

指令行为使用场景
client:load页面加载后立即水合首屏关键交互
client:idle浏览器空闲时水合非关键交互
client:visible元素可见时水合懒加载组件
client:only仅客户端渲染依赖浏览器 API
client:media媒体查询匹配时水合响应式组件

布局系统

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 端点

Astro 原生支持创建 API 端点:

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;

  // 验证逻辑
  if (!email || !password) {
    return new Response(
      JSON.stringify({ success: false, message: '邮箱和密码不能为空' }),
      { status: 400, headers: { 'Content-Type': 'application/json' } }
    );
  }

  // 认证逻辑...

  return new Response(
    JSON.stringify({
      success: true,
      message: '登录成功',
      user: { id: 1, name: '管理员', role: 'admin' },
      token: 'mock_token',
    }),
    { status: 200, headers: { 'Content-Type': 'application/json' } }
  );
};

文件路由

文件路径URL说明
src/pages/index.astro/首页
src/pages/auth/login.astro/auth/login登录
src/pages/dashboard/index.astro/dashboard仪表盘
src/pages/dashboard/[id].astro/dashboard/:id动态路由
src/pages/api/auth/login.ts/api/auth/loginAPI 端点

页面路由

路径页面说明
/首页落地页
/auth/login登录用户登录
/auth/register注册用户注册
/auth/forgot-password忘记密码密码找回
/auth/reset-password重置密码设置新密码
/dashboard仪表盘数据概览
/dashboard/analytics数据分析图表统计
/dashboard/users用户管理用户列表
/dashboard/accounts账户管理账户列表
/dashboard/documents文档管理文档列表
/dashboard/files文件管理文件列表
/dashboard/messages消息中心消息列表
/dashboard/notifications通知通知列表
/dashboard/calendar日历日程管理
/dashboard/profile个人中心个人信息
/dashboard/settings设置系统设置
/privacy隐私政策法律页面
/terms服务条款法律页面

配置

Astro 配置

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 模式
  adapter: node({
    mode: 'standalone',
  }),
  server: {
    port: 4321,
    host: true,
  },
});

输出模式

模式说明适用场景
static静态站点生成 (SSG)博客、文档站
server服务端渲染 (SSR)动态应用
hybrid混合模式部分动态

部署

Node.js 服务器

bash
pnpm build
node ./dist/server/entry.mjs

Docker

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
# 安装 Vercel 适配器
pnpm add @astrojs/vercel

# astro.config.mjs
import vercel from '@astrojs/vercel/serverless';

export default defineConfig({
  adapter: vercel(),
});

Cloudflare Pages

bash
# 安装 Cloudflare 适配器
pnpm add @astrojs/cloudflare

# astro.config.mjs
import cloudflare from '@astrojs/cloudflare';

export default defineConfig({
  adapter: cloudflare(),
});

测试

bash
# 运行测试
pnpm test

# 生成覆盖率报告
pnpm test --coverage

与其他框架对比

特性AstroNext.jsNuxt
默认 JS 体积0 KB~80 KB~70 KB
Islands 架构原生支持不支持不支持
多框架组件支持不支持不支持
SSG/SSR支持支持支持
学习曲线