Skip to content

Commit ab5e21f

Browse files
authored
Merge pull request #19 from tattersoftware/use-timestamps
Config useTimestamps
2 parents e79fb19 + eab544b commit ab5e21f

File tree

4 files changed

+35
-1
lines changed

4 files changed

+35
-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: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
$suffix = '?v=' . filemtime($this->config->fileBase . $file);
14+
15+
$this->assertEquals('<script src="http://example.com/assets/alert.js' . $suffix . '" type="text/javascript"></script>', $result);
16+
}
17+
18+
public function testTagRespectsConfigUseTimestamps()
19+
{
20+
$this->config->useTimestamps = false;
21+
22+
$file = 'alert.js';
23+
$assets = new Assets($this->config);
24+
$result = $assets->tag($file);
25+
26+
$this->assertEquals('<script src="http://example.com/assets/alert.js" type="text/javascript"></script>', $result);
27+
}
28+
}

0 commit comments

Comments
 (0)