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.
This commit is contained in:
Sam Saffron 2020-07-01 12:58:02 +10:00
parent d8c2fc3ee0
commit ac968f5363
No known key found for this signature in database
GPG Key ID: B9606168D2FFD9F5
2 changed files with 11 additions and 0 deletions

View File

@ -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

View File

@ -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)