更新Gatsby源插件中的数据 [英] Updating Data in Gatsby Source Plugin

查看:123
本文介绍了更新Gatsby源插件中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个简单的gatsby源插件,可以提取api数据.到目前为止,一切都很好–它完全可以按需提取数据.

I have created a simple gatsby source plugin that pulls in api data. So far, so good -- it pulls in the data exactly as it should.

但是,有一个问题. api数据会定期更新.也就是说,新文章会添加到api中.我想知道是否有可能用新文章更新Gatsby中的graphql数据而不会丢失已经获取的数据.

However, there is one problem. The api data gets updated on a regular basis. That is to say, new articles get added to the api. I am wondering if it is possible to update the graphql data in Gatsby with the new articles without losing the data that was already fetched.

如果是,怎么办?

谢谢.

P.S.如果相关的话,这是插件的代码:

P.S. In case it is relevant, here is the code of the plugin:

const fetch = require("node-fetch")

exports.sourceNodes = (
  { actions, createNodeId, createContentDigest },
  config
) => {
  const { createNode } = actions

  delete config.plugins

  const processFeed = item => {
    const nodeId = createNodeId(`[NODE_NAME]-${item.id}`)
    const nodeContent = JSON.stringify(item)
    const nodeData = Object.assign({}, item, {
      id: nodeId,
      parent: null,
      children: [],
      internal: {
        type: `[TYPE_NAME]`,
        content: nodeContent,
        contentDigest: createContentDigest(item),
      },
    })
    return nodeData
  }

  ...

  return fetch([FETCH_URL], [FETCH_HEADERS])
    .then(response => response.json())
    .then(data => {
      data.items.forEach(item => {
        const nodeData = processFeed(item)
        createNode(nodeData)
      })
    })
}

推荐答案

由于Gatsby中的GraphQL服务器仅在开发期间(或在创建内部版本时)运行,因此源插件无法将数据推送到常规开箱即用.相反,您可以做的是按周期性时间表(每N分钟/小时/等)触发新的构建,以获取更新的数据.

Since the GraphQL server in Gatsby only runs during development (or while the build is being created), there's no way for a source plugin to push data on a regular basis out of the box. What you could do instead is trigger a new build on a recurring schedule (every N minutes/hours/etc) to fetch updated data.

如果您的构建需要一些时间才能完成,并且希望加快此过程,则还可以考虑使用 Gatsby Cloud 支持执行增量构建(请注意:Gatsby Cloud是Gatsby核心团队提供的付费SaaS产品).

If your builds take a while to complete and you want to speed up this process, you could also consider using Gatsby Cloud which has support for doing incremental builds (note: Gatsby Cloud is a paid SaaS product from the Gatsby core team).

这篇关于更新Gatsby源插件中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆