Jerry's Blog

Back

部署

将你的站点部署到各种平台

包模式#

本主题自 v4.0.0 起集成了 NPM 包模式。推荐的方式是删除本地包文件,并从 NPM 安装主题集成。如果出于某些原因你想更改包内容,应将本地包链接到你的主题模板。这里集成了 bun 的方法。你应该:

  1. 添加环境变量 BUN_LINK_PKG=true
  2. 运行 bun pure check 自动链接包。

此方法也可用于其他平台(如 Vercel)的部署。你只需在平台控制设置的构建选项中添加该环境变量。构建命令 astro-pure check && astro check && astro build 将自动链接包。

部署模式#

Vercel#

本主题默认支持 Vercel:

  1. 将你的代码推送到在线 Git 仓库(GitHub、GitLab、BitBucket)。

  2. 导入你的项目 到 Vercel。

  3. Vercel 会自动检测 Astro 并配置正确的设置。

  4. 你的应用程序就部署好了!(例如 astro.vercel.app

Vercel 也支持静态方法:

astro.config.ts
import vercelServerless from '@astrojs/vercel/serverless'
import vercelStatic from '@astrojs/vercel/static'; 

export default defineConfig({
  //   ...
  adapter: vercelServerless() 
  adapter: vercelStatic(), 
})
ts

Node 服务器#

如果你要在本地使用 Node.js 部署,需要先安装 @astrojs/node

bun add '@astrojs/node'
shell

然后按照 astro.config.ts 中的注释进行修改,例如:

astro.config.ts
import node from '@astrojs/node'
import vercelServerless from '@astrojs/vercel/serverless'

export default defineConfig({
  //   ...
  adapter: vercelServerless(), 
  adapter: node({ mode: 'standalone' }), 

  integrations: [
    // prettier-ignore
    outputCopier({ 
      integ: ['sitemap', 'pagefind'] 
    }) 
  ]
})
ts

Bun 服务器#

Bun 也支持静态方法。

查看 @nurodev/astro-bun 了解更多。

静态站点#

移除 astro.config.ts 中所有的服务器适配器配置:

astro.config.ts
import vercelServerless from '@astrojs/vercel/serverless'

export default defineConfig({
  //   ...
  adapter: vercelServerless(), 
  output: 'server'
})
ts

GitHub Pages#

参见 Astro:为 GitHub Pages 配置 Astro

对于在带有基础路径的 GitHub Pages 上部署的情况,例如 username.github.io/repo-name,请仔细阅读 部署#带基础路径的平台

带基础路径的平台#

  1. astro.config.ts 中设置 base 选项为你的基础路径,例如:
astro.config.ts
export default defineConfig({
  //   ...
  base: '/repo-name/' // 替换为你的仓库名称
})
ts
  1. 更新动态脚本文件中的所有内部链接:
  • src/components/BaseHead.astro
  • src/pages/docs/DocsContent.astro
  • src/pages/rss.xml.ts
  • packages/pure/components/pages/PostPreview.astro(需要进行 自定义主题 才能修改)
  1. 更新所有静态资源路径:
  • src/site.config.ts 中添加基础路径。特别是菜单配置。
  • src/plugins/shiki-transformers.ts 中为所有 /icons/code.svg 添加基础路径。
  • src/assets/styles/app.css 中为 @font-face 属性中的所有 url 添加基础路径。