PERF: cache all metadata for 60 seconds

PERF: cache all metadata for 60 seconds

Clients tend to request webmanifests and such very often.

Keep the data cached for 60 seconds so it is not requested aggresively.

diff --git a/app/controllers/metadata_controller.rb b/app/controllers/metadata_controller.rb
index fecf6c1..33d19f2 100644
--- a/app/controllers/metadata_controller.rb
+++ b/app/controllers/metadata_controller.rb
@@ -5,20 +5,24 @@ class MetadataController < ApplicationController
   skip_before_action :preload_json, :check_xhr, :redirect_to_login_if_required
 
   def manifest
+    expires_in 1.minutes
     render json: default_manifest.to_json, content_type: 'application/manifest+json'
   end
 
   def opensearch
+    expires_in 1.minutes
     render template: "metadata/opensearch.xml"
   end
 
   def app_association_android
     raise Discourse::NotFound unless SiteSetting.app_association_android.present?
+    expires_in 1.minutes
     render plain: SiteSetting.app_association_android, content_type: 'application/json'
   end
 
   def app_association_ios
     raise Discourse::NotFound unless SiteSetting.app_association_ios.present?
+    expires_in 1.minutes
     render plain: SiteSetting.app_association_ios, content_type: 'application/json'
   end
 
diff --git a/spec/requests/metadata_controller_spec.rb b/spec/requests/metadata_controller_spec.rb
index ae81664..93f120b 100644
--- a/spec/requests/metadata_controller_spec.rb
+++ b/spec/requests/metadata_controller_spec.rb
@@ -22,6 +22,8 @@ RSpec.describe MetadataController do
       get "/manifest.webmanifest"
       expect(response.status).to eq(200)
       expect(response.media_type).to eq('application/manifest+json')
+      expect(response.headers["Cache-Control"]).to eq('max-age=60, private')
+
       manifest = JSON.parse(response.body)
 
       expect(manifest["name"]).to eq(title)
@@ -105,6 +107,8 @@ RSpec.describe MetadataController do
       SiteSetting.favicon = upload
       get "/opensearch.xml"
 
+      expect(response.headers["Cache-Control"]).to eq('max-age=60, private')
+
       expect(response.status).to eq(200)
       expect(response.body).to include(title)
       expect(response.body).to include("/search?q={searchTerms}")
@@ -130,6 +134,8 @@ RSpec.describe MetadataController do
       EOF
       get "/.well-known/assetlinks.json"
 
+      expect(response.headers["Cache-Control"]).to eq('max-age=60, private')
+
       expect(response.status).to eq(200)
       expect(response.body).to include("hash_of_app_certificate")
       expect(response.body).to include("com.example.app")
@@ -156,6 +162,7 @@ RSpec.describe MetadataController do
       expect(response.status).to eq(200)
       expect(response.body).to include("applinks")
       expect(response.media_type).to eq('application/json')
+      expect(response.headers["Cache-Control"]).to eq('max-age=60, private')
 
       get "/apple-app-site-association.json"
       expect(response.status).to eq(404)

GitHub sha: ac968f53

1 Like