Google App Engine 标准环境是否与 next/image 兼容?

Is Google App Engine standard environment compatible with next/image?

我在使用 next/image 组件在云存储渲染前使用 imagekit.io CDN。在网上看到它与 GAE 标准环境不兼容之前,我曾 next/image 工作过。这让我很担心……即使它在工作,我想知道是否存在某种低效率,因为 next 无法在 /tmp 之外缓存图像。 next/image 对我来说就像一个神奇的黑盒子。

next.config.js

const nextConfig = {
  experimental: {
    externalDir: true
  },
  reactStrictMode: true,
  images: {
    domains: ["ik.imagekit.io"]
  },
  distDir: "build"
};

module.exports = nextConfig;

迁移到 flex 目前不是一个选择。我需要知道我是否应该离开 next/image.

TL; DR Google App Engine Standard 不是 Next

的好选择

正如您在 tutorial 中指出的:

Nextjs Features like Image Component with Optimization, Incremental Static Regeneration requires nextjs cache folder to be read/written on runtime. But our Standard Environment has read and write access only to the /tmp directory.

由于无法在运行时编写的限制,不适合在 App Engine Standard 上使用 next/image 组件。此结论基于 Google App Engine Standard 和 NextJS

的官方文档

NextJS (next/image)

Images are optimized dynamically upon request and stored in the <distDir>/cache/images directory. The optimized image file will be served for subsequent requests until the expiration is reached.

Google App Engine Standard:

All files in this directory are stored in the instance's RAM, therefore writing to /tmp takes up system memory. In addition, files in the /tmp directory are only available to the app instance that created the files. When the instance is deleted, the temporary files are deleted.

这似乎是使用某些需要写入 App Engine Standard 上的 /cache 文件夹 运行 的 NextJS 组件的开发人员之间的常见问题,如此 GitHub discussion 所示:

Currently, the image optimiser caches images locally, in the servers filesystem, typically under .next/cache/images. While this is fine on some platform, it's not uncommon for managed deployment target to restrict write access to the filesystem (e.g. Google App Engine only provides write access to an in-memory /tmp folder) ... Currently, the only option we have to use next image optimization within Google App Engine is to use a custom server, override the handler for _next/images with an handler that does exactly the same as the image optimizer

在这个 GitHub issue:

Did you have a solution for this one? Considering moving our marketing site to AWS temporarily over this. Can't use next/image on GCP GAE.

If you really need to deploy to GCP GAE, you can set your application to a flex environment.

另请参阅: