You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ENABLE_GATSBY_REFRESH_ENDPOINT失效:Gatsby+WordPress实时更新异常

Gatsby + WordPress 实时同步问题排查

问题背景

使用Gatsby作为前端框架,WordPress作为无头CMS,修改WordPress文章后,变更无法实时同步到Gatsby页面,必须重启服务器才能生效。已在.env文件中添加ENABLE_GATSBY_REFRESH_ENDPOINT=true但未起作用。

当前配置

.env 文件内容

ENABLE_GATSBY_REFRESH_ENDPOINT=true

gatsby-config.ts 文件内容

import type { GatsbyConfig } from 'gatsby'
import * as dotenv from 'dotenv' // see https://github.com/motdotla/dotenv#how-do-i-use-dotenv-with-import
dotenv.config()


const config: GatsbyConfig = {
  siteMetadata: {
    title: meta.title,
    description: meta.description,
    siteUrl: meta.siteUrl,
    logo: meta.logo,
  },
  // More easily incorporate content into your pages through automatic TypeScript type generation and better GraphQL IntelliSense.
  // If you use VSCode you can also use the GraphQL plugin
  // Learn more at: https://gatsby.dev/graphql-typegen
  graphqlTypegen: true,
  plugins: [
    'gatsby-plugin-postcss',
    'gatsby-plugin-image',
    'gatsby-plugin-sitemap',
    {
      resolve: 'gatsby-source-wordpress',
      options: {
        url:
          // allows a fallback url if WPGRAPHQL_URL is not set in the env, this may be a local or remote WP instance.

          `http://url/graphql`,
        schema: {
          //Prefixes all WP Types with "Wp" so "Post and allPost" become "WpPost and allWpPost".
          typePrefix: `Wp`,
          timeout: 3000000,
        },
        develop: {
          //caches media files outside of Gatsby's default cache an thus allows them to persist through a cache reset.
          hardCacheMediaFiles: true,
        },
        type: {
          Post: {
            limit:
              process.env.NODE_ENV === `development`
                ? // Lets just pull 50 posts in development to make it easy on ourselves (aka. faster).
                  50
                : // and we don't actually need more than 5000 in production for this particular site
                  5000,
          },
          Page: {
            limit: 0,
          },

          MenuItem: {
            limit: 0,
          },

          User: {
            limit: 0,
          },
          Tag: {
            limit: 0,
          },
        },
      },
    },
    {
      resolve: 'gatsby-plugin-manifest',
      options: {
        icon: 'src/images/logo.png',
      },
    },
    'gatsby-plugin-sharp',
    {
      resolve: 'gatsby-plugin-sitemap',
      options: {
        exclude: ['/thankyou'],
      },
    },
    'gatsby-transformer-sharp',
    {
      resolve: 'gatsby-source-filesystem',
      options: {
        name: 'images',
        path: './src/images/',
      },
      __key: 'images',
    },
    'gatsby-plugin-react-svg',
    {
      resolve: 'gatsby-plugin-manifest',
      options: {
        name: meta.title,
        short_name: meta.title,
        start_url: '/',
        background_color: 'white',
        theme_color: '#101C45',
        display: 'standalone',
        lang: 'en',
        icon: 'static/logo.png',
        description: meta.description,
      },
    },
  ],
}

export default config

可能的问题及解决步骤

1. 环境变量未正确加载

ENABLE_GATSBY_REFRESH_ENDPOINT变量可能未被Gatsby正确读取:

  • 确认添加变量后重启了Gatsby开发服务器,环境变量修改后必须重启才能生效;
  • 在gatsby-config.ts中添加日志排查:
    dotenv.config()
    console.log('ENABLE_GATSBY_REFRESH_ENDPOINT:', process.env.ENABLE_GATSBY_REFRESH_ENDPOINT)
    
    启动服务器后查看控制台是否输出true,如果是undefined,检查.env文件是否在项目根目录,或显式指定路径:
    dotenv.config({ path: `.env` })
    

2. 仅开启刷新端点,未配置触发机制

ENABLE_GATSBY_REFRESH_ENDPOINT=true只是让Gatsby暴露/___refresh端点,需要主动触发该端点才能同步内容:

  • 开发环境手动触发:访问http://localhost:8000/___refresh,若返回success: true,说明端点正常,此时Gatsby会重新拉取WordPress数据;
  • 自动触发:在WordPress后台配置webhook,当文章修改时自动调用Gatsby的/___refresh端点。可安装WordPress的webhook插件,设置触发条件为“文章更新”,目标URL填写Gatsby服务器地址(开发环境为http://localhost:8000/___refresh,生产环境替换为线上地址)。

3. gatsby-source-wordpress 插件缺失自动刷新配置

在插件配置中添加develop.refreshInterval,让Gatsby定期拉取WordPress数据(适合开发环境快速调试):

{
  resolve: 'gatsby-source-wordpress',
  options: {
    // 其他原有配置...
    develop: {
      hardCacheMediaFiles: true,
      // 每5秒拉取一次数据(单位:毫秒)
      refreshInterval: 5000
    },
    // 其他原有配置...
  },
}

4. 重复插件配置导致冲突

你的gatsby-config.ts中重复注册了gatsby-plugin-manifest插件,可能引发配置异常,建议合并为一个配置:

{
  resolve: 'gatsby-plugin-manifest',
  options: {
    name: meta.title,
    short_name: meta.title,
    start_url: '/',
    background_color: 'white',
    theme_color: '#101C45',
    display: 'standalone',
    lang: 'en',
    icon: 'static/logo.png', // 保留正确的图标路径
    description: meta.description,
  },
},

内容的提问来源于stack exchange,提问作者oladimeji

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 00:27:08