mirror of
https://codeberg.org/PostERG/xamxam.git
synced 2026-05-06 11:09:18 +02:00
cache-bust: add filemtime-based versioning to all CSS and JS assets
This commit is contained in:
13
src/App.php
13
src/App.php
@@ -136,6 +136,19 @@ class App
|
||||
exit;
|
||||
}
|
||||
|
||||
// ── Asset versioning ─────────────────────────────────────────────────────
|
||||
|
||||
/**
|
||||
* Return an asset URL with a filemtime-based cache-busting query string.
|
||||
* Input is a root-relative URL path (e.g. /assets/css/main.css).
|
||||
*/
|
||||
public static function assetV(string $urlPath): string
|
||||
{
|
||||
$file = APP_ROOT . '/public' . $urlPath;
|
||||
$v = file_exists($file) ? filemtime($file) : 0;
|
||||
return $urlPath . ($v ? '?v=' . $v : '');
|
||||
}
|
||||
|
||||
// ── Template rendering ────────────────────────────────────────────────────
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
<?php foreach ($extraJs ?? [] as $js): ?>
|
||||
<script src="<?= htmlspecialchars($js) ?>"></script>
|
||||
<script src="<?= App::assetV($js) ?>"></script>
|
||||
<?php endforeach; ?>
|
||||
<?php if (!empty($extraJsInline)): ?>
|
||||
<script><?= $extraJsInline ?></script>
|
||||
|
||||
@@ -64,10 +64,10 @@
|
||||
<link rel="icon" type="image/png" sizes="16x16" href="/assets/favicon/favicon-16x16.png">
|
||||
<link rel="shortcut icon" href="/assets/favicon/favicon.ico">
|
||||
<meta name="theme-color" content="#ffffff">
|
||||
<link rel="stylesheet" href="/assets/css/modern-normalize.min.css">
|
||||
<link rel="stylesheet" href="/assets/css/common.css">
|
||||
<link rel="stylesheet" href="<?= App::assetV('/assets/css/modern-normalize.min.css') ?>">
|
||||
<link rel="stylesheet" href="<?= App::assetV('/assets/css/common.css') ?>">
|
||||
<?php foreach ($extraCss ?? [] as $css): ?>
|
||||
<link rel="stylesheet" href="<?= htmlspecialchars($css) ?>">
|
||||
<link rel="stylesheet" href="<?= App::assetV($css) ?>">
|
||||
<?php endforeach; ?>
|
||||
<?php if (php_sapi_name() === 'cli-server'): ?>
|
||||
<script>
|
||||
|
||||
Reference in New Issue
Block a user