From 012eb922d52d4749a988c6c7ba18cfe7d618dade Mon Sep 17 00:00:00 2001 From: David Taylor Date: Thu, 3 Nov 2022 11:31:16 +0000 Subject: [PATCH] Reduce theme/color-scheme cookie cookie duration (#18858) Previously these were set to expire after 9999 days (27 years). This commit updates them to last 1 year, and to automatically be extended on every user visit. --- .../app/initializers/handle-cookies.js | 18 +++++++++++++ .../discourse/app/lib/color-scheme-picker.js | 27 ++++++++++++++++--- .../discourse/app/lib/theme-selector.js | 18 ++++++++++--- 3 files changed, 57 insertions(+), 6 deletions(-) create mode 100644 app/assets/javascripts/discourse/app/initializers/handle-cookies.js diff --git a/app/assets/javascripts/discourse/app/initializers/handle-cookies.js b/app/assets/javascripts/discourse/app/initializers/handle-cookies.js new file mode 100644 index 00000000000..4526f51055f --- /dev/null +++ b/app/assets/javascripts/discourse/app/initializers/handle-cookies.js @@ -0,0 +1,18 @@ +import { extendThemeCookie } from "discourse/lib/theme-selector"; +import { extendColorSchemeCookies } from "discourse/lib/color-scheme-picker"; +import { later } from "@ember/runloop"; +import { isTesting } from "discourse-common/config/environment"; + +const DELAY = isTesting() ? 0 : 5000; + +export default { + name: "handle-cookies", + + initialize() { + // No need to block boot for this housekeeping - we can defer it a few seconds + later(() => { + extendThemeCookie(); + extendColorSchemeCookies(); + }, DELAY); + }, +}; diff --git a/app/assets/javascripts/discourse/app/lib/color-scheme-picker.js b/app/assets/javascripts/discourse/app/lib/color-scheme-picker.js index ff3e81d3289..7582323fcf6 100644 --- a/app/assets/javascripts/discourse/app/lib/color-scheme-picker.js +++ b/app/assets/javascripts/discourse/app/lib/color-scheme-picker.js @@ -89,14 +89,35 @@ export function loadColorSchemeStylesheet( ); } +const COLOR_SCHEME_COOKIE_NAME = "color_scheme_id"; +const DARK_SCHEME_COOKIE_NAME = "dark_scheme_id"; +const COOKIE_EXPIRY_DAYS = 365; + export function updateColorSchemeCookie(id, options = {}) { - const cookieName = options.dark ? "dark_scheme_id" : "color_scheme_id"; + const cookieName = options.dark + ? DARK_SCHEME_COOKIE_NAME + : COLOR_SCHEME_COOKIE_NAME; if (id) { cookie(cookieName, id, { path: "/", - expires: 9999, + expires: COOKIE_EXPIRY_DAYS, }); } else { - removeCookie(cookieName, { path: "/", expires: 1 }); + removeCookie(cookieName, { path: "/" }); + } +} + +export function extendColorSchemeCookies() { + for (const cookieName of [ + COLOR_SCHEME_COOKIE_NAME, + DARK_SCHEME_COOKIE_NAME, + ]) { + const currentValue = cookie(cookieName); + if (currentValue) { + cookie(cookieName, currentValue, { + path: "/", + expires: COOKIE_EXPIRY_DAYS, + }); + } } } diff --git a/app/assets/javascripts/discourse/app/lib/theme-selector.js b/app/assets/javascripts/discourse/app/lib/theme-selector.js index 7d6ca354ccf..58d4d41fdc0 100644 --- a/app/assets/javascripts/discourse/app/lib/theme-selector.js +++ b/app/assets/javascripts/discourse/app/lib/theme-selector.js @@ -3,6 +3,8 @@ import I18n from "I18n"; import deprecated from "discourse-common/lib/deprecated"; const keySelector = "meta[name=discourse_theme_id]"; +const COOKIE_NAME = "theme_ids"; +const COOKIE_EXPIRY_DAYS = 365; export function currentThemeKey() { // eslint-disable-next-line no-console @@ -35,12 +37,22 @@ export function currentThemeId() { export function setLocalTheme(ids, themeSeq) { ids = ids.reject((id) => !id); if (ids && ids.length > 0) { - cookie("theme_ids", `${ids.join(",")}|${themeSeq}`, { + cookie(COOKIE_NAME, `${ids.join(",")}|${themeSeq}`, { path: "/", - expires: 9999, + expires: COOKIE_EXPIRY_DAYS, }); } else { - removeCookie("theme_ids", { path: "/", expires: 1 }); + removeCookie(COOKIE_NAME, { path: "/" }); + } +} + +export function extendThemeCookie() { + const currentValue = cookie(COOKIE_NAME); + if (currentValue) { + cookie(COOKIE_NAME, currentValue, { + path: "/", + expires: COOKIE_EXPIRY_DAYS, + }); } }