Taffy icon indicating copy to clipboard operation
Taffy copied to clipboard

Best Method to prevent GET caching?

Open JamoCA opened this issue 4 years ago • 7 comments

What's the best way to prevent caching of a GET response to a single URI?

I tried adding taffy_cache="false" to the component, but it didn't seem to make any difference.

My approach was to generate Pragma, Cache-Control, Last-Modified and Expires headers and return them using the withHeaders() method and, while it works, I thought there might be something else available.

JamoCA avatar Sep 14 '21 18:09 JamoCA

Based on the context of your question I'm pretty certain you don't mean server-side caching.

There's no way to guarantee that the client will never cache the response; at least not from the server side. It's just not within our control. However, you seem to have found all of the right headers to suggest to the client that it shouldn't cache the response. There's also ETags to consider, but I think if you've got all of the rest of those you're pretty well covered.

Sounds like maybe we should add a method to easily set them in the response... Something like the following?

return rep(data).neverCache();

atuttle avatar Sep 22 '21 13:09 atuttle

Never is a long time. How about noCache()?

The API response either contains overriding caching directive headers or it doesn't. Setting the cache dates to the past prevents caching so that subsequent GET requests actually connect to the server without requiring a random cache-busting parameter in the request string.

return rep(data).noCache();

JamoCA avatar Sep 28 '21 16:09 JamoCA

That sounds good to me. You volunteering to add it? 😇

atuttle avatar Oct 04 '21 13:10 atuttle

Sure. I've written the function and have tested it on my server. Which branch should I add it to?

JamoCA avatar Oct 11 '21 21:10 JamoCA

I explained the steps here: https://adamtuttle.codes/blog/2014/your-first-github-pull-request/

But tl;dr: fork the repo, create a new branch forked from main, make your change in that branch, push it to your forked repo, and then submit a PR asking to merge your branch into main here.

atuttle avatar Oct 12 '21 14:10 atuttle

Ahh... ok. I was looking for a quick recommendation in the repository itself and didn't realize I needed to refer to your blog. (I'm currently using GitHub's web-based GUI to submit changes.) I'll see what I can do as the instructions are a little different for someone who isn't using the console.

JamoCA avatar Oct 12 '21 15:10 JamoCA

No worries. Looks like you did fine. For reference, you might want to check this out: https://makeapullrequest.com/

atuttle avatar Oct 13 '21 21:10 atuttle