Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Improve cache buster for user backgrounds
Signed-off-by: Vincent Petry <[email protected]>
  • Loading branch information
PVince81 committed Oct 14, 2022
commit d094884057d7e499a2b9174c6009ee5968c2dd5d
4 changes: 2 additions & 2 deletions apps/theming/lib/Controller/UserThemeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public function getBackground(): Http\Response {
* @NoAdminRequired
*/
public function setBackground(string $type = 'default', string $value = ''): JSONResponse {
$currentVersion = (int)$this->config->getUserValue($this->userId, Application::APP_ID, 'backgroundVersion', '0');
$currentVersion = (int)$this->config->getUserValue($this->userId, Application::APP_ID, 'userCacheBuster', '0');

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

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

return new JSONResponse([
'type' => $type,
Expand Down
5 changes: 0 additions & 5 deletions apps/theming/lib/Listener/BeforeTemplateRenderedListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,6 @@ public function handle(Event $event): void {
$this->config->getUserValue($userId, Application::APP_ID, 'background', 'default'),
);

$this->initialState->provideInitialState(
'backgroundVersion',
(int)$this->config->getUserValue($userId, Application::APP_ID, 'backgroundVersion', '0'),
);

$this->initialState->provideInitialState(
'themingDefaultBackground',
$this->config->getAppValue('theming', 'backgroundMime', ''),
Expand Down
30 changes: 26 additions & 4 deletions apps/theming/lib/Service/ThemeInjectionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,35 @@
*/
namespace OCA\Theming\Service;

use OCA\Theming\AppInfo\Application;
use OCA\Theming\Themes\DefaultTheme;
use OCP\IConfig;
use OCP\IURLGenerator;
use OCP\IUserSession;
use OCP\Util;

class ThemeInjectionService {

private IURLGenerator $urlGenerator;
private ThemesService $themesService;
private DefaultTheme $defaultTheme;
private IConfig $config;
private ?string $userId;

public function __construct(IURLGenerator $urlGenerator,
ThemesService $themesService,
DefaultTheme $defaultTheme) {
DefaultTheme $defaultTheme,
IConfig $config,
IUserSession $userSession) {
$this->urlGenerator = $urlGenerator;
$this->themesService = $themesService;
$this->defaultTheme = $defaultTheme;
$this->config = $config;
if ($userSession->getUser() !== null) {
$this->userId = $userSession->getUser()->getUID();

Check notice

Code scanning / Psalm

PossiblyNullReference

Cannot call method getUID on possibly null value
} else {
$this->userId = null;
}
}

public function injectHeaders() {
Expand All @@ -50,13 +63,13 @@ public function injectHeaders() {

// Default theme fallback
$this->addThemeHeader($defaultTheme->getId());

// Themes applied by media queries
foreach($mediaThemes as $theme) {
$this->addThemeHeader($theme->getId(), true, $theme->getMediaQuery());
}

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

/**
* Inject theme header into rendered page
*
*
* @param string $themeId the theme ID
* @param bool $plain request the :root syntax
* @param string $media media query to use in the <link> element
*/
private function addThemeHeader(string $themeId, bool $plain = true, string $media = null) {
$cacheBuster = $this->config->getAppValue('theming', 'cachebuster', '0');
if ($this->userId !== null) {
// need to bust the cache for the CSS file when the user background changed as its
// URL is served in those files
$userCacheBuster = $this->config->getUserValue($this->userId, Application::APP_ID, 'userCacheBuster', '0');
$cacheBuster .= $this->userId . '_' . $userCacheBuster;
}

$linkToCSS = $this->urlGenerator->linkToRoute('theming.Theming.getThemeStylesheet', [
'themeId' => $themeId,
'plain' => $plain,
'v' => substr(sha1($cacheBuster), 0, 8),
]);
Util::addHeader('link', [
'rel' => 'stylesheet',
Expand Down
5 changes: 3 additions & 2 deletions apps/theming/lib/Themes/DefaultTheme.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,10 +239,11 @@ public function getCSSVariables(): array {
$user = $this->userSession->getUser();
if ($appManager->isEnabledForUser(Application::APP_ID) && $user !== null) {
$themingBackground = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'background', 'default');
$currentVersion = (int)$this->config->getUserValue($user->getUID(), Application::APP_ID, 'backgroundVersion', '0');
$currentVersion = (int)$this->config->getUserValue($user->getUID(), Application::APP_ID, 'userCacheBuster', '0');

if ($themingBackground === 'custom') {
$variables['--image-main-background'] = "url('" . $this->urlGenerator->linkToRouteAbsolute('theming.userTheme.getBackground') . "?v=$currentVersion')";
$cacheBuster = substr(sha1($user->getUID() . '_' . $currentVersion), 0, 8);
$variables['--image-main-background'] = "url('" . $this->urlGenerator->linkToRouteAbsolute('theming.userTheme.getBackground') . "?v=$cacheBuster')";
} elseif (isset(BackgroundService::SHIPPED_BACKGROUNDS[$themingBackground])) {
$variables['--image-main-background'] = "url('" . $this->urlGenerator->linkTo(Application::APP_ID, "/img/background/$themingBackground") . "')";
} elseif (substr($themingBackground, 0, 1) === '#') {
Expand Down
3 changes: 0 additions & 3 deletions apps/theming/src/UserThemes.vue
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ const enforceTheme = loadState('theming', 'enforceTheme', '')
const shortcutsDisabled = loadState('theming', 'shortcutsDisabled', false)

const background = loadState('theming', 'background')
const backgroundVersion = loadState('theming', 'backgroundVersion')
const themingDefaultBackground = loadState('theming', 'themingDefaultBackground')
const shippedBackgroundList = loadState('theming', 'shippedBackgrounds')

Expand All @@ -109,7 +108,6 @@ export default {
enforceTheme,
shortcutsDisabled,
background,
backgroundVersion,
themingDefaultBackground,
}
},
Expand Down Expand Up @@ -169,7 +167,6 @@ export default {
methods: {
updateBackground(data) {
this.background = (data.type === 'custom' || data.type === 'default') ? data.type : data.value
this.backgroundVersion = data.version
this.updateGlobalStyles()
this.$emit('update:background')
},
Expand Down
4 changes: 2 additions & 2 deletions dist/theming-theming-settings.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/theming-theming-settings.js.map

Large diffs are not rendered by default.