serve 88x31 buttons from cache URLs instead of base64, preload the art font
Deploy / deploy (push) Successful in 3s
Deploy / deploy (push) Successful in 3s
This commit is contained in:
+16
-16
@@ -20,10 +20,10 @@ const BUTTONS_88x31 = [
|
||||
const BUTTON_CACHE_TTL = 7 * 86400; // re-fetch each button at most once a week
|
||||
|
||||
/**
|
||||
* Fetch a remote image, cache it locally, and return it as a base64 data: URI.
|
||||
* Returns null on failure so the caller can fall back to hotlinking.
|
||||
* Fetch a remote image into the local cache (at most once a week) and return
|
||||
* the cache file's path, or null if it couldn't be fetched.
|
||||
*/
|
||||
function button_data_uri(string $url): ?string
|
||||
function button_cache(string $url): ?string
|
||||
{
|
||||
$dir = DATA_DIR . '/buttons';
|
||||
@mkdir($dir, 0775, true);
|
||||
@@ -47,19 +47,19 @@ function button_data_uri(string $url): ?string
|
||||
}
|
||||
}
|
||||
|
||||
if (!is_file($file)) {
|
||||
return null;
|
||||
}
|
||||
return is_file($file) ? $file : null;
|
||||
}
|
||||
|
||||
$raw = (string) file_get_contents($file);
|
||||
$nl = strpos($raw, "\n");
|
||||
if ($nl === false) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$type = substr($raw, 0, $nl);
|
||||
$data = substr($raw, $nl + 1);
|
||||
return 'data:' . $type . ';base64,' . base64_encode($data);
|
||||
/**
|
||||
* Where the page should load a button from: our own copy (button.php serves
|
||||
* it, versioned by the cache file's mtime so browsers can keep it), or the
|
||||
* original if fetching failed. Was inlined as base64 before, which made every
|
||||
* page ~260 KB heavier.
|
||||
*/
|
||||
function button_src(string $url): string
|
||||
{
|
||||
$file = button_cache($url);
|
||||
return $file ? '/button.php?h=' . basename($file) . '&v=' . filemtime($file) : $url;
|
||||
}
|
||||
|
||||
/** Render the whole 88x31 button row as HTML. */
|
||||
@@ -67,7 +67,7 @@ function render_buttons_88x31(): string
|
||||
{
|
||||
$html = '';
|
||||
foreach (BUTTONS_88x31 as $b) {
|
||||
$uri = button_data_uri($b['src']) ?? $b['src']; // fall back to hotlink if fetch failed
|
||||
$uri = button_src($b['src']);
|
||||
$img = '<img src="' . htmlspecialchars($uri) . '" alt="' . htmlspecialchars($b['alt']) . '" width="88" height="31">';
|
||||
$href = $b['href'] ?? '';
|
||||
if ($href !== '') {
|
||||
|
||||
Reference in New Issue
Block a user