Skip to content

Commit

Permalink
(apache): fix mod_rewrite rule to match Vite build's hashed filenames
Browse files Browse the repository at this point in the history
Sets far future expire headers to the Vite build assets.

Other css/js can be versioned by using similar pattern:

    /path/to/file.12345678.(css|js)

NOTES

The old cache.php used to remove the hash, so that filenames on the
server don't need to change. That doesn't work with Vite's default
output, but then again we rarely need to change the filenames anyway
-- so instead we'll update the filenames for the non-Vite build files
to match the rewrite rule.
  • Loading branch information
fabd committed Oct 19, 2021
1 parent 0dfdfe7 commit 4f63cae
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 8 deletions.
7 changes: 5 additions & 2 deletions src/apps/koohii/modules/study/templates/_SideColumn.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
<?php
use_helper('CJK', 'Form');

// use versioning file pattern here (cf .htaccess rule) for max-age
use_javascript('/revtk/study/keywords-'.CJ_MODE.'-'.$sf_user->getUserSequence().'_v2019.js', 'first', ['defer' => true]);
// file hash matches the ones output by Vite build (cf .htaccess rule)
$FILEHASH = '20211019';
$rtkIndex = $sf_user->getUserSequence(); // 0 = OLD, 1 = NEW
$keywordsFile = "/revtk/study/keywords-rtk-{$rtkIndex}.{$FILEHASH}.js";
use_javascript($keywordsFile, 'first', ['defer' => true]);

//$restudyCount = ReviewsPeer::getRestudyKanjiCount($sf_user->getUserId());

Expand Down
2 changes: 1 addition & 1 deletion src/lib/core/coreWebResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function addViteEntries()
// FIXME : obsolete YUI2 dependency to be phased out
if (!$isLandingPage)
{
$this->addJavascript('/build/yui2/yui2-bundle.min.js', self::FIRST, ['defer' => true]);
$this->addJavascript('/build/yui2/yui2-bundle.20211019.js', self::FIRST, ['defer' => true]);
}

if (KK_ENV_DEV && self::USE_DEV_SERVER && false === $viteClientLoaded)
Expand Down
2 changes: 1 addition & 1 deletion src/web/.htaccess_default
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
RewriteEngine On

# php handler for versioning of front end assets
RewriteRule ^(.*)_v[0-9.]+(css|js)$ /version/cache.php?env=dev&app=revtk&path=$1.$2 [L]
RewriteRule ^(.*)\.[a-z0-9]+\.(css|js)$ /version/cache.php?file=$0 [L]

# we skip all files with .something
RewriteCond %{REQUEST_URI} \..+$
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
11 changes: 7 additions & 4 deletions src/web/version/cache.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
<?php
/**
* Outputs resource with gzip compression and far future expire headers.
*
*
* Query parameters (set by htaccess redirection):
*
* path Absolute path from the web root to resource file (starts with leading slash)
*
* file Absolute path from the web root to resource file
*
* CHANGELOG
*
* Oct 2021 ... updated to match Vite build hashes
*
* @author Fabrice Denis
*/
Expand All @@ -23,7 +26,7 @@ function __construct()

function execute()
{
$filepath = $this->getParameter('path');
$filepath = $this->getParameter('file');

// on web server the path doesn't come with a leading slash, go figure
if (strpos($filepath, DIRECTORY_SEPARATOR) !== 0) {
Expand Down

0 comments on commit 4f63cae

Please sign in to comment.