Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions app/Controllers/Findingaid.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ public function show()
return;
}

$css = [
'extra_version' => $this->assetVersion('css/extra.css'),
'mediaelementplayer_version' => $this->assetVersion('css/mediaelementplayer.min.css'),
];

/* First, fill out top-level metadata, including the
* table of contents.
*/
Expand Down Expand Up @@ -305,17 +310,10 @@ public function show()
'content' => $content,
'toc' => $toc,
'requests' => $requests,
'css' => $css,
'js' => [[
'href' => 'js/app.js',
'hash' => hash_file('sha256', implode(
DIRECTORY_SEPARATOR,
[
ROOT,
'public',
'js',
'app.js',
]
)),
'version' => $this->assetVersion('js/app.js'),
]],
'title' => $model->title(),
'requestable' => $requestable,
Expand Down Expand Up @@ -353,6 +351,7 @@ public function show()
[
'title' => $meta['title'],
'repository' => $meta['repository'],
'css' => $css,
]
);
} else {
Expand All @@ -362,6 +361,7 @@ public function show()
'title' => $meta['title'],
'repository' => $meta['repository'],
'repo_url' => $repo_url,
'css' => $css,
]
);
}
Expand Down
12 changes: 12 additions & 0 deletions app/Core/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,16 @@ public function __construct(protected $params = [])
global $g_config;
$this->config = $g_config;
}

public function assetVersion($path)
{
$file_path = implode(DIRECTORY_SEPARATOR, [
realpath(ROOT),
'public',
$path,
]);
$algo = 'sha384';
$version = $algo . '-' . base64_encode(hash($algo, file_get_contents($file_path), true));
return $version;
}
}
4 changes: 2 additions & 2 deletions app/Views/Euk_Shared/head-common.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
<link rel="preconnect" href="https://fonts.gstatic.com">

<!--shared css-->
<link rel="stylesheet" href="css/extra.css">
<link rel="stylesheet" href="css/mediaelementplayer.min.css">
<link rel="stylesheet" href="css/extra.css?{{css.extra_version}}">
<link rel="stylesheet" href="css/mediaelementplayer.min.css?{{css.mediaelementplayer_version}}">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/lity/2.3.1/lity.min.css" integrity="sha256-0SZxASYAglrmIuTx+ZYHE3hzTnCZWB7XLu+iA8AG0Z0=" crossorigin="anonymous" />

<!--Limestone CSS -->
Expand Down
2 changes: 1 addition & 1 deletion app/Views/Layouts/application.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
{{> universal-footer}}
{{> limestone-resources}}
{{#js}}
<script src="{{href}}?hash={{hash}}"></script>
<script src="{{href}}?{{version}}"></script>
{{/js}}
{{#requestable}}
<script>
Expand Down
22 changes: 22 additions & 0 deletions tests/ControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Tests\Unit;

use App\Core\Controller;
use PHPUnit\Framework\TestCase;

class ControllerTest extends TestCase
{
public function testAssetVersionIsASha384DigestOfTheAssetContents(): void
{
$path = 'css/extra.css';
$contents = file_get_contents(implode(DIRECTORY_SEPARATOR, [
ROOT,
'public',
$path,
]));
$expected = 'sha384-' . base64_encode(hash('sha384', $contents, true));

$this->assertSame($expected, (new Controller())->assetVersion($path));
}
}
Loading