mirror of
https://github.com/discourse/discourse.git
synced 2025-05-22 22:43:33 +08:00
FEATURE: add immutable caching to rails site of things
This commit is contained in:
@ -66,6 +66,12 @@ class ApplicationController < ActionController::Base
|
|||||||
refresh_session(current_user)
|
refresh_session(current_user)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def immutable_for(duration)
|
||||||
|
response.cache_control[:max_age] = duration.to_i
|
||||||
|
response.cache_control[:public] = true
|
||||||
|
response.cache_control[:extras] = ["immutable"]
|
||||||
|
end
|
||||||
|
|
||||||
def dont_cache_page
|
def dont_cache_page
|
||||||
if !response.headers["Cache-Control"] && response.cache_control.blank?
|
if !response.headers["Cache-Control"] && response.cache_control.blank?
|
||||||
response.headers["Cache-Control"] = "no-store, must-revalidate, no-cache, private"
|
response.headers["Cache-Control"] = "no-store, must-revalidate, no-cache, private"
|
||||||
|
@ -20,7 +20,7 @@ class HighlightJsController < ApplicationController
|
|||||||
|
|
||||||
response.headers["Last-Modified"] = 10.years.ago.httpdate
|
response.headers["Last-Modified"] = 10.years.ago.httpdate
|
||||||
response.headers["Content-Length"] = highlight_js.bytesize.to_s
|
response.headers["Content-Length"] = highlight_js.bytesize.to_s
|
||||||
expires_in 1.year, public: true
|
immutable_for 1.year
|
||||||
|
|
||||||
render text: highlight_js, disposition: nil, content_type: 'application/javascript'
|
render text: highlight_js, disposition: nil, content_type: 'application/javascript'
|
||||||
end
|
end
|
||||||
|
@ -117,7 +117,7 @@ class StaticController < ApplicationController
|
|||||||
response.headers["Content-Length"] = @@default_favicon.bytesize.to_s
|
response.headers["Content-Length"] = @@default_favicon.bytesize.to_s
|
||||||
render text: @@default_favicon, content_type: "image/png"
|
render text: @@default_favicon, content_type: "image/png"
|
||||||
else
|
else
|
||||||
expires_in 1.year, public: true
|
immutable_for 1.year
|
||||||
response.headers["Expires"] = 1.year.from_now.httpdate
|
response.headers["Expires"] = 1.year.from_now.httpdate
|
||||||
response.headers["Content-Length"] = data.bytesize.to_s
|
response.headers["Content-Length"] = data.bytesize.to_s
|
||||||
response.headers["Last-Modified"] = Time.new('2000-01-01').httpdate
|
response.headers["Last-Modified"] = Time.new('2000-01-01').httpdate
|
||||||
@ -151,7 +151,7 @@ class StaticController < ApplicationController
|
|||||||
response.headers["Cache-Control"] = 'max-age=31557600, public'
|
response.headers["Cache-Control"] = 'max-age=31557600, public'
|
||||||
response.headers["Content-Encoding"] = 'br'
|
response.headers["Content-Encoding"] = 'br'
|
||||||
|
|
||||||
expires_in 1.year, public: true, must_revalidate: false
|
immutable_for 1.year
|
||||||
|
|
||||||
send_file(path, opts)
|
send_file(path, opts)
|
||||||
end
|
end
|
||||||
@ -163,7 +163,7 @@ class StaticController < ApplicationController
|
|||||||
# SECURITY what if path has /../
|
# SECURITY what if path has /../
|
||||||
raise Discourse::NotFound unless path.start_with?(Rails.root.to_s + "/public/assets")
|
raise Discourse::NotFound unless path.start_with?(Rails.root.to_s + "/public/assets")
|
||||||
|
|
||||||
expires_in 1.year, public: true
|
immutable_for 1.year
|
||||||
|
|
||||||
response.headers["Expires"] = 1.year.from_now.httpdate
|
response.headers["Expires"] = 1.year.from_now.httpdate
|
||||||
response.headers["Access-Control-Allow-Origin"] = params[:origin] if params[:origin]
|
response.headers["Access-Control-Allow-Origin"] = params[:origin] if params[:origin]
|
||||||
|
@ -41,7 +41,7 @@ class StylesheetsController < ApplicationController
|
|||||||
end
|
end
|
||||||
|
|
||||||
response.headers['Last-Modified'] = stylesheet_time.httpdate if stylesheet_time
|
response.headers['Last-Modified'] = stylesheet_time.httpdate if stylesheet_time
|
||||||
expires_in 1.year, public: true unless Rails.env == "development"
|
immutable_for(1.year) unless Rails.env == "development"
|
||||||
send_file(location, disposition: :inline)
|
send_file(location, disposition: :inline)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -40,7 +40,7 @@ class UserAvatarsController < ApplicationController
|
|||||||
|
|
||||||
response.headers["Last-Modified"] = File.ctime(image).httpdate
|
response.headers["Last-Modified"] = File.ctime(image).httpdate
|
||||||
response.headers["Content-Length"] = File.size(image).to_s
|
response.headers["Content-Length"] = File.size(image).to_s
|
||||||
expires_in 1.year, public: true
|
immutable_for(1.year)
|
||||||
send_file image, disposition: nil
|
send_file image, disposition: nil
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -57,7 +57,7 @@ class UserAvatarsController < ApplicationController
|
|||||||
|
|
||||||
response.headers["Last-Modified"] = File.ctime(image).httpdate
|
response.headers["Last-Modified"] = File.ctime(image).httpdate
|
||||||
response.headers["Content-Length"] = File.size(image).to_s
|
response.headers["Content-Length"] = File.size(image).to_s
|
||||||
expires_in 1.year, public: true
|
immutable_for(1.year)
|
||||||
send_file image, disposition: nil
|
send_file image, disposition: nil
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -112,7 +112,7 @@ class UserAvatarsController < ApplicationController
|
|||||||
if image
|
if image
|
||||||
response.headers["Last-Modified"] = File.ctime(image).httpdate
|
response.headers["Last-Modified"] = File.ctime(image).httpdate
|
||||||
response.headers["Content-Length"] = File.size(image).to_s
|
response.headers["Content-Length"] = File.size(image).to_s
|
||||||
expires_in 1.year, public: true
|
immutable_for 1.year
|
||||||
send_file image, disposition: nil
|
send_file image, disposition: nil
|
||||||
else
|
else
|
||||||
render_blank
|
render_blank
|
||||||
@ -139,7 +139,7 @@ class UserAvatarsController < ApplicationController
|
|||||||
# putting a bogus date cause download is not retaining the data
|
# putting a bogus date cause download is not retaining the data
|
||||||
response.headers["Last-Modified"] = DateTime.parse("1-1-2000").httpdate
|
response.headers["Last-Modified"] = DateTime.parse("1-1-2000").httpdate
|
||||||
response.headers["Content-Length"] = File.size(path).to_s
|
response.headers["Content-Length"] = File.size(path).to_s
|
||||||
expires_in 1.year, public: true
|
immutable_for(1.year)
|
||||||
send_file path, disposition: nil
|
send_file path, disposition: nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -46,7 +46,7 @@ describe UserAvatarsController do
|
|||||||
|
|
||||||
get :show, size: 98, username: user.username, version: upload.id, hostname: 'default'
|
get :show, size: 98, username: user.username, version: upload.id, hostname: 'default'
|
||||||
expect(response.body).to eq("image")
|
expect(response.body).to eq("image")
|
||||||
expect(response.headers["Cache-Control"]).to eq('max-age=31557600, public')
|
expect(response.headers["Cache-Control"]).to eq('max-age=31557600, public, immutable')
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'serves image even if size missing and its in local mode' do
|
it 'serves image even if size missing and its in local mode' do
|
||||||
|
Reference in New Issue
Block a user