有没有办法禁用relayjs垃圾收集(版本5.0.0或6.0.0)?
我们仍在使用relayjs classic,它会缓存会话中的所有数据。这使得在获取新数据时快速加载以前的页面。在relayjs 5.0.0中,他们在QueryRenderer上有一个可以设置为"STORE_THEN_NETWORK“的dataFrom,它将首先尝试中继缓存存储并从网络中获取,就像rejay classic一样。除了较新版本的中继使用垃圾收集功能来删除当前不使用的数据之外。这使得几乎所有的页面都从网络获取数据。
发布于 2019-09-20 17:36:57
我设法让它工作起来了。这里的关键是environment.retain(operation.root);,它将对象保留在缓存中。
然后在QueryRenderer中使用fetchPolicy="store-and-network"。
请参阅下面的完整中继环境文件。
import {Environment, Network, RecordSource, Store} from 'relay-runtime';
function fetchQuery(operation, variables) {
const environment = RelayEnvironment.getInstance();
environment.retain(operation.root);
return fetch(process.env.GRAPHQL_ENDPOINT, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
credentials: 'include',
body: JSON.stringify({
query: operation.text,
variables
})
}).then(response => {
return response.json();
});
}
const RelayEnvironment = (function() {
let instance;
function createInstance() {
return new Environment({
network: Network.create(fetchQuery),
store: new Store(new RecordSource())
});
}
return {
getInstance: function() {
if (!instance) {
instance = createInstance();
}
return instance;
}
};
})();
export default RelayEnvironment;这也是从Relay Slack频道得到的。还没试过呢。
const store = new Store(new RecordSource());
(store as any).holdGC(); // Disable GC on the store.https://stackoverflow.com/questions/58022925
复制相似问题