posts: use low quality thumbnails when Save-Data header is set.

When the Save-Data HTTP header is present, disable high quality (2x
pixel density) thumbnails. This is normally set when "Data Saver mode"
is enabled on Android, or "Lite mode" is enabled in Chrome.

This setting can also be set using the `save_data` URL param or HTTP
cookie. This is mainly for testing.

The <body> tag has a `current-user-save-data` data attribute that
indicates whether save data mode is on.

https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Save-Data
https://developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/save-data/#the_save-data_request_header
https://source.android.com/devices/tech/connect/data-saver
This commit is contained in:
evazion
2021-12-09 19:52:02 -06:00
parent 7dbde7bc14
commit 52013eac1f
5 changed files with 28 additions and 13 deletions

View File

@@ -80,6 +80,7 @@ class SessionLoader
set_time_zone
set_country
set_safe_mode
set_save_data_mode
initialize_session_cookies
CurrentUser.user.unban! if CurrentUser.user.ban_expired?
ensure
@@ -177,6 +178,13 @@ class SessionLoader
CurrentUser.safe_mode = safe_mode
end
# https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Save-Data
# https://www.keycdn.com/blog/save-data
def set_save_data_mode
save_data = params[:save_data].presence || request.cookies[:save_data].presence || request.headers["Save-Data"].presence || "false"
CurrentUser.save_data = save_data.truthy?
end
def initialize_session_cookies
session.options[:expire_after] = 20.years
session[:started_at] ||= Time.now.utc.to_s