Skip to content

Commit cdb2cc1

Browse files
authored
Merge pull request #8999 from nextcloud/css-js-name-based-on-apps-versions
Use app version to generate scss filename
2 parents 2124eba + 1d7b14d commit cdb2cc1

File tree

2 files changed

+44
-17
lines changed

2 files changed

+44
-17
lines changed

lib/private/Template/SCSSCacher.php

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public function process(string $root, string $file, string $app): bool {
112112
$path = explode('/', $root . '/' . $file);
113113

114114
$fileNameSCSS = array_pop($path);
115-
$fileNameCSS = $this->prependBaseurlPrefix(str_replace('.scss', '.css', $fileNameSCSS));
115+
$fileNameCSS = $this->prependVersionPrefix($this->prependBaseurlPrefix(str_replace('.scss', '.css', $fileNameSCSS)), $app);
116116

117117
$path = implode('/', $path);
118118
$webDir = $this->getWebDir($path, $app, $this->serverRoot, \OC::$WEBROOT);
@@ -138,7 +138,8 @@ public function process(string $root, string $file, string $app): bool {
138138
*/
139139
public function getCachedCSS(string $appName, string $fileName): ISimpleFile {
140140
$folder = $this->appData->getFolder($appName);
141-
return $folder->getFile($this->prependBaseurlPrefix($fileName));
141+
$cachedFileName = $this->prependVersionPrefix($this->prependBaseurlPrefix($fileName), $appName);
142+
return $folder->getFile($cachedFileName);
142143
}
143144

144145
/**
@@ -322,19 +323,34 @@ private function rebaseUrls(string $css, string $webDir): string {
322323
public function getCachedSCSS(string $appName, string $fileName): string {
323324
$tmpfileLoc = explode('/', $fileName);
324325
$fileName = array_pop($tmpfileLoc);
325-
$fileName = $this->prependBaseurlPrefix(str_replace('.scss', '.css', $fileName));
326+
$fileName = $this->prependVersionPrefix($this->prependBaseurlPrefix(str_replace('.scss', '.css', $fileName)), $appName);
326327

327328
return substr($this->urlGenerator->linkToRoute('core.Css.getCss', ['fileName' => $fileName, 'appName' => $appName]), strlen(\OC::$WEBROOT) + 1);
328329
}
329330

330331
/**
331332
* Prepend hashed base url to the css file
332-
* @param string$cssFile
333+
* @param string $cssFile
333334
* @return string
334335
*/
335336
private function prependBaseurlPrefix(string $cssFile): string {
336337
$frontendController = ($this->config->getSystemValue('htaccess.IgnoreFrontController', false) === true || getenv('front_controller_active') === 'true');
337-
return substr(md5($this->urlGenerator->getBaseUrl() . $frontendController), 0, 8) . '-' . $cssFile;
338+
return substr(md5($this->urlGenerator->getBaseUrl() . $frontendController), 0, 4) . '-' . $cssFile;
339+
}
340+
341+
/**
342+
* Prepend hashed app version hash
343+
* @param string $cssFile
344+
* @param string $appId
345+
* @return string
346+
*/
347+
private function prependVersionPrefix(string $cssFile, string $appId): string {
348+
$appVersion = \OC_App::getAppVersion($appId);
349+
if ($appVersion !== '0') {
350+
return substr(md5($appVersion), 0, 4) . '-' . $cssFile;
351+
}
352+
$coreVersion = \OC_Util::getVersionString();
353+
return substr(md5($coreVersion), 0, 4) . '-' . $cssFile;
338354
}
339355

340356
/**

tests/lib/Template/SCSSCacherTest.php

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
use OCP\IConfig;
3636
use OCP\ILogger;
3737
use OCP\IURLGenerator;
38+
use OC_App;
3839

3940
class SCSSCacherTest extends \Test\TestCase {
4041
/** @var ILogger|\PHPUnit_Framework_MockObject_MockObject */
@@ -58,17 +59,26 @@ protected function setUp() {
5859
parent::setUp();
5960
$this->logger = $this->createMock(ILogger::class);
6061
$this->appData = $this->createMock(IAppData::class);
62+
6163
/** @var Factory|\PHPUnit_Framework_MockObject_MockObject $factory */
6264
$factory = $this->createMock(Factory::class);
6365
$factory->method('get')->with('css')->willReturn($this->appData);
66+
6467
$this->urlGenerator = $this->createMock(IURLGenerator::class);
68+
$this->urlGenerator->expects($this->any())
69+
->method('getBaseUrl')
70+
->willReturn('http://localhost/nextcloud');
71+
6572
$this->config = $this->createMock(IConfig::class);
6673
$this->cacheFactory = $this->createMock(ICacheFactory::class);
6774
$this->depsCache = $this->createMock(ICache::class);
6875
$this->cacheFactory->expects($this->at(0))
6976
->method('createDistributed')
7077
->willReturn($this->depsCache);
78+
7179
$this->themingDefaults = $this->createMock(ThemingDefaults::class);
80+
$this->themingDefaults->expects($this->any())->method('getScssVariables')->willReturn([]);
81+
7282
$this->scssCacher = new SCSSCacher(
7383
$this->logger,
7484
$factory,
@@ -78,11 +88,6 @@ protected function setUp() {
7888
\OC::$SERVERROOT,
7989
$this->cacheFactory
8090
);
81-
$this->themingDefaults->expects($this->any())->method('getScssVariables')->willReturn([]);
82-
83-
$this->urlGenerator->expects($this->any())
84-
->method('getBaseUrl')
85-
->willReturn('http://localhost/nextcloud');
8691
}
8792

8893
public function testProcessUncachedFileNoAppDataFolder() {
@@ -96,7 +101,8 @@ public function testProcessUncachedFileNoAppDataFolder() {
96101

97102
$fileDeps = $this->createMock(ISimpleFile::class);
98103
$gzfile = $this->createMock(ISimpleFile::class);
99-
$filePrefix = substr(md5('http://localhost/nextcloud'), 0, 8) . '-';
104+
$filePrefix = substr(md5(\OC_Util::getVersionString('core')), 0, 4) . '-' .
105+
substr(md5('http://localhost/nextcloud'), 0, 4) . '-';
100106

101107
$folder->method('getFile')
102108
->will($this->returnCallback(function($path) use ($file, $gzfile, $filePrefix) {
@@ -131,7 +137,8 @@ public function testProcessUncachedFile() {
131137
$file->expects($this->any())->method('getSize')->willReturn(1);
132138
$fileDeps = $this->createMock(ISimpleFile::class);
133139
$gzfile = $this->createMock(ISimpleFile::class);
134-
$filePrefix = substr(md5('http://localhost/nextcloud'), 0, 8) . '-';
140+
$filePrefix = substr(md5(\OC_Util::getVersionString('core')), 0, 4) . '-' .
141+
substr(md5('http://localhost/nextcloud'), 0, 4) . '-';
135142

136143
$folder->method('getFile')
137144
->will($this->returnCallback(function($path) use ($file, $gzfile, $filePrefix) {
@@ -162,7 +169,8 @@ public function testProcessCachedFile() {
162169
$fileDeps = $this->createMock(ISimpleFile::class);
163170
$fileDeps->expects($this->any())->method('getSize')->willReturn(1);
164171
$gzFile = $this->createMock(ISimpleFile::class);
165-
$filePrefix = substr(md5('http://localhost/nextcloud'), 0, 8) . '-';
172+
$filePrefix = substr(md5(\OC_Util::getVersionString('core')), 0, 4) . '-' .
173+
substr(md5('http://localhost/nextcloud'), 0, 4) . '-';
166174

167175
$folder->method('getFile')
168176
->will($this->returnCallback(function($name) use ($file, $fileDeps, $gzFile, $filePrefix) {
@@ -197,6 +205,8 @@ public function testProcessCachedFileMemcache() {
197205

198206
$gzFile = $this->createMock(ISimpleFile::class);
199207
$filePrefix = substr(md5('http://localhost/nextcloud'), 0, 8) . '-';
208+
$filePrefix = substr(md5(\OC_Util::getVersionString('core')), 0, 4) . '-' .
209+
substr(md5('http://localhost/nextcloud'), 0, 4) . '-';
200210
$folder->method('getFile')
201211
->will($this->returnCallback(function($name) use ($file, $fileDeps, $gzFile, $filePrefix) {
202212
if ($name === $filePrefix.'styles.css') {
@@ -382,8 +392,8 @@ public function testRebaseUrls($scss, $expected) {
382392

383393
public function dataGetCachedSCSS() {
384394
return [
385-
['core', 'core/css/styles.scss', '/css/core/styles.css'],
386-
['files', 'apps/files/css/styles.scss', '/css/files/styles.css']
395+
['core', 'core/css/styles.scss', '/css/core/styles.css', \OC_Util::getVersionString()],
396+
['files', 'apps/files/css/styles.scss', '/css/files/styles.css', \OC_App::getAppVersion('files')]
387397
];
388398
}
389399

@@ -393,11 +403,12 @@ public function dataGetCachedSCSS() {
393403
* @param $result
394404
* @dataProvider dataGetCachedSCSS
395405
*/
396-
public function testGetCachedSCSS($appName, $fileName, $result) {
406+
public function testGetCachedSCSS($appName, $fileName, $result, $version) {
397407
$this->urlGenerator->expects($this->once())
398408
->method('linkToRoute')
399409
->with('core.Css.getCss', [
400-
'fileName' => substr(md5('http://localhost/nextcloud'), 0, 8) . '-styles.css',
410+
'fileName' => substr(md5($version), 0, 4) . '-' .
411+
substr(md5('http://localhost/nextcloud'), 0, 4) . '-styles.css',
401412
'appName' => $appName
402413
])
403414
->willReturn(\OC::$WEBROOT . $result);

0 commit comments

Comments
 (0)