Skip to content

Commit 931c825

Browse files
committed
Add config option for timestamps, fixes #18
1 parent e79fb19 commit 931c825

File tree

4 files changed

+34
-1
lines changed

4 files changed

+34
-1
lines changed

examples/Assets.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ class Assets extends \Tatter\Assets\Config\Assets
2828
// Starting directory for manifest publication
2929
public $publishBase = ROOTPATH . 'vendor/';
3030

31+
// Whether to append file modification timestamps on asset tags
32+
public $useTimestamps = true;
33+
3134
// Additional paths to load per route
3235
// Relative to fileBase, no leading/trailing slashes
3336
public $routes = [

src/Config/Assets.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ class Assets extends BaseConfig
1919
// Starting directory for manifest publication
2020
public $publishBase = ROOTPATH . 'vendor/';
2121

22+
// Whether to append file modification timestamps on asset tags
23+
public $useTimestamps = true;
24+
2225
// Additional paths to load per route
2326
// Relative to fileBase, no leading/trailing slashes
2427
public $routes = [];

src/Libraries/Assets.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ public function tag(string $path)
186186

187187
// Check for the actual file for version control
188188
$file = $this->config->fileBase . $path;
189-
if (is_file($file))
189+
if (is_file($file) && $this->config->useTimestamps)
190190
{
191191
$url .= '?v=' . filemtime($file);
192192
}

tests/assets/LibraryTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
use CodeIgniter\Config\Services;
4+
use Tatter\Assets\Libraries\Assets;
5+
use Tests\Support\AssetsTestCase;
6+
7+
class LibraryTest extends AssetsTestCase
8+
{
9+
public function testTagAddsTimestamps()
10+
{
11+
$file = 'alert.js';
12+
$result = $this->assets->tag($file);
13+
14+
$this->assertEquals('<script src="http://example.com/assets/alert.js?v=1594481279" type="text/javascript"></script>', $result);
15+
}
16+
17+
public function testTagRespectsConfigUseTimestamps()
18+
{
19+
$this->config->useTimestamps = false;
20+
21+
$file = 'alert.js';
22+
$assets = new Assets($this->config);
23+
$result = $assets->tag($file);
24+
25+
$this->assertEquals('<script src="http://example.com/assets/alert.js" type="text/javascript"></script>', $result);
26+
}
27+
}

0 commit comments

Comments
 (0)