Deploy multilingual static websites for free on Cloudflare using Astro framework with markdown source files. Use when: (1) Creating new static sites or blogs, (2) Setting up multilingual (i18n) content, (3) Deploying to Cloudflare Pages, (4) Converting markdown to static websites, (5) Setting up free hosting infrastructure.
使用 Astro 框架在 Cloudflare 上免费部署多语言静态网站。
bash
npm create astro@latest my-site -- --template minimal
cd my-site
npm install
静态站点(推荐大多数用例)
无需适配器。使用默认静态输出:
javascript
// astro.config.mjs
import { defineConfig } from astro/config;
export default defineConfig({
site: https://your-site.pages.dev,
});
SSR/边缘函数(可选)
如果需要服务端渲染或边缘函数:
bash
npm install @astrojs/cloudflare
javascript
// astro.config.mjs
import { defineConfig } from astro/config;
import cloudflare from @astrojs/cloudflare;
export default defineConfig({
output: server,
adapter: cloudflare(),
site: https://your-site.pages.dev,
});
Git 集成(推荐)
直接上传
bash
javascript
// astro.config.mjs
export default defineConfig({
i18n: {
defaultLocale: en,
locales: [en, es, fr, de],
routing: {
prefixDefaultLocale: false, // /about 而非 /en/about
},
},
});
路由模式:
| 设置 | URL 结构 | 最佳用途 |
|---|---|---|
| prefixDefaultLocale: false | /about, /es/about | 默认语言在根路径 |
| prefixDefaultLocale: true |
src/content/
├── config.ts # 内容集合模式
└── docs/
├── en/
│ ├── index.md
│ └── guide.md
├── es/
│ ├── index.md
│ └── guide.md
└── fr/
├── index.md
└── guide.md
typescript
// src/content/config.ts
import { defineCollection, z } from astro:content;
const docs = defineCollection({
type: content,
schema: z.object({
title: z.string(),
description: z.string(),
lang: z.enum([en, es, fr, de]),
}),
});
export const collections = { docs };
注意: 添加内容集合后运行 npx astro sync 以生成类型。
astro
const currentPath = Astro.url.pathname;
const currentLang = Astro.currentLocale || en;
my-site/
├── astro.config.mjs # Astro 配置
├── package.json
├── public/
│ ├── favicon.svg
│ └── _redirects # Cloudflare 重定向(可选)
├── src/
│ ├── components/
│ │ └── LanguageSwitcher.astro
│ ├── content/
│ │ ├── config.ts
│ │ └── blog/
│ │ ├── en/
│ │ └── es/
│ ├── layouts/
│ │ └── BaseLayout.astro
│ └── pages/
│ ├── index.astro
│ ├── en/
│ │ └── index.astro
│ └── es/
│ └── index.astro
| 设置 | 值 |
|---|---|
| 构建命令 | npm run build |
| 构建输出目录 |
Cloudflare 控制台 → Pages → your-site → 自定义域名 → 添加域名
创建 public/_redirects:
/ /en/ 302
/old-page /new-page 301
| 命令 | 描述 |
|---|---|
| npm run dev | 启动开发服务器 |
| npm run build |
astro
export async function getStaticPaths() {
const posts = await getCollection(blog);
return posts.map(post => ({
params: { slug: post.slug },
props: { post },
}));
}
const { post } = Astro.props;
const { Content } = await post.render();
在 Cloudflare Pages 环境变量中设置 NODE_VERSION=20。
javascript
// astro.config.mjs
export default defineConfig({
trailingSlash: always,
});
确保:
运行 npx astro sync 以生成 TypeScript 类型。
| 脚本 | 描述 |
|---|---|
| astro-new-post.py | 创建多语言博客文章 |
| astro-i18n-check.py |
bash
所有脚本仅使用 Python 标准库(无依赖项)。
该技能支持在以下平台通过对话安装:
帮我安装 SkillHub 和 astro-1776295769 技能
设置 SkillHub 为我的优先技能安装源,然后帮我安装 astro-1776295769 技能
skillhub install astro-1776295769
文件大小: 6.15 KB | 发布时间: 2026-4-16 18:09