From a86e236d19b66f1b1d4bead707b6994a2d55bded Mon Sep 17 00:00:00 2001 From: Shivam Mishra Date: Wed, 14 Jun 2023 13:21:51 +0530 Subject: [PATCH] feat: update cache headers for cache_keys (#7283) Update the cache headers for cache_keys to max-age=10, private, stale-while-revalidate=300 1. The cache will be fresh for 10 seconds (max-age=10). During this time, the browser will use the cached version without checking with the server. 2. After the initial 10 seconds, the browser can continue to serve the stale cache for up to 5 minutes (stale-while-revalidate=300). During this period, it will also try to revalidate and update the cache in the background. 3. After 310 seconds in total (10 seconds fresh, 300 seconds stale), if the browser has not been able to revalidate the cache, it will attempt to fetch the fresh resource directly from the server for subsequent requests, causing potential latency equivalent to a network request. This means that the data will be directly revalidated only every 5 mins. Other times, it will stay fresh for 10 seconds and revalidate in the background. In most cases, we won't have to rely on a cache validation check because there is a WebSocket event for revalidation, so we know if something changes. Right now the stale-while-revalidate is 5 minutes, we can then move it to 15 minutes. > The stale-while-revalidate header is not supported in Safari, for Safari the cache keys will only stay in memory for 10 seconds before being marked stale --- app/controllers/api/v1/accounts_controller.rb | 1 + spec/controllers/api/v1/accounts_controller_spec.rb | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/app/controllers/api/v1/accounts_controller.rb b/app/controllers/api/v1/accounts_controller.rb index 5af8a1fd1..ef0e0c777 100644 --- a/app/controllers/api/v1/accounts_controller.rb +++ b/app/controllers/api/v1/accounts_controller.rb @@ -37,6 +37,7 @@ class Api::V1::AccountsController < Api::BaseController end def cache_keys + expires_in 10.seconds, public: false, stale_while_revalidate: 5.minutes render json: { cache_keys: get_cache_keys }, status: :ok end diff --git a/spec/controllers/api/v1/accounts_controller_spec.rb b/spec/controllers/api/v1/accounts_controller_spec.rb index 6b21cfbb2..ce5f9623c 100644 --- a/spec/controllers/api/v1/accounts_controller_spec.rb +++ b/spec/controllers/api/v1/accounts_controller_spec.rb @@ -151,6 +151,16 @@ RSpec.describe 'Accounts API', type: :request do expect(response).to have_http_status(:success) expect(response.parsed_body['cache_keys'].keys).to match_array(%w[label inbox team]) end + + it 'sets the appropriate cache headers' do + get "/api/v1/accounts/#{account.id}/cache_keys", + headers: admin.create_new_auth_token, + as: :json + + expect(response.headers['Cache-Control']).to include('max-age=10') + expect(response.headers['Cache-Control']).to include('private') + expect(response.headers['Cache-Control']).to include('stale-while-revalidate=300') + end end describe 'PUT /api/v1/accounts/{account.id}' do