Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

All: add jq_url_append_version() #470

Merged
merged 2 commits into from
Oct 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions themes/jquery/functions.jquery.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,19 @@ function jq_search_get_provider() {
}

/**
* Get a checksum of styles used in the header
* Try to append a "?v=" cache buster to a static CSS or JS file URI.
*/
function jq_css_checksum() {
$base = get_template_directory() . '/css/base.css';
$typesense = get_template_directory() . '/lib/typesense-minibar/typesense-minibar.css';
$styles = get_stylesheet_directory() . '/style.css';
function jq_url_append_version($uri) {
Krinkle marked this conversation as resolved.
Show resolved Hide resolved
foreach ( [
get_stylesheet_directory_uri() => get_stylesheet_directory(),
get_template_directory_uri() => get_template_directory(),
] as $uriPrefix => $directory ) {
if ( str_starts_with( $uri, $uriPrefix ) ) {
$filepath = strtr( $uri, [ $uriPrefix => $directory ] );
return $uri . '?v=' . substr( @md5_file( $filepath ) ?: '', 0, 8 );
}
}

return md5( filemtime( $base ) . filemtime( $typesense ) . filemtime( $styles ) );
// Unchanged
return $uri;
}
10 changes: 5 additions & 5 deletions themes/jquery/header.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@
<meta name="viewport" content="width=device-width">

<link rel="shortcut icon" href="<?php echo get_stylesheet_directory_uri(); ?>/i/favicon.ico">
<link rel="stylesheet" href="<?php echo get_template_directory_uri(); ?>/lib/typesense-minibar/typesense-minibar.css?v=<?php echo jq_css_checksum(); ?>">
<link rel="stylesheet" href="<?php echo get_template_directory_uri(); ?>/css/base.css?v=<?php echo jq_css_checksum(); ?>">
<link rel="stylesheet" href="<?php bloginfo( 'stylesheet_url' ); ?>?v=<?php echo jq_css_checksum(); ?>">
<link rel="stylesheet" href="<?php echo jq_url_append_version( get_template_directory_uri() . '/lib/typesense-minibar/typesense-minibar.css' ); ?>">
<link rel="stylesheet" href="<?php echo jq_url_append_version( get_template_directory_uri() . '/css/base.css' ); ?>">
<link rel="stylesheet" href="<?php echo jq_url_append_version( get_bloginfo( 'stylesheet_url' ) ); ?>">

<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<script src="<?php echo get_template_directory_uri(); ?>/js/main.js"></script>
<script src="<?php echo jq_url_append_version( get_template_directory_uri() . '/js/main.js' ); ?>"></script>
<?php
if ( jq_search_get_provider() === 'typesense' ) :
?>
<script defer type="module" src="<?php echo get_template_directory_uri(); ?>/lib/typesense-minibar/typesense-minibar.js?v=<?php echo jq_css_checksum(); ?>"></script>
<script defer type="module" src="<?php echo jq_url_append_version( get_template_directory_uri() . '/lib/typesense-minibar/typesense-minibar.js' ); ?>"></script>
<?php
endif;

Expand Down