skin/base: fix static version rendering

This commit is contained in:
Evgeny Zinoviev 2022-07-11 00:14:21 +03:00
parent 8209144cdf
commit 5eecaffdef
2 changed files with 26 additions and 18 deletions

View File

@ -84,6 +84,7 @@ function build_static(): void {
$hashes[$name] = gethash($file); $hashes[$name] = gethash($file);
} }
} }
logInfo($hashes);
// 4. write config-static.php // 4. write config-static.php
$scfg = "<?php\n\n"; $scfg = "<?php\n\n";

View File

@ -76,30 +76,29 @@ function renderStatic($ctx, $static, $theme) {
$dark = $theme == 'dark'; $dark = $theme == 'dark';
$ctx->styleNames = []; $ctx->styleNames = [];
foreach ($static as $name) { foreach ($static as $name) {
// list($name, $options) = $item; // javascript
$version = $config['is_dev'] ? time() : $config['static'][substr($name, 1)] ?? 'notfound';
if (str_ends_with($name, '.js')) if (str_ends_with($name, '.js'))
$html[] = jsLink($name, $version); $html[] = jsLink($name);
// cs
else if (str_ends_with($name, '.css')) { else if (str_ends_with($name, '.css')) {
$html[] = cssLink($name, 'light', $version, $style_name); $html[] = cssLink($name, 'light', $style_name);
$ctx->styleNames[] = $style_name; $ctx->styleNames[] = $style_name;
if ($dark) if ($dark)
$html[] = cssLink($name, 'dark', $version, $style_name); $html[] = cssLink($name, 'dark', $style_name);
else if (!$config['is_dev']) else if (!$config['is_dev'])
$html[] = cssPrefetchLink(str_replace('.css', '_dark.css', $name), $version); $html[] = cssPrefetchLink(str_replace('.css', '_dark.css', $name));
} }
} }
return implode("\n", $html); return implode("\n", $html);
} }
function jsLink(string $name, $version = null): string { function jsLink(string $name): string {
if ($version !== null) return '<script src="'.$name.'?'.getStaticVersion($name).'" type="text/javascript"></script>';
$name .= '?'.$version;
return '<script src="'.$name.'" type="text/javascript"></script>';
} }
function cssLink(string $name, string $theme, $version = null, &$bname = null): string { function cssLink(string $name, string $theme, &$bname = null): string {
global $config; global $config;
$dname = dirname($name); $dname = dirname($name);
@ -108,25 +107,33 @@ function cssLink(string $name, string $theme, $version = null, &$bname = null):
$bname = substr($bname, 0, $pos); $bname = substr($bname, 0, $pos);
if ($config['is_dev']) { if ($config['is_dev']) {
$href = '/sass.php?name='.urlencode($bname).'&amp;theme='.$theme; $href = '/sass.php?name='.urlencode($bname).'&amp;theme='.$theme.'&amp;v='.time();
} else { } else {
$href = $dname.'/'.$bname.($theme == 'dark' ? '_dark' : '').'.css'.($version !== null ? '?'.$version : ''); $version = getStaticVersion('css/'.$bname.($theme == 'dark' ? '_dark' : '').'.css');
$href = $dname.'/'.$bname.($theme == 'dark' ? '_dark' : '').'.css?'.$version;
} }
$id = 'style_'.$bname; $id = 'style_'.$bname;
if ($theme == 'dark') if ($theme == 'dark')
$id .= '_dark'; $id .= '_dark';
return '<link rel="stylesheet" id="'.$id.'" type="text/css" href="'.$href.'">'; return '<link rel="stylesheet" id="'.$id.'" type="text/css" href="'.$href.'">';
} }
function cssPrefetchLink(string $name, $verison = null): string { function cssPrefetchLink(string $name): string {
$url = $name; $url = $name.'?'.getStaticVersion($name);
if ($verison)
$url .= '?'.$verison;
return <<<HTML return <<<HTML
<link rel="prefetch" href="{$url}" /> <link rel="prefetch" href="{$url}" />
HTML; HTML;
} }
function getStaticVersion(string $name): string {
global $config;
if (str_starts_with($name, '/'))
$name = substr($name, 1);
return $config['is_dev'] ? time() : $config['static'][$name] ?? 'notfound';
}
function renderHeader($ctx, $theme, $unsafe_logo_html) { function renderHeader($ctx, $theme, $unsafe_logo_html) {
return <<<HTML return <<<HTML
<div class="head base-width"> <div class="head base-width">