本文概述了 Gatsby 中可用的性能优化技术和机制。它涵盖了构建时和运行时优化,以帮助您创建快速高效的网站。有关部署特定的优化,请参阅部署适配器。
Gatsby 提供了多层次的性能优化,从构建时高效的资源处理到提升用户体验的运行时技术
来源
packages/gatsby/src/utils/webpack.config.jspackages/gatsby-plugin-image/package.jsonpackages/gatsby-plugin-sharp/package.jsonpackages/gatsby-plugin-offline/package.jsonGatsby 利用 webpack 进行资源打包和优化。它会根据构建阶段自动配置 webpack,采用性能最佳实践。
Gatsby 根据构建阶段使用不同的 webpack 配置
来源
packages/gatsby/src/utils/webpack.config.js:40-45packages/gatsby/src/utils/webpack.config.js:331-341Gatsby 自动实施代码分割策略以优化包大小
来源
packages/gatsby/src/utils/webpack.config.js:580-612packages/gatsby/src/utils/webpack.config.js:652-698生产构建的具体实现(build-javascript 阶段)
// In production JavaScript builds
if (stage === `build-javascript`) {
const splitChunks = {
chunks: `all`,
cacheGroups: {
default: false,
defaultVendors: false,
framework: {
chunks: `all`,
name: `framework`,
test: FRAMEWORK_BUNDLES_REGEX,
priority: 40,
enforce: true,
},
// ... other optimization options
}
}
}
来源
packages/gatsby/src/utils/webpack.config.js:653-670对于生产构建,Gatsby 应用激进的优化
来源
packages/gatsby/src/utils/webpack-utils.ts:344-347packages/gatsby/src/utils/webpack.config.js:274-280Gatsby 通过多个包提供了一个强大的图像优化生态系统。
来源
packages/gatsby-plugin-image/package.jsonpackages/gatsby-plugin-sharp/package.jsonpackages/gatsby-transformer-sharp/package.jsonpackages/gatsby-transformer-sqip/package.jsongatsby-plugin-image 提供了用于优化图像加载的 React 组件
这两个组件都会自动
来源
packages/gatsby-plugin-image/package.jsonpackages/gatsby-plugin-sharp/package.json:3-4gatsby-transformer-sqip 包会创建图像的低质量 SVG 预览,这些预览会立即加载,并在完整图像加载时被替换。
来源
packages/gatsby-transformer-sqip/package.json:2-4Gatsby 会在链接页面进入视口时自动预取,使导航感觉即时。
初始渲染不需要的组件可以进行懒加载
来源
packages/gatsby/src/utils/webpack.config.js:582-608gatsby-plugin-offline 包增加了对 Service Worker 的支持,以缓存资源并启用离线功能,从而提高感知性能和弹性。
来源
packages/gatsby-plugin-offline/package.json:3-4Gatsby 通过高效的数据获取策略,优化来自各种源(Contentful、WordPress、Shopify)的内容交付。
来源
packages/gatsby-source-contentful/package.jsonpackages/gatsby-source-wordpress/package.jsonpackages/gatsby-source-shopify/package.jsonpackages/gatsby-source-drupal/package.jsonpackages/gatsby-source-filesystem/package.json不同的数据源有专门的性能优化
来源
packages/gatsby-source-contentful/CHANGELOG.md:10-11packages/gatsby-source-wordpress/CHANGELOG.md:29-31packages/gatsby-source-shopify/CHANGELOG.md:29-30packages/gatsby-source-drupal/CHANGELOG.md:23-25Gatsby 利用工作池(worker pools)进行并行处理,以进行图像转换和其他 CPU 密集型任务。
来源
packages/gatsby-worker/package.json:3-4packages/gatsby-plugin-sharp/package.json:2-4Gatsby 可以检测哪些数据已更改,并仅重建受影响的页面,从而显著缩短内容更新的构建时间。
来源
packages/gatsby-source-contentful/CHANGELOG.md:45-47packages/gatsby-source-wordpress/CHANGELOG.md:133-135Gatsby 提供了内置的性能分析工具来识别性能瓶颈
// Enable React profiling
if (stage === `build-javascript` && program.profile) {
resolve.alias[`react-dom$`] = `react-dom/profiling`
resolve.alias[`scheduler/tracing`] = `scheduler/tracing-profiling`
}
来源
packages/gatsby/src/utils/webpack.config.js:518-521Gatsby 根据您的目标浏览器生成优化的包
来源
packages/gatsby/src/utils/webpack.config.js:179-186packages/gatsby/src/utils/webpack.config.js:199-206packages/gatsby/src/utils/browserslist.ts常见的性能问题包括
来源
packages/gatsby-plugin-image/package.jsonpackages/gatsby/src/utils/webpack.config.js:398-406packages/gatsby/src/utils/webpack.config.js:582-608要衡量 Gatsby 站点的性能
Lighthouse 分数关注于
通过实施上述性能优化技术,Gatsby 站点可以在这些指标上取得出色的性能分数。