Hello,
Can somebody please show me how to invalidate the cache programmatically so that a data loader may fetch fresh data? I understand that this can be easily done by touching a data loader file but how can i do the same thing using code?
Thanks a lot
Alexis
Assuming that docs
is your root directory, you can clear the cache by running
rm -rf docs/.observablehq/cache
If you want to re-run a data loader, you can touch
the data loader. You’ll want to be running the latest version 1.5.1 in order for this to take immediate effect during preview.
Does touch
on the command line not count as code? When you say “using code”, are you trying to automate re-running the data loader in response to something, and if so, what?
i want to click a button (e.g. ‘refresh’) that will invalidate the cache and re-run the data loader
That’s not a use case we support because Framework builds static sites. (Data loaders only run at build time, so there’s no way to re-run a data loader once a built site is deployed.) You can use touch
to re-run your data loader manually, though, as discussed.
I understand. Would something like Window.navigation.reload()
help to solve part of the problem?
No, there’s no way for a client to invalidate the data loader cache by design — it’s a build-time operation.
Can you talk more about your workflow? Why do you need to click a button to refresh your data? Could you use a cron job to refresh the data instead? Or could you setup some other process to automate invalidation of the data based on whatever upstream changes?
What I’m working on is a kind of visualiser/editor on data saved remotely. Currently, I can fetch, edit and save back the data but I have no means to see what I did unless I switch to a command prompt to invalidate my cache.
Maybe i should also mention that edits cause async ops at the backend. When everything is finished I get back a web sockets notification which I would like to trigger a ‘refresh’…
Could you write a script that runs alongside the preview server and that listens for these web socket notifications and then invokes touch
to trigger the data loader? Or that simply polls to see if new data is available?
Absolutely, that works. I just thought there was an easy solution I was not aware of. I first became aware of Observable a week ago. Thanks a lot for your help!
1 Like