gatsby、hexo 和 vuepress 都是用于构建静态网站的流行工具,但它们在架构理念、技术栈和适用场景上有显著差异。gatsby 是基于 React 的现代化静态站点生成器,强调数据层抽象和高性能输出;hexo 是一个轻量级、基于 Node.js 的博客框架,以 Markdown 写作为核心;vuepress 则是由 Vue 团队打造的文档优先的静态站点生成器,深度集成 Vue 生态。三者都支持 Markdown 内容、静态资源处理和 SEO 友好输出,但在扩展能力、开发体验和目标用途上各有侧重。
在构建静态网站时,gatsby、hexo 和 vuepress 是三个主流选择,但它们的设计哲学、技术实现和适用边界截然不同。本文从真实工程视角出发,通过代码示例和架构分析,帮助专业开发者做出精准选型。
gatsby 采用 编译时数据层 + React 渲染 架构。所有内容(Markdown、CMS、API)通过 source 插件统一注入 GraphQL 层,页面组件在构建时查询数据并生成静态 HTML。
// gatsby: 使用 GraphQL 查询 Markdown 数据
import { graphql } from "gatsby";
export const query = graphql`
query BlogPostQuery($slug: String!) {
markdownRemark(fields: { slug: { eq: $slug } }) {
frontmatter { title }
html
}
}
`;
export default function BlogPost({ data }) {
return <div dangerouslySetInnerHTML={{ __html: data.markdownRemark.html }} />;
}
hexo 是 模板引擎驱动的静态生成器,核心流程为:读取 _posts/ 下的 Markdown → 通过 Nunjucks/EJS 等模板渲染 → 输出 HTML。无运行时框架,数据通过全局变量(如 page、site)注入模板。
<!-- hexo: EJS 模板示例 -->
<% page.posts.each(function(post){ %>
<article>
<h2><%- post.title %></h2>
<%- post.content %>
</article>
<% }) %>
vuepress 基于 Vue 单页应用 + Markdown 编译。每个 Markdown 文件被转换为 Vue 组件,支持在 Markdown 中直接使用 Vue 组件。数据通过 frontmatter 和 Vue 的响应式系统管理。
<!-- vuepress: 在 Markdown 中嵌入 Vue 组件 -->
# 文档标题
<Badge text="beta" type="warning"/>
```js
// .vuepress/components/Badge.vue
<template>
<span class="badge" :class="type">{{ text }}</span>
</template>
<script>
export default {
props: ['text', 'type']
}
</script>
gatsby 通过 统一 GraphQL 接口 整合多源数据。例如同时拉取本地 Markdown 和远程 CMS:
// gatsby-config.js
module.exports = {
plugins: [
`gatsby-source-filesystem`, // 本地文件
{
resolve: `gatsby-source-contentful`, // Contentful CMS
options: { /*...*/ }
}
]
};
hexo 仅支持 本地文件系统 作为内容源,依赖 _config.yml 配置元数据。扩展需通过插件修改全局变量:
# _config.yml
author: John Doe
social:
github: johndoe
vuepress 默认从 文件系统读取 Markdown,但可通过插件扩展数据源。例如使用 @vuepress/plugin-blog 添加分类/标签:
// .vuepress/config.js
module.exports = {
plugins: [
['@vuepress/blog', {
directories: [{
id: 'post',
dirname: '_posts'
}]
}]
]
};
gatsby 主题通过 React 组件 + Gatsby 插件 实现。可完全控制页面结构、数据查询和样式:
// src/components/layout.js
import Header from "./header";
export default function Layout({ children }) {
return (
<div>
<Header />
<main>{children}</main>
</div>
);
}
hexo 主题基于 模板引擎(如 Nunjucks)。修改需编辑 .ejs 或 .pug 文件,逻辑能力有限:
{# themes/landscape/layout.ejs #}
<!DOCTYPE html>
<html>
<head>
<title>{{ config.title }}</title>
</head>
<body>
{{ body }}
</body>
</html>
vuepress 主题由 Vue 单文件组件 构成。默认主题提供 Layout.vue 入口,可深度覆盖:
<!-- .vuepress/theme/Layout.vue -->
<template>
<div class="theme-container">
<Navbar />
<Sidebar />
<Content />
</div>
</template>
<script>
import Navbar from './components/Navbar.vue'
export default { components: { Navbar } }
</script>
gatsby 提供 热重载开发服务器 和增量构建优化。但大型站点首次构建可能较慢:
# 启动开发服务器
npm run develop
# 构建生产版本
npm run build
hexo 开发体验极简:
# 本地预览
hexo server
# 生成静态文件
hexo generate
vuepress 基于 Vue DevServer,支持 组件热更新 和 Markdown 实时预览:
# 启动开发服务器
vuepress dev
# 构建
vuepress build
三者均输出纯静态 HTML/CSS/JS,可部署到任何 CDN 或静态托管服务(如 Vercel、Netlify、GitHub Pages)。
gatsby 输出包含 预加载脚本 和 路由分片,支持 PWA。hexo 输出最简静态文件,无额外运行时。vuepress 输出 Vue SPA,首屏为静态 HTML,后续交互由 Vue 接管。| 能力 | gatsby | hexo | vuepress |
|---|---|---|---|
| 多数据源 | ✅ GraphQL 统一接入 | ❌ 仅本地文件 | ⚠️ 需插件扩展 |
| 组件化 UI | ✅ 完整 React 生态 | ❌ 模板引擎限制 | ✅ Vue 组件嵌入 |
| 自定义逻辑 | ✅ Node.js API + 插件系统 | ⚠️ 通过插件修改全局变量 | ✅ Vue 生命周期 + Composition API |
| 构建性能 | ⚠️ 大型站点较慢 | ✅ 极快 | ✅ 中等 |
| 学习曲线 | ⚠️ 高(需掌握 GraphQL/React) | ✅ 低 | ⚠️ 中(需 Vue 基础) |
gatsby:强大的数据管道和 React 生态适合复杂需求。hexo:零配置启动,专注写作本身。vuepress:Markdown 增强 + Vue 组件完美契合 API 文档场景。注意:截至 2024 年,三者均处于活跃维护状态,无官方弃用声明。但
hexo社区更新节奏较慢,新特性较少;gatsby和vuepress持续跟进现代 Web 标准(如 Webpack 5、Vite 支持)。
最终,选择应基于 团队技术栈、内容复杂度 和 长期维护成本 —— 而非单纯的功能列表。
选择 gatsby 如果你需要构建高度定制化、内容驱动的营销网站、博客或文档站,并希望利用 GraphQL 统一数据源、React 组件生态以及丰富的插件系统。它适合对性能(如懒加载、预取)有高要求且团队熟悉 React 的项目。但要注意其构建速度在大型站点中可能较慢,且学习曲线较陡。
选择 hexo 如果你主要目标是快速搭建一个轻量级个人博客,重视写作体验和部署简单性,且不需要复杂的交互逻辑或组件化 UI。它启动快、配置简洁,适合纯 Markdown 内容发布,但扩展性和现代前端工程能力有限,不适合构建复杂应用式站点。
选择 vuepress 如果你正在编写技术文档、API 手册或需要 Vue 组件嵌入的说明性内容,且团队已使用 Vue 技术栈。它提供开箱即用的主题系统、侧边栏自动生成和 Markdown 增强功能(如代码块高亮、组件嵌入),但定制主题或复杂布局时灵活性不如 Gatsby。
The future of web development is here.
Gatsby is a free and open source framework based on React that helps developers build blazing fast websites and apps.
It combines the control and scalability of dynamically rendered sites with the speed of static-site generation, creating a whole new web of possibilities.
Gatsby helps professional developers efficiently create maintainable, highly-performant, content-rich websites.
Load Data From Anywhere. Gatsby pulls in data from any data source, whether it’s Markdown files, a headless CMS like Contentful or WordPress, or a REST or GraphQL API. Use source plugins to load your data, then develop using Gatsby’s uniform GraphQL interface.
Go Beyond Static Websites. Get all the benefits of static websites with none of the limitations. Gatsby sites are fully functional React apps, so you can create high-quality, dynamic web apps, from blogs to e-commerce sites to user dashboards.
Choose your Rendering Options. You can choose alternative rendering options, namely Deferred Static Generation (DSG) and Server-Side Rendering (SSR), in addition to Static Site Generation (SSG) — on a per-page basis. This type of granular control allows you to optimize for performance and productivity without sacrificing one for the other.
Performance Is Baked In. Ace your performance audits by default. Gatsby automates code splitting, image optimization, inlining critical styles, lazy-loading, prefetching resources, and more to ensure your site is fast — no manual tuning required.
Use a Modern Stack for Every Site. No matter where the data comes from, Gatsby sites are built using React and GraphQL. Build a uniform workflow for you and your team, regardless of whether the data is coming from the same backend.
Host at Scale for Pennies. Gatsby sites don’t require servers, so you can host your entire site on a CDN for a fraction of the cost of a server-rendered site. Many Gatsby sites can be hosted entirely free on Gatsby Cloud and other similar services.
Use Gatsby's Centralized Data Layer Everywhere. With Gatsby's Valhalla Content Hub you can bring Gatsby's data layer to any project. Making it accessible via a unified GraphQL API for building content sites, eCommerce platforms, and both native and web applications.
Learn how to use Gatsby for your next project.
Click the link below to quickly try the workflow of developing, building, and deploying websites with Gatsby and Gatsby Cloud.
At the end of this process, you'll have
You can get a new Gatsby site up and running on your local dev environment in 5 minutes with these four steps:
Initialize a new project.
npm init gatsby
Give it the name "My Gatsby Site".
Start the site in develop mode.
Next, move into your new site’s directory and start it up:
cd my-gatsby-site/
npm run develop
Open the source code and start editing!
Your site is now running at http://localhost:8000. Open the my-gatsby-site directory in your code editor of choice and edit src/pages/index.js. Save your changes, and the browser will update in real time!
At this point, you’ve got a fully functional Gatsby website. For additional information on how you can customize your Gatsby site, see our plugins and the official tutorial.
Full documentation for Gatsby lives on the website.
For most developers, we recommend starting with our in-depth tutorial for creating a site with Gatsby. It starts with zero assumptions about your level of ability and walks through every step of the process.
To dive straight into code samples head to our documentation. In particular, check out the “How-to Guides”, “Reference”, and “Conceptual Guides” sections in the sidebar.
We welcome suggestions for improving our docs. See the “how to contribute” documentation for more details.
Start Learning Gatsby: Follow the Tutorial · Read the Docs
Wondering what we've shipped recently? Check out our release notes for key highlights, performance improvements, new features, and notable bugfixes.
Also, read our documentation on version support to understand our plans for each version of Gatsby.
Already have a Gatsby site? These handy guides will help you add the improvements of Gatsby v5 to your site without starting from scratch!
Gatsby is dedicated to building a welcoming, diverse, safe community. We expect everyone participating in the Gatsby community to abide by our Code of Conduct. Please read it. Please follow it. In the Gatsby community, we work hard to build each other up and create amazing things together. 💪💜
Whether you're helping us fix bugs, improve the docs, or spread the word, we'd love to have you as part of the Gatsby community!
Check out our Contributing Guide for ideas on contributing and setup steps for getting our repositories up and running on your local machine.
This repository is a monorepo managed using Lerna. This means there are multiple packages managed in this codebase, even though we publish them to NPM as separate packages.
Licensed under the MIT License.
Thanks go out to all our many contributors creating plugins, starters, videos, and blog posts. And a special appreciation for our community members helping with issues and PRs, or answering questions on GitHub Discussions.
A big part of what makes Gatsby great is each and every one of you in the community. Your contributions enrich the Gatsby experience and make it better every day.