From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-4.2 required=3.0 tests=ALL_TRUSTED,AWL,BAYES_00, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF shortcircuit=no autolearn=ham autolearn_force=no version=3.4.6 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id BA6111F5D6; Thu, 26 Sep 2024 00:55:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=80x24.org; s=selector1; t=1727312107; bh=GDwwolZb+uZBxvlOwrZg7Eoc8b3h/MFkXWz2OFntynI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Af69jrcmeer8LyGT9iM1bn3Ofspum5WRLnElRFXs2+Ezyi6ZGxMSzGO1HAgqu3HOZ 6c2rONBSx7XnNxvy1THVGbjEzvjVu0vEKi9fiZgeemo6fcVRQ9sdP0lHfC6j/6B0JQ FvnZ7+enthYxGNJLYJftPMpn5Nx7VBlHqQCdb16M= From: Eric Wong To: meta@public-inbox.org Cc: "Robin H. Johnson" Subject: [PATCH 5/5] www: use mtime as CSS cache-buster instead of ctime Date: Thu, 26 Sep 2024 00:55:05 +0000 Message-ID: <20240926005506.3703216-6-e@80x24.org> In-Reply-To: <20240926005506.3703216-1-e@80x24.org> References: <20240926005506.3703216-1-e@80x24.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: 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 | 5 +++-- lib/PublicInbox/WWW.pm | 22 +++++++++++----------- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/lib/PublicInbox/UserContent.pm b/lib/PublicInbox/UserContent.pm index 9dacfa0b..1ad8eba7 100644 --- a/lib/PublicInbox/UserContent.pm +++ b/lib/PublicInbox/UserContent.pm @@ -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: diff --git a/lib/PublicInbox/WWW.pm b/lib/PublicInbox/WWW.pm index 60c437a5..06889861 100644 --- a/lib/PublicInbox/WWW.pm +++ b/lib/PublicInbox/WWW.pm @@ -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 ] ];