question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`worktop/cache` POST requests

See original GitHub issue

https://developers.cloudflare.com/workers/examples/cache-post-request

worktop already sets Cache-Control to private, no-cache so a good solution may be to apply POST caching when this header gets modified.

Main use case for this is to cache GraphQL queries

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
lukeedcommented, Sep 15, 2021

With the changes in the next release, the above example can be consolidated into this:

import { Router } from 'worktop';
import * as Cache from 'worktop/cache';

const API = new Router;
// ...

// reusable function
const main = Cache.reply(API.run);

addEventListener('fetch', event => {
  let request = event.request;

  if (request.method !== 'POST') {
    return main(event);
  }

  let clone = request.clone();
  let key = toBodyHash(clone); // string

  // use new `key` for cache lookup
  return main(event, key);
});
1reaction
lukeedcommented, Sep 15, 2021

With the latest changes in worktop@next, this snippet (https://github.com/lukeed/worktop/issues/53#issuecomment-840825730) changes a little bit.

Now Cache.reply returns a Module.Worker definition (object), and there’s no method that does the lookup -> save sequence for you, but it’s pretty straightforward & it’s more configurable now too:

import { Router } from 'worktop';
import * as Cache from 'worktop/cache';

const API = new Router;
// ...

async function reply(req, ctx, custom) {
  let res = await Cache.lookup(custom || req);
  if (res) return res;
  
  res = await API.run(req, ctx);
  return Cache.save(custom || req, res);
} 

export default {
  fetch(req, env, ctx) {
    ctx.bindings = env;

    if (req.method === 'POST') {
      let hash = toBodyHash(clone); // string
      return reply(req, ctx, hash);
    }

    return reply(req, ctx);
  }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Caching POST requests - Varnish Software Documentation
When a user logs in to a site, or provides some unique or private information, this will be done through a POST request....
Read more >
Cache POST requests · Cloudflare Workers docs
Cache POST requests using the Cache API. async function sha256(message) { ... Hash the request body to use it as a part of...
Read more >
Caching HTTP POST Requests and Responses
The solution is to digest the POST body (along with a few headers), append the URL with the digest, and use this digest...
Read more >
Allow post caching · Issue #2615 · GoogleChrome/workbox
Developers should be able to cache post requests as get requests using cacheKeyWillBeUsed . Currently there is a check that prevents post ...
Read more >
how to cache response of a POST request using workbox
Let me clarify S. Esteves' answer. Browser cache storage doesn't has an ability to persist POST requests as a keys (see spec here)....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found