w3c / w3c/ServiceWorker

Allow Querying a CacheStorage

Open
#1,694 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Bikeshed
Stars
3.6k
Forks
324
Avg merge
14d 22h
Merged PRs (30d)
1

Description

Currently, the cache storage is mainly useful to store into a cache a response that matches somehow a request. So if you have a request already cached a very similar request can be found in the cache. It's fine but sometimes, it's just not enough to be very useful.

Currently, the only way you can query the cache storage is by fetching the keys and passing them through a custom router that would allow you to find a key that matches your criteria. The other way which might be a bit easier is to implement a custom database storage in an IndexedDB with the indexes that you need to search on.

For example, let say you had such url: /store/templates/xml/1?bundle=bundle1

Think of it like a xml template of version 1 and taking a bundle from this template called bundle1.

Basically, the query could try to find a bundle of the most recent version that is in the cache. So a search on:

"/store/template/xml/(?<version>\w+)\?bundle=(?<bundle>\w+)"

It would be nice if we could query the storage with something like a range or exact match.

cache.query(
    /\/store\/template\/xml\/(?<version>\w+)\?bundle=(?<bundle>\w+)/,
    {
      bundle: 'bundle1',
      version: new Range(0, new Date().valueOf())
    }
)

Or possibly allow a way to create indexes that can be queried.

   cache.createIndex('templates', (request) => extract_keys(request)) // ['bundle1', 1]

Then simply the index by taking a value from it using a key or range and possibly add a way to change the order in which the keys of the index are stored.

cache.index('templates').search(['bundle1']) // gets all bundle1 regardless of the version
cache.index('templates').search(['bundle1'], ['bundle1', 10]) // gets all bundle with a version less than 10.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the CacheStorage entry points described in the issue: cache.query, cache.createIndex, and cache.index, and compare them with the mentioned IndexedDB approach. The proposal needs a defined query or indexing API, including exact matches, ranges, and ordering, before implementation can be considered done.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
api, web-dev
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.