Skip to content

Commit

Permalink
Strip querystring from asset path so mix manifest key matches filename (
Browse files Browse the repository at this point in the history
#40)

* Strip querystring from mix path to avoid timestamped assets causing problems

* Add tests for asset versioning

* Revert auto-formatting

* More formatting fixes

* Fix stan
  • Loading branch information
jrbakooba authored Aug 12, 2022
1 parent 46270b1 commit 8b45718
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/Mix.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ class Mix
*/
public function __invoke($path, $manifestDirectory = '')
{
if (! starts_with($path, '/')) {
$path = preg_replace('/\?.*/', '', $path);
if ($path && ! starts_with($path, '/')) {
$path = "/{$path}";
}

Expand Down
39 changes: 38 additions & 1 deletion tests/TestCase/View/Helper/AssetMixHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ public function setUp()
{
parent::setUp();

mkdir(TEST_APP_DIR . 'webroot');
$webroot = TEST_APP_DIR . 'webroot';
if (! file_exists($webroot)) {
mkdir($webroot);
}

$view = new View();
$this->AssetMix = new AssetMixHelper($view);
Expand Down Expand Up @@ -155,6 +158,23 @@ public function testStyleTagWithVersion()
$this->assertContains('href="/css/main.css?id=9c4259d5465e35535a2a"', $result);
}

/**
* Test `css()` function returns proper tag
* with asset timestamping enabled
*
* @return void
*/
public function testStyleTagWithTimestamp()
{
$this->_copyWithoutVersion();

$result = $this->AssetMix->css('main.css?1660315070');

$this->assertContains('<link', $result);
$this->assertContains('rel="stylesheet"', $result);
$this->assertContains('href="/css/main.css', $result);
}

/**
* Test `script()` function returns proper tag
* with versioning enabled
Expand All @@ -171,4 +191,21 @@ public function testScriptTagWithVersion()
$this->assertContains('/js/app.js?id=f059fcadc7eba26be9ae', $result);
$this->assertContains('defer="defer"', $result);
}

/**
* Test `script()` function returns proper tag
* with asset timestamping enabled
*
* @return void
*/
public function testScriptTagWithTimestamp()
{
$this->_copyWithoutVersion();

$result = $this->AssetMix->script('app.js?1660315070');

$this->assertContains('<script', $result);
$this->assertContains('/js/app.js', $result);
$this->assertContains('defer="defer"', $result);
}
}

0 comments on commit 8b45718

Please sign in to comment.