当我使用调用REST API而不重新启动Gatsby的Source插件时,如何在GatsbyJS中强制数据刷新? [英] How do I force a data refresh in GatsbyJS when I'm using a Source plugin that calls a REST API without restarting Gatsby?
问题描述
有可能吗?如何强制GatsbyJS重新加载外部数据?是通过禁用缓存还是限制缓存中的TTL?
Is it possible and how can I force GatsbyJS to reload external data? Either by disabling caching or limiting TTL in the cache?
我有一个运行gatsby develop
的GatsbyJS,它将立即重新加载代码更改,但已使用源插件通过获取将其连接到外部REST API.一旦在构建时/首次加载时加载了GraphQL中的数据,就不会更新.
I have a GatsbyJS running gatsby develop
and it will reload code changes immediately, but it's hooked up to an external REST API via fetch using a source plugin. The data in GraphQL doesn't update once the data is loaded at build time / first load.
我正在使用staticQuery(...)和非静态查询,两者的行为相同.我希望static不会更新,但是普通查询会触发一次并永远被缓存.
I'm using staticQuery(...) and non-static queries, both behave the same. I'd expect static to not update, but the normal query fires once and is forever cached.
来自gatsby-site/gatsby-node.js
From gatsby-site/gatsby-node.js
exports.createPages = async ({graphql, actions}) => {
const pages = await graphql(`
...
`);
问题我一直无法找到...的有用答案
Questions I've been unable to find useful answers to...
- 我可以使其完全不缓存吗?
- 我可以限制数据保留多长时间吗?
- 是在内存中,还是磁盘上有文件夹可以删除它?
所有有价值的帮助和想法.我找不到任何其他相关问题,甚至gatsbyjs.org上的教程都表明服务器会因数据更改而重新启动.
All help and ideas valuable. I couldn't find any other questions related, and even the tutorials on gatsbyjs.org indicate a server restart on data changes.
推荐答案
这有效,但可能无法扩展.
This works but probably doesn't scale.
- 使用
ENABLE_GATSBY_REFRESH_ENDPOINT=true gatsby develop
启动Gatsby或将设置放入.env文件 - 使用
curl -X POST http://localhost:8000/__refresh
调用完全刷新. - 或者,使其反复调用该钩子以使用
watch -n5 "curl -X POST http://localhost:8000/__refresh"
定期触发刷新(在这种情况下,每5秒一次,根据需要进行调整),或将其放在crontab中,以进行较长时间的无人值守刷新.
- Start Gatsby with
ENABLE_GATSBY_REFRESH_ENDPOINT=true gatsby develop
or put the setting in the .env file - Use
curl -X POST http://localhost:8000/__refresh
to invoke a full refresh. - Alternatively, make it repeatedly call the hook to trigger a refresh periodically with
watch -n5 "curl -X POST http://localhost:8000/__refresh"
(every 5 seconds in this case, tune as needed) or put it in a crontab for unattended refresh with longer intervals.
现在将反映对源数据的更改,但要以锤击源为代价.我只需要这个用于开发,而对于生产用途来说可能不是一个好主意.
Changes to source data will now be reflected, at the expense of hammering the source. I only needed this for development, and might not be a great idea for production use.
这篇关于当我使用调用REST API而不重新启动Gatsby的Source插件时,如何在GatsbyJS中强制数据刷新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!