Skip to content

Commit b9464d1

Browse files
authored
Fix Last-Modified header on archive pages (#2042)
1 parent e10aeab commit b9464d1

2 files changed

Lines changed: 22 additions & 2 deletions

File tree

‎include/layout.inc‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,21 @@ function news_archive_sidebar(): void
367367
}
368368
}
369369

370+
// Get Last-Modified timestamp for a year's archive page:
371+
// mtime of file or latest article, whichever is newer
372+
/** @param list<array<array-key, mixed>> $news */
373+
function news_archive_last_modified(array $news, string $pageFile): int
374+
{
375+
$timestamps = array_map(static fn (array $entry): int => strtotime($entry['published']), $news);
376+
377+
$mtime = filemtime($pageFile);
378+
if ($mtime !== false) {
379+
$timestamps[] = $mtime;
380+
}
381+
382+
return $timestamps === [] ? time() : max($timestamps);
383+
}
384+
370385
// Print news
371386
function print_news($news, $dog, $max = 5, $return = false) {
372387
$retval = [];

‎public/archive/2026.php‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@
55
$_SERVER['BASE_PAGE'] = 'archive/2026.php';
66
require_once __DIR__ . '/../../include/prepend.inc';
77
news_archive_sidebar();
8-
site_header("News Archive - 2026", ['cache' => true, 'cache_control' => 15 * 60]);
8+
$news = (new NewsHandler())->getNewsByYear(2026);
9+
10+
site_header("News Archive - 2026", [
11+
'cache' => news_archive_last_modified($news, __FILE__),
12+
'cache_control' => 15 * 60,
13+
]);
914
?>
1015

1116
<h1>News Archive - 2026</h1>
@@ -18,5 +23,5 @@
1823

1924
<?php
2025

21-
print_news((new NewsHandler())->getNewsByYear(2026), null, 500);
26+
print_news($news, null, 500);
2227
site_footer(['elephpants' => true, 'sidebar' => $SIDEBAR_DATA]);

0 commit comments

Comments
 (0)