version_asset_links.php
This script implements CSS-level cache busting by scanning generated CSS files for asset URLs (like SVG icons) and appending version query parameters based on file modification.
Read this when Use this page when tracing PHP asset generation, build hooks, template assembly, or maintenance scripts around version_asset_links.
Overview
This script implements CSS-level cache busting by scanning generated CSS files for asset URLs (like SVG icons) and appending version query parameters based on file modification times. Unlike build_functions.php which generates versioned URLs for <link> and <script> tags, this script modifies the CSS files themselves to version-stamp URLs they contain.
The script runs during the build pipeline after CSS generation but before final deployment. It reads -GENERATED.css files, identifies asset references (currently just icons.svg), calculates their modification timestamps, performs string replacement to add ?v={timestamp} parameters, and writes the result to -VERSIONED.css files.
This two-stage approach (GENERATED → VERSIONED) ensures that CSS files reference the most current versions of assets they depend on, preventing browser cache staleness when icons or other embedded resources are updated.
Key Operations
-
Asset inventory: Builds a map of asset paths to modification times
- Currently limited to
{$icon_dir}/icons.svg - Can be extended to other embedded assets
- Currently limited to
-
CSS processing: For each target CSS file:
- Reads the
-GENERATEDvariant - Replaces asset URLs with versioned variants
- Writes to
-VERSIONEDoutput file - Records output path in
$updated_files
- Reads the
-
Target files:
{$css_dir}/head-GENERATED.css→head-VERSIONED.css{$css_dir}/style-GENERATED.css→style-VERSIONED.css
Input/Output
Input:
{$css_dir}/head-GENERATED.css- Generated critical CSS{$css_dir}/style-GENERATED.css- Generated main stylesheet{$icon_dir}/icons.svg- SVG icon sprite (for timestamp)
Output:
{$css_dir}/head-VERSIONED.css- CSS with versioned asset URLs{$css_dir}/style-VERSIONED.css- CSS with versioned asset URLs- Updates
$updated_filesarray with output paths
Usage
Invoked by the build system after CSS generation:
php build/version_asset_links.php
Prints: Versioning assets links...
It is invoked by the Git pre-commit hook (pre-commit.hook.php) during the asset pipeline:
# After CSS bundling:
php build/build_unified_assets.php # Produces *-GENERATED.css files
# Version asset references within CSS:
php build/version_asset_links.php # Produces *-VERSIONED.css files
# The *-VERSIONED.css files are then referenced by the include builders
The script requires that:
- The
-GENERATED.cssfiles already exist (produced by earlier build steps) - The asset files being referenced (like
icons.svg) exist and are readable - The output directory is writable
See Also
- build_functions.php - Versions
<link>and<script>tag URLs in HTML - build_paths.php - Provides directory path constants
- build_variables.php - Tracks which files were updated
- build_unified_assets.php - Creates the GENERATED CSS files this script processes
- build_head_includes.php - References the VERSIONED CSS in HTML includes
- pre-commit-hook.php - Git pre-commit hook that invokes this script