-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
JAMstack
I001962 edited this page Jul 24, 2021
·
5 revisions
Have webpages access a cached version of Gun data from the CDN the site is deployed to. The goal is faster first paint time then going through network and getting updates.
Assuming JAM Stack app has a build step we could have Gun added to the project eg a Gatsbyjs site we
npm install gun
- in a postBuild step have
gun.get('yourdata').get('for first paint).once()
- then move the node_modules/gun/radata folder to the public folder created by gatsby build.
- Gatsby in this case deploys the content of the public folder to a cnd.
The pages that require gun data would then pull in the precached version of the data in gun. Note this data is frozen once deployed to the CDN. Here is some sample code that tells RAD to get from the CDN instead of eg browser localStorage:
var Rad = window.Radisk; // in Browser, still needs rad related scripts. See [RAD docs] (https://gun.eco/docs/RAD)
var opt = {store: {}, file:'https://yourfavoritecdn.com/yoursite/radata/'}; // cdn location
opt.store.put = function(key, data, cb){
localStorage[''+key] = data;
cb(null, 1);
} // important! not going to put any data but must be defined.
opt.store.get = async function(file, cb){
cb( null, await (await fetch(opt.file + file)).text())};
// need better way but works for now.
var rad = Rad(opt);
rad('KMAC', function(err, data){ console.log(data) }) // get rad tree. see [RAD docs] (https://gun.eco/docs/RAD)
- ideally we have RAD data already loaded before RAD code runs, which should be faster than
- this which loads RAD async upon 1st query, which should be faster than
- loading data by first connecting into the network and then getting updates. (this will happen even if 1 or 2 occur)