Skip to content

Commit 796b4f1

Browse files
committed
Add Cache-control: immutable
Cache generated CSS forever! Also cache combined JS forever Fix tests Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
1 parent 9834f33 commit 796b4f1

4 files changed

Lines changed: 38 additions & 33 deletions

File tree

core/Controller/CssController.php

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
declare(strict_types=1);
23
/**
34
* @copyright Copyright (c) 2016, John Molakvoæ (skjnldsv@protonmail.com)
45
*
@@ -31,11 +32,13 @@
3132
use OCP\AppFramework\Http;
3233
use OCP\AppFramework\Http\NotFoundResponse;
3334
use OCP\AppFramework\Http\FileDisplayResponse;
35+
use OCP\AppFramework\Http\Response;
3436
use OCP\AppFramework\Utility\ITimeFactory;
3537
use OCP\Files\IAppData;
3638
use OCP\Files\NotFoundException;
3739
use OCP\Files\SimpleFS\ISimpleFile;
3840
use OCP\Files\SimpleFS\ISimpleFolder;
41+
use OCP\IConfig;
3942
use OCP\IRequest;
4043

4144
class CssController extends Controller {
@@ -46,13 +49,10 @@ class CssController extends Controller {
4649
/** @var ITimeFactory */
4750
protected $timeFactory;
4851

49-
/**
50-
* @param string $appName
51-
* @param IRequest $request
52-
* @param Factory $appDataFactory
53-
* @param ITimeFactory $timeFactory
54-
*/
55-
public function __construct($appName, IRequest $request, Factory $appDataFactory, ITimeFactory $timeFactory) {
52+
public function __construct(string $appName,
53+
IRequest $request,
54+
Factory $appDataFactory,
55+
ITimeFactory $timeFactory) {
5656
parent::__construct($appName, $request);
5757

5858
$this->appData = $appDataFactory->get('css');
@@ -67,7 +67,7 @@ public function __construct($appName, IRequest $request, Factory $appDataFactory
6767
* @param string $appName css folder name
6868
* @return FileDisplayResponse|NotFoundResponse
6969
*/
70-
public function getCss($fileName, $appName) {
70+
public function getCss(string $fileName, string $appName): Response {
7171
try {
7272
$folder = $this->appData->getFolder($appName);
7373
$gzip = false;
@@ -80,10 +80,13 @@ public function getCss($fileName, $appName) {
8080
if ($gzip) {
8181
$response->addHeader('Content-Encoding', 'gzip');
8282
}
83-
$response->cacheFor(86400);
83+
84+
$ttl = 31536000;
85+
$response->addHeader('Cache-Control', 'max-age='.$ttl.', immutable');
86+
8487
$expires = new \DateTime();
8588
$expires->setTimestamp($this->timeFactory->getTime());
86-
$expires->add(new \DateInterval('PT24H'));
89+
$expires->add(new \DateInterval('PT'.$ttl.'S'));
8790
$response->addHeader('Expires', $expires->format(\DateTime::RFC1123));
8891
$response->addHeader('Pragma', 'cache');
8992
return $response;
@@ -94,8 +97,9 @@ public function getCss($fileName, $appName) {
9497
* @param string $fileName
9598
* @param bool $gzip is set to true if we use the gzip file
9699
* @return ISimpleFile
100+
* @throws NotFoundException
97101
*/
98-
private function getFile(ISimpleFolder $folder, $fileName, &$gzip) {
102+
private function getFile(ISimpleFolder $folder, string $fileName, bool &$gzip): ISimpleFile {
99103
$encoding = $this->request->getHeader('Accept-Encoding');
100104

101105
if (strpos($encoding, 'gzip') !== false) {

core/Controller/JsController.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
declare(strict_types=1);
23
/**
34
* @copyright 2017, Roeland Jago Douma <roeland@famdouma.nl>
45
*
@@ -29,6 +30,7 @@
2930
use OCP\AppFramework\Http;
3031
use OCP\AppFramework\Http\NotFoundResponse;
3132
use OCP\AppFramework\Http\FileDisplayResponse;
33+
use OCP\AppFramework\Http\Response;
3234
use OCP\AppFramework\Utility\ITimeFactory;
3335
use OCP\Files\IAppData;
3436
use OCP\Files\NotFoundException;
@@ -44,12 +46,6 @@ class JsController extends Controller {
4446
/** @var ITimeFactory */
4547
protected $timeFactory;
4648

47-
/**
48-
* @param string $appName
49-
* @param IRequest $request
50-
* @param Factory $appDataFactory
51-
* @param ITimeFactory $timeFactory
52-
*/
5349
public function __construct($appName, IRequest $request, Factory $appDataFactory, ITimeFactory $timeFactory) {
5450
parent::__construct($appName, $request);
5551

@@ -65,7 +61,7 @@ public function __construct($appName, IRequest $request, Factory $appDataFactory
6561
* @param string $appName css folder name
6662
* @return FileDisplayResponse|NotFoundResponse
6763
*/
68-
public function getJs($fileName, $appName) {
64+
public function getJs(string $fileName, string $appName): Response {
6965
try {
7066
$folder = $this->appData->getFolder($appName);
7167
$gzip = false;
@@ -78,10 +74,13 @@ public function getJs($fileName, $appName) {
7874
if ($gzip) {
7975
$response->addHeader('Content-Encoding', 'gzip');
8076
}
81-
$response->cacheFor(86400);
77+
78+
$ttl = 31536000;
79+
$response->addHeader('Cache-Control', 'max-age='.$ttl.', immutable');
80+
8281
$expires = new \DateTime();
8382
$expires->setTimestamp($this->timeFactory->getTime());
84-
$expires->add(new \DateInterval('PT24H'));
83+
$expires->add(new \DateInterval('PT'.$ttl.'S'));
8584
$response->addHeader('Expires', $expires->format(\DateTime::RFC1123));
8685
$response->addHeader('Pragma', 'cache');
8786
return $response;
@@ -92,8 +91,10 @@ public function getJs($fileName, $appName) {
9291
* @param string $fileName
9392
* @param bool $gzip is set to true if we use the gzip file
9493
* @return ISimpleFile
94+
*
95+
* @throws NotFoundException
9596
*/
96-
private function getFile(ISimpleFolder $folder, $fileName, &$gzip) {
97+
private function getFile(ISimpleFolder $folder, string $fileName, bool &$gzip): ISimpleFile {
9798
$encoding = $this->request->getHeader('Accept-Encoding');
9899

99100
if (strpos($encoding, 'gzip') !== false) {

tests/Core/Controller/CssControllerTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,10 @@ public function testGetFile() {
109109
->willReturn($file);
110110

111111
$expected = new FileDisplayResponse($file, Http::STATUS_OK, ['Content-Type' => 'text/css']);
112-
$expected->cacheFor(86400);
112+
$expected->addHeader('Cache-Control', 'max-age=31536000, immutable');
113113
$expires = new \DateTime();
114114
$expires->setTimestamp(1337);
115-
$expires->add(new \DateInterval('PT24H'));
115+
$expires->add(new \DateInterval('PT31536000S'));
116116
$expected->addHeader('Expires', $expires->format(\DateTime::RFC1123));
117117
$expected->addHeader('Pragma', 'cache');
118118

@@ -137,10 +137,10 @@ public function testGetGzipFile() {
137137

138138
$expected = new FileDisplayResponse($gzipFile, Http::STATUS_OK, ['Content-Type' => 'text/css']);
139139
$expected->addHeader('Content-Encoding', 'gzip');
140-
$expected->cacheFor(86400);
140+
$expected->addHeader('Cache-Control', 'max-age=31536000, immutable');
141141
$expires = new \DateTime();
142142
$expires->setTimestamp(1337);
143-
$expires->add(new \DateInterval('PT24H'));
143+
$expires->add(new \DateInterval('PT31536000S'));
144144
$expected->addHeader('Expires', $expires->format(\DateTime::RFC1123));
145145
$expected->addHeader('Pragma', 'cache');
146146

@@ -170,10 +170,10 @@ function($fileName) use ($file) {
170170
->willReturn('gzip, deflate');
171171

172172
$expected = new FileDisplayResponse($file, Http::STATUS_OK, ['Content-Type' => 'text/css']);
173-
$expected->cacheFor(86400);
173+
$expected->addHeader('Cache-Control', 'max-age=31536000, immutable');
174174
$expires = new \DateTime();
175175
$expires->setTimestamp(1337);
176-
$expires->add(new \DateInterval('PT24H'));
176+
$expires->add(new \DateInterval('PT31536000S'));
177177
$expected->addHeader('Expires', $expires->format(\DateTime::RFC1123));
178178
$expected->addHeader('Pragma', 'cache');
179179

tests/Core/Controller/JsControllerTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,10 @@ public function testGetFile() {
109109
->willReturn($file);
110110

111111
$expected = new FileDisplayResponse($file, Http::STATUS_OK, ['Content-Type' => 'application/javascript']);
112-
$expected->cacheFor(86400);
112+
$expected->addHeader('Cache-Control', 'max-age=31536000, immutable');
113113
$expires = new \DateTime();
114114
$expires->setTimestamp(1337);
115-
$expires->add(new \DateInterval('PT24H'));
115+
$expires->add(new \DateInterval('PT31536000S'));
116116
$expected->addHeader('Expires', $expires->format(\DateTime::RFC1123));
117117
$expected->addHeader('Pragma', 'cache');
118118

@@ -137,10 +137,10 @@ public function testGetGzipFile() {
137137

138138
$expected = new FileDisplayResponse($gzipFile, Http::STATUS_OK, ['Content-Type' => 'application/javascript']);
139139
$expected->addHeader('Content-Encoding', 'gzip');
140-
$expected->cacheFor(86400);
140+
$expected->addHeader('Cache-Control', 'max-age=31536000, immutable');
141141
$expires = new \DateTime();
142142
$expires->setTimestamp(1337);
143-
$expires->add(new \DateInterval('PT24H'));
143+
$expires->add(new \DateInterval('PT31536000S'));
144144
$expected->addHeader('Expires', $expires->format(\DateTime::RFC1123));
145145
$expected->addHeader('Pragma', 'cache');
146146

@@ -170,10 +170,10 @@ function($fileName) use ($file) {
170170
->willReturn('gzip, deflate');
171171

172172
$expected = new FileDisplayResponse($file, Http::STATUS_OK, ['Content-Type' => 'application/javascript']);
173-
$expected->cacheFor(86400);
173+
$expected->addHeader('Cache-Control', 'max-age=31536000, immutable');
174174
$expires = new \DateTime();
175175
$expires->setTimestamp(1337);
176-
$expires->add(new \DateInterval('PT24H'));
176+
$expires->add(new \DateInterval('PT31536000S'));
177177
$expected->addHeader('Expires', $expires->format(\DateTime::RFC1123));
178178
$expected->addHeader('Pragma', 'cache');
179179

0 commit comments

Comments
 (0)