dojo / cli-build-app

Command for building Dojo applications
Other
9 stars 32 forks source link

Add the ability to store resources against individual pages in the cache for Build Time Render #459

Open matt-gadd opened 3 years ago

matt-gadd commented 3 years ago

Enhancement At the moment Build Time Render has the concept of a page cache, and appropriate cli options to manage it. This is non programmatic for the end user and is a literal cache of the output of a specific page.

It would be nice if users could programmatically attach resources that are scoped to a page, for example network requests. We could then offer the user to be able to evict those as part of evicting a page from the cache (or not). This would be extremely useful on request heavy pages where the data from the request doesn't often change, these requests are normally quite expensive in terms of rendering for Build Time Render.

To do this, we would have to introduce some kind of Build Time Render api set that can be used in blocks, this has been mused before for different scenarios (https://github.com/dojo/webpack-contrib/issues/134).

For example usage something like this in a block:

import { cache } from '@dojo/framework/core/build'

const myBlock = async (url: string) => {
  // where 'current-page' is the scope, might be other options later like 'global'
  // where 'network-request' is a user defined key
  const myRequest = cache.get('current-page', 'network-request', url);
  if (myRequest) {
    return myRequest;
  }
  const response = await fetch(url);
  const json = await response.json();
  cache.set('current-page', 'network-request', url, { json });
  return json;
}

export default myBlock;

We can also potentially build higher level utilities on top of this in @dojo/framework, ie a fetchWithCache etc.

We would then want to supplement the existing page cache command options to be able to evict resources under a page, and list resources under a page too.