0

我有一个使用 11ty 构建的 Netlify 网站,其中某些数据存储在 Airtable 中。使用 Airtable,我的客户可以修改该网站上的某些信息(照片和其他一些信息),然后,在仍然在 Airtable 中时,按下一个按钮,该按钮发送一个 webhook 以供 Netlify 使用来自的最新信息来重建网站空气桌。

对于图像,我有一个 Gulp 任务,在每个构建中:

  • 查看“Images”Airtable 表中的所有记录(他们的 API 给了我文件名和 URL)并以 JSON 格式将它们列在一个变量中
  • 然后它将这个列表与它在上次构建期间创建的列表进行比较,存储在一个文件中(portfoliocache.json)
  • 如果这些列表相同,则意味着 Airtable 中的图像自上次构建以来没有更改,所以什么都不做,节省带宽。
  • 但是,如果这些列表不同,它会删除我的“图像”文件夹中的所有文件,从 Airtable 下载它们然后优化它们,将新图像放在我的“图像”文件夹中。portfoliocache.json被新列表覆盖。这需要一些时间并消耗一些 Netlify 构建时间,所以我只在 Airtable 图像已更改时才这样做。

这在我的本地 Node.js 环境中效果很好,但是由于 Netlify 使用 Git 存储库作为其源,它每次都需要相同的portfolioCache.json 和相同的“图像”文件夹,因此我的所有图像都会在每次 Netlify 构建时重新下载.

有没有办法portfoliocache在 Netlify 构建期间更新和保存我和我的“图像”文件夹,以便下一个构建具有最新的图像列表?也许有一种方法可以在构建期间更新我的 Git 存储库中的文件?

不确定在这里显示我的代码是否相关,但如果需要,我会很乐意。

4

1 回答 1

0

Don't store the files and cache in the repository

I don't think committing your cache file and the images to the repository is a good idea. First, it doesn't really belong there, as caches should not be part of a repository and having automated commits in your repository will clutter your commit history. From a practical perspective, this might even cause an endless loop — the netlify build changes the repository which triggers a rebuild and so on.

Besides that, your approach can't work like it does on your local environment. Netlify uses a clean virtual environment for each build, so the images folder from a previous build will never exist during the next build.

Use a build plugin

Your build step appears to be working, the only issue being that processing all images every time takes too long for regular netlify builds, consuming many build minutes. In this case, why not move the caching to a netlify build plugin? Take a look at the documentation for creating your own plugin, it includes a cache utility. You could use that to store the image information in this cache and persist it in between builds. Or even cache the processed images there, only pulling images that don't exist in the cache yet.

Separate the image processing from the build step

Another solution would be to separate the image processing and the build step entirely, allowing you to store the processed images in the repository. For example, this should be possible using Github Actions or maybe the Zapier integration for Airtable + Github.

于 2020-11-16T13:48:01.493 回答