Gatsby 本地开发指南
使用 Gatsby 构建 React 静态站点,GraphQL 数据层,本地默认 localhost:8000 开发。
Gatsby 是基于 React 的静态站点生成器(SSG),通过 GraphQL 统一拉取 Markdown、CMS、API 等数据源,构建时生成高性能静态页面,适合博客、营销站与内容聚合站点。
快速开始
npm init gatsby
cd my-gatsby-site
npm run develop或使用 CLI:
npx gatsby new my-gatsby-site https://github.com/gatsbyjs/gatsby-starter-default
cd my-gatsby-site
npm run develop默认 localhost 访问
| 用途 | 地址 |
|---|---|
| 开发服务器 | http://localhost:8000 |
| GraphQL 浏览器 | http://localhost:8000/___graphql |
| 预览生产构建 | http://localhost:9000(npm run serve) |
修改端口:gatsby develop -p 8080。
常用命令
| 命令 | 说明 |
|---|---|
npm run develop | 启动开发服务器 |
npm run build | 生成 public/ 静态站点 |
npm run serve | 预览生产构建 |
npm run clean | 清除缓存与构建产物 |
数据与插件
- 通过 gatsby-source-* 插件接入文件系统、WordPress、Contentful 等
- 构建时在 GraphQL 层查询数据,页面组件接收 props
- 丰富的 gatsby-plugin-* 生态(图片优化、SEO、sitemap 等)
小结
Gatsby 本地开发默认 **http://localhost:8000**,GraphQL 数据层适合内容驱动的 React 静态站点,部署到 CDN 或 Pages 即可。