Skip to content

Commit 19f35f4

Browse files
committed
Improve cache buster for user backgrounds
Signed-off-by: Vincent Petry <vincent@nextcloud.com>
1 parent d77e832 commit 19f35f4

7 files changed

Lines changed: 34 additions & 19 deletions

File tree

apps/theming/lib/Controller/UserThemeController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public function getBackground(): Http\Response {
156156
* @NoAdminRequired
157157
*/
158158
public function setBackground(string $type = 'default', string $value = ''): JSONResponse {
159-
$currentVersion = (int)$this->config->getUserValue($this->userId, Application::APP_ID, 'backgroundVersion', '0');
159+
$currentVersion = (int)$this->config->getUserValue($this->userId, Application::APP_ID, 'userCacheBuster', '0');
160160

161161
try {
162162
switch ($type) {
@@ -182,7 +182,7 @@ public function setBackground(string $type = 'default', string $value = ''): JSO
182182
}
183183

184184
$currentVersion++;
185-
$this->config->setUserValue($this->userId, Application::APP_ID, 'backgroundVersion', (string)$currentVersion);
185+
$this->config->setUserValue($this->userId, Application::APP_ID, 'userCacheBuster', (string)$currentVersion);
186186

187187
return new JSONResponse([
188188
'type' => $type,

apps/theming/lib/Listener/BeforeTemplateRenderedListener.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,6 @@ public function handle(Event $event): void {
8989
$this->config->getUserValue($userId, Application::APP_ID, 'background', 'default'),
9090
);
9191

92-
$this->initialState->provideInitialState(
93-
'backgroundVersion',
94-
(int)$this->config->getUserValue($userId, Application::APP_ID, 'backgroundVersion', '0'),
95-
);
96-
9792
$this->initialState->provideInitialState(
9893
'themingDefaultBackground',
9994
$this->config->getAppValue('theming', 'backgroundMime', ''),

apps/theming/lib/Service/ThemeInjectionService.php

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,35 @@
2222
*/
2323
namespace OCA\Theming\Service;
2424

25+
use OCA\Theming\AppInfo\Application;
2526
use OCA\Theming\Themes\DefaultTheme;
27+
use OCP\IConfig;
2628
use OCP\IURLGenerator;
29+
use OCP\IUserSession;
2730
use OCP\Util;
2831

2932
class ThemeInjectionService {
3033

3134
private IURLGenerator $urlGenerator;
3235
private ThemesService $themesService;
3336
private DefaultTheme $defaultTheme;
37+
private IConfig $config;
38+
private ?string $userId;
3439

3540
public function __construct(IURLGenerator $urlGenerator,
3641
ThemesService $themesService,
37-
DefaultTheme $defaultTheme) {
42+
DefaultTheme $defaultTheme,
43+
IConfig $config,
44+
IUserSession $userSession) {
3845
$this->urlGenerator = $urlGenerator;
3946
$this->themesService = $themesService;
4047
$this->defaultTheme = $defaultTheme;
48+
$this->config = $config;
49+
if ($userSession->getUser() !== null) {
50+
$this->userId = $userSession->getUser()->getUID();
51+
} else {
52+
$this->userId = null;
53+
}
4154
}
4255

4356
public function injectHeaders() {
@@ -50,13 +63,13 @@ public function injectHeaders() {
5063

5164
// Default theme fallback
5265
$this->addThemeHeader($defaultTheme->getId());
53-
66+
5467
// Themes applied by media queries
5568
foreach($mediaThemes as $theme) {
5669
$this->addThemeHeader($theme->getId(), true, $theme->getMediaQuery());
5770
}
5871

59-
// Themes
72+
// Themes
6073
foreach($this->themesService->getThemes() as $theme) {
6174
// Ignore default theme as already processed first
6275
if ($theme->getId() === $this->defaultTheme->getId()) {
@@ -68,15 +81,24 @@ public function injectHeaders() {
6881

6982
/**
7083
* Inject theme header into rendered page
71-
*
84+
*
7285
* @param string $themeId the theme ID
7386
* @param bool $plain request the :root syntax
7487
* @param string $media media query to use in the <link> element
7588
*/
7689
private function addThemeHeader(string $themeId, bool $plain = true, string $media = null) {
90+
$cacheBuster = $this->config->getAppValue('theming', 'cachebuster', '0');
91+
if ($this->userId !== null) {
92+
// need to bust the cache for the CSS file when the user background changed as its
93+
// URL is served in those files
94+
$userCacheBuster = $this->config->getUserValue($this->userId, Application::APP_ID, 'userCacheBuster', '0');
95+
$cacheBuster .= $this->userId . '_' . $userCacheBuster;
96+
}
97+
7798
$linkToCSS = $this->urlGenerator->linkToRoute('theming.Theming.getThemeStylesheet', [
7899
'themeId' => $themeId,
79100
'plain' => $plain,
101+
'v' => substr(sha1($cacheBuster), 0, 8),
80102
]);
81103
Util::addHeader('link', [
82104
'rel' => 'stylesheet',

apps/theming/lib/Themes/DefaultTheme.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,10 +239,11 @@ public function getCSSVariables(): array {
239239
$user = $this->userSession->getUser();
240240
if ($appManager->isEnabledForUser(Application::APP_ID) && $user !== null) {
241241
$themingBackground = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'background', 'default');
242-
$currentVersion = (int)$this->config->getUserValue($user->getUID(), Application::APP_ID, 'backgroundVersion', '0');
242+
$currentVersion = (int)$this->config->getUserValue($user->getUID(), Application::APP_ID, 'userCacheBuster', '0');
243243

244244
if ($themingBackground === 'custom') {
245-
$variables['--image-main-background'] = "url('" . $this->urlGenerator->linkToRouteAbsolute('theming.userTheme.getBackground') . "?v=$currentVersion')";
245+
$cacheBuster = substr(sha1($user->getUID() . '_' . $currentVersion), 0, 8);
246+
$variables['--image-main-background'] = "url('" . $this->urlGenerator->linkToRouteAbsolute('theming.userTheme.getBackground') . "?v=$cacheBuster')";
246247
} elseif (isset(BackgroundService::SHIPPED_BACKGROUNDS[$themingBackground])) {
247248
$variables['--image-main-background'] = "url('" . $this->urlGenerator->linkTo(Application::APP_ID, "/img/background/$themingBackground") . "')";
248249
} elseif (substr($themingBackground, 0, 1) === '#') {

apps/theming/src/UserThemes.vue

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ const enforceTheme = loadState('theming', 'enforceTheme', '')
8888
const shortcutsDisabled = loadState('theming', 'shortcutsDisabled', false)
8989
9090
const background = loadState('theming', 'background')
91-
const backgroundVersion = loadState('theming', 'backgroundVersion')
9291
const themingDefaultBackground = loadState('theming', 'themingDefaultBackground')
9392
const shippedBackgroundList = loadState('theming', 'shippedBackgrounds')
9493
@@ -109,7 +108,6 @@ export default {
109108
enforceTheme,
110109
shortcutsDisabled,
111110
background,
112-
backgroundVersion,
113111
themingDefaultBackground,
114112
}
115113
},
@@ -169,7 +167,6 @@ export default {
169167
methods: {
170168
updateBackground(data) {
171169
this.background = (data.type === 'custom' || data.type === 'default') ? data.type : data.value
172-
this.backgroundVersion = data.version
173170
this.updateGlobalStyles()
174171
this.$emit('update:background')
175172
},

dist/theming-theming-settings.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/theming-theming-settings.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)