Using Durable Objects for Assets
To enable Fast Deployment and Fast Development XP
Create
Durable Objects can be used to store « files », along with their headers. It is a Strong Consistency Asset Storage at edge. When the Worker is called to put or get the file in Durable Objects, it gets a file_id using env.files.idFromName(« a8ef-menu.js »). Once the file is created, the Worker puts the file in the edge cache. In edge cache, file is subject to eviction, anytime. If the file is a small PWA asset, the Worker also puts the file it in its memory (128 MB), using LRU algorithm.
Get
Read from edge cache, or from Worker’s memory, or from Durable Objects.
Delete
Feature Request : Files can be purged automatically or manually. Ex : User leaves service before GDPR retention clause. Currently, one way for Durable Object to emulate a delete is to storage.put(null), then respond 404 to future requests. But sadly the Durable Object itself still exists. Ideally, the storage attached to Durable Object accepts a TTL, a storage.delete() method, and Durable Object accepts a global delete method : env.files.delete(env.files.idFromName(name));
–
Why assets deserve Strong Consistency
Your new index.html is uploaded. It loads all of its new dependencies (JS modules, CSS, images…). These dependencies must exists. They must be immediately available. Not in 60s. Now ! Your whole PWA is released as a Blue / Green deployment. Everything or nothing. A generic importmap tracks version change of your files.
Fast Deployment
You upload the new static files. Optional steps : You upload the new worker. You destroy the cache for static pathnames (/, sw.js…) if they must deliver a new version. Your new PWA is available. In less than 5 seconds. You can test your PWA online as fast as you code, while still having control over your custom build process.