Cloudflare Pages 推出构建缓存(Build cache)功能

今天无意中看到 Cloudflare Pages 新推出构建缓存(Build cache)支持,试用了一下,提升还是挺大的。

Cloudflare Pages Build cache (beta)

Cloudflare Pages 的官方文档 https://developers.cloudflare.com/pages/platform/build-caching/

Cloudflare Pages Build cache 类似于 Github Action 的 Cache,可以将依赖缓存起来以加快构建速度。目前还是 beta 状态,支持部分包管理器和框架,可以在自己的项目设置中进行开启。需要注意的是构建缓存(Build cache) 依赖于 V2 构建系统(build system)。如果你正在使用 V1,需要先先升级到 V2.

注意这是 Cloudflare Pages 的构建缓存,不是 Cloudflare CDN 的缓存。

配置

配置界面目前很简单,只需要在在你的 pages 项目中的 Settings > Builds & deployments > Build cache 中点击开启即可。另外在设置中手动清除缓存(Clear cache)。

支持范围

根据官方文档,目前支持

  • yarn 1, npm, pnpm, bun 包管理器,(Cloudflare 将其称为 dependencies cache
  • Gatsby, Next.js, Astro 框架。(Cloudflare 将其称为 build output cache

限制

目前的缓存保留 7 天,每个项目最多 10GB 的缓存空间。

试用

开启前触发一次构建,开启后再触发一次构建,对比两次构建的速度。

我使用的是 pnpm 包管理器。

开启前

开启前,pnpm install 耗时 9.9s,所有的包都是通过下载(download)进行安装的。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
09:01:12.995    Detected the following tools from environment: [email protected], [email protected]
09:01:15.074 Installing project dependencies: pnpm install
09:01:16.011 Lockfile is up to date, resolution step is skipped
09:01:16.047 Progress: resolved 1, reused 0, downloaded 0, added 0
09:01:16.178 Packages: +490
09:01:16.179 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
09:01:16.723 Packages are hard linked from the content-addressable store to the virtual store.
09:01:16.724 Content-addressable store is at: /opt/buildhome/.pnpm-store/v3
09:01:16.724 Virtual store is at: node_modules/.pnpm
09:01:17.054 Progress: resolved 490, reused 0, downloaded 36, added 30
09:01:18.058 Progress: resolved 490, reused 0, downloaded 82, added 75
09:01:19.062 Progress: resolved 490, reused 0, downloaded 155, added 150
09:01:20.070 Progress: resolved 490, reused 0, downloaded 243, added 242
09:01:21.079 Progress: resolved 490, reused 0, downloaded 345, added 344
09:01:22.079 Progress: resolved 490, reused 0, downloaded 445, added 444
09:01:23.081 Progress: resolved 490, reused 0, downloaded 486, added 486
09:01:23.855 .../node_modules/****** postinstall$ ......
09:01:24.084 Progress: resolved 490, reused 0, downloaded 490, added 490, done
09:01:24.668 .../node_modules/****** postinstall: > ......
09:01:25.455
09:01:25.456 dependencies:
......
09:01:25.460 + ......
09:01:25.460
09:01:25.465 Done in 9.9s

开启后

开启后,pnpm install 耗时 3.4 秒。所有的包都是通过重用(reuse)进行安装。

可以看到日志中多出 Restoring from dependencies cache, Restoring from build output cache,值得注意的是这个过程将近 4 秒(我这里只有 dependencies cache)。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
22:35:03.716    Restoring from dependencies cache
22:35:03.739 Restoring from build output cache
22:35:07.525 Success: Dependencies restored from build cache.

22:35:07.663 Detected the following tools from environment: [email protected], [email protected]
22:35:09.726 Installing project dependencies: pnpm install
22:35:10.632 Lockfile is up to date, resolution step is skipped
22:35:10.668 Progress: resolved 1, reused 0, downloaded 0, added 0
22:35:10.791 Packages: +490
22:35:10.791 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
22:35:11.137 Packages are hard linked from the content-addressable store to the virtual store.
22:35:11.138 Content-addressable store is at: /opt/buildhome/.pnpm-store/v3
22:35:11.138 Virtual store is at: node_modules/.pnpm
22:35:11.669 Progress: resolved 490, reused 59, downloaded 0, added 56
22:35:12.669 Progress: resolved 490, reused 411, downloaded 0, added 410
22:35:13.635
22:35:13.635 dependencies:
......
22:35:13.637 + ......
22:35:13.638
22:35:13.645 Done in 3.4s
22:35:13.670 Progress: resolved 490, reused 490, downloaded 0, added 490, done

结果

可以看到 pnpm install 从 9.9 秒缩短到 3.4 秒,我认为这个速度提升还是挺大的。

我这个网站是小项目,依赖少,提升还不算明显。如果是稍大的项目,相信提升会更明显。

目前构建缓存(Build cache)这个功能支持的范围较少,也不能自定义缓存的内容,希望未来会有所改进。