如何防止 gatsby 从 WordPress 下载远程文件?

问题描述

每当我运行 npm run developnpm run build 时,gatsby 都会从 wordpress 下载远程文件并将它们存储在缓存中。从缓存中提供图像和其他静态文件

由于它每次都在下载远程文件,因此需要花费大量时间来完成下载并为应用程序提供服务。这不利于开发。

我不想要。我只是想使用远程 URL 来呈现图像。我该怎么做?

解决方法

您有很多选项可用于自定义 gatsby-source-wordpress 插件中的行为:

  • hardCacheMediaFiles(实验性):当设置为 true 时,媒体文件将被硬缓存在 Gatsby 缓存之外 ./.wordpress-cache/path/to/media/file.jpeg. 这对于防止媒体文件被当 Gatsby 缓存自动清除时重新下载。使用此选项时,请务必忽略项目根目录中的 wordpress-cache 目录。

    用法:

    {
       resolve: `gatsby-source-wordpress`,options: {
         production: {
           hardCacheMediaFiles: true,// false by default
         },},}
    

    此选项不会避免从服务器下载,它会缓存图像以避免后续下载。

  • excludeByMimeTypes:允许根据 MIME 类型阻止下载与 MediaItem 节点关联的文件。

    用法:

    {
       resolve: `gatsby-source-wordpress`,options: {
         type: {
           MediaItem: {
             localFile: {
               excludeByMimeTypes: [`video/mp4`],// add your images format
             },}
    

    此选项可能会避免下载您的图像和指定格式

  • maxFileSizeBytes:允许阻止下载超过特定文件大小(以字节为单位)的文件。默认为 15mb。

    用法:

    {
       resolve: `gatsby-source-wordpress`,options: {
         type: {
          MediaItem: {
             localFile: {
               maxFileSizeBytes: 10485760,// 10Mb. Set it to 0 to avoid downloads
             },}
    

    此选项可能会避免从服务器下载资产。