]> git.ipfire.org Git - thirdparty/public-inbox.git/commitdiff
www: use mtime as CSS cache-buster instead of ctime
authorEric Wong <e@80x24.org>
Thu, 26 Sep 2024 00:55:05 +0000 (00:55 +0000)
committerEric Wong <e@80x24.org>
Sun, 29 Sep 2024 00:23:03 +0000 (00:23 +0000)
mtime can be synchronized across multiple machines via package
managers, tarballs, rsync deploys, or tools like
`git-set-file-times'.  This synchronization increases cache hit
rates for browsers hitting multi-host public-inbox instances
loadbalancing behind a single $hostname:$port identity.

While mtime may be less correct, it's unusual that anyone would
want to intentionally alter or preserve mtime after a file is
changed on the FS.

lib/PublicInbox/UserContent.pm
lib/PublicInbox/WWW.pm

index 9dacfa0bef6f9dee350a89e7cd6e2ab9ba795251..1ad8eba7270270209b78b058e0a4b45796b48878 100644 (file)
@@ -74,7 +74,7 @@ _
 sub sample ($) {
        my ($ctx) = @_;
        my $url_prefix = $ctx->{ibx}->base_url($ctx->{env});
-       my $preamble = <<"";
+       my $css = <<"";
 /*
  * Firefox users: this goes in \$PROFILE_FOLDER/chrome/userContent.css
  * where \$PROFILE_FOLDER is platform-specific
@@ -87,7 +87,8 @@ sub sample ($) {
  */
 \@-moz-document url-prefix($url_prefix) { /* moz-only */
 
-       $preamble . CSS . "\n} /* moz-only */\n";
+       $css .= CSS . "\n} /* moz-only */\n";
+       wantarray ? ($css, (stat(__FILE__))[9]) : $css;
 }
 
 # Auto-update this file based on the contents of a CSS file:
index 987d644e9aef410c0ed7026f55f770ecf4151367..4dfe83c135a95e3b241f7ae098a44e6a8882d0a0 100644 (file)
@@ -568,14 +568,14 @@ our $STYLE = 'pre{white-space:pre-wrap}*{font-size:100%;font-family:monospace}';
 
 sub _read_css ($$$) {
        my ($fh, $mini, $fn) = @_;
-       my $ctime = 0;
+       my $mtime = 0;
        my $local = PublicInbox::IO::read_all $fh; # sets _
        if ($local =~ /\S/) {
-               $ctime = sprintf('%x',(stat(_))[10]);
+               $mtime = sprintf('%x',(stat(_))[9]);
                $local = $mini->($local);
        }
        # do not let BOFHs override userContent.css:
-       return ($local, $ctime) if $local !~ /!\s*important\b/i;
+       return ($local, $mtime) if $local !~ /!\s*important\b/i;
        warn "W: ignoring $fn since it uses `!important'\n";
        ();
 }
@@ -626,22 +626,22 @@ sub stylesheets_prepare ($$) {
                                warn "ignoring $fn, non-ASCII word character\n";
                                next;
                        }
-                       my ($local, $ctime);
+                       my ($local, $mtime);
                        if (my $rec = $css_map->{$key}) { # already loaded
-                               ($local, $ctime) = @$rec;
+                               ($local, $mtime) = @$rec;
                        } elsif (open(my $fh, '<', $fn)) {
-                               ($local, $ctime) = _read_css $fh, $mini, $fn;
+                               ($local, $mtime) = _read_css $fh, $mini, $fn;
                                if ($local =~ /\@import\b/) {
                                        $import = $attr->{-do_import} = 1;
                                        push @css_dir, $dir if !$css_dir{$dir}++
                                }
-                               $css_map->{$key} = [ $local, $ctime ];
+                               $css_map->{$key} = [ $local, $mtime ];
                        } else {
                                warn "failed to open $fn: $!\n";
                                next;
                        }
 
-                       $attr->{href} = "$upfx$key.css?$ctime";
+                       $attr->{href} = "$upfx$key.css?$mtime";
                        if (defined($attr->{title})) { # browser-selectable
                                $inline_ok = 0;
                        } elsif (($attr->{media}||'screen') eq 'screen') {
@@ -710,9 +710,9 @@ sub stylesheets_prepare ($$) {
                                # no warning for autoloaded CSS
                                open my $fh, '<', $fn or next;
                                -T $fh or next;
-                               my ($local, $ctime) =
+                               my ($local, $mtime) =
                                                _read_css $fh, $mini, "$d/$fn";
-                               $css_map->{$key} = [ $local, $ctime ];
+                               $css_map->{$key} = [ $local, $mtime ];
                        }
                        chdir $cwddh;
                }
@@ -748,7 +748,7 @@ sub get_css ($$$) {
                $rec = [ PublicInbox::UserContent::sample($ctx) ];
        }
        $rec // return r404();
-       my ($css, undef) = @$rec; # TODO: Last-Modified
+       my ($css, undef) = @$rec; # TODO: Last-Modified + If-Modified-Since
        my $h = [ 'Content-Length', length($css), 'Content-Type', 'text/css' ];
        PublicInbox::GitHTTPBackend::cache_one_year($h);
        [ 200, $h, [ $css ] ];