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

Add versioning for fonts #424

Merged
merged 5 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
32 changes: 25 additions & 7 deletions bin/font-subset.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,17 @@
*/
import { spawn } from 'child_process';
import path from 'path';
import { renameSync, writeFile } from 'fs';
import { existsSync, renameSync, readFileSync, writeFile } from 'fs';
import { createHash } from 'crypto';

function generateHash( filename ) {
filename = path.join( __dirname, `output/${ filename }` )
/*
* Magic Number 12, See get_file_hash()
* https://github.com/WordPress/wporg-mu-plugins/blob/trunk/mu-plugins/cdn/assets.php
*/
return createHash('sha1').update( readFileSync( filename ), 'binary').digest('hex').substring( 0, 12 );
}

const alphabets = [
{
Expand Down Expand Up @@ -89,17 +99,25 @@ let cssCode = '';
// Create our font face rules
// This would need to be modified for other weights and styles
alphabets.forEach( ( alphabet ) => {
let filename = `${ fontFileName }-${ alphabet.name }.woff2`;

if ( ! existsSync( filename ) ) {
return;
}

let hash = generateHash( filename );

cssCode += `
/* ${ alphabet.name } */
@font-face {
@font-face {
font-family: ${ fontFamily };
font-weight: ${ fontWeight };
font-style: ${ fontStyle };
font-display: swap;
src: url(./${ fontFinalDir }/${ fontFileName }-${ alphabet.name }.woff2) format("woff2");
unicode-range: ${ alphabet.unicodeRange };
}
`;
font-display: swap;
src: url(./${ fontFinalDir }/${ filename }?ver=${ hash }) format("woff2");
unicode-range: ${ alphabet.unicodeRange };
}
`;
} );

// Determine where to save our file
Expand Down
20 changes: 16 additions & 4 deletions mu-plugins/global-fonts/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,19 @@ function relative_to_absolute_urls( $editor_settings ) {
}

foreach ( $editor_settings['styles'] as $i => $style ) {
if ( str_contains( $style['css'], './Inter' ) || str_contains( $style['css'], './EB-Garamond' ) ) {
$url = plugins_url( '', __FILE__ );
$style['css'] = str_replace( 'url(./', "url($url/", $style['css'] );
if (
str_contains( $style['css'], './Inter' ) ||
str_contains( $style['css'], './EB-Garamond' ) ||
str_contains( $style['css'], './CourierPrime' ) ||
str_contains( $style['css'], './IBMPlexMono' )
) {
$style['css'] = preg_replace_callback(
'!url\(./(?P<path>[^)]+)\)!i',
function( $m ) {
return "url(" . plugins_url( $m['path'], __FILE__ ) . ")";
}
);

$editor_settings['styles'][ $i ] = $style;
}
}
Expand Down Expand Up @@ -199,5 +209,7 @@ function get_font_url( $font, $subset ) {
return false;
}

return plugins_url( $filepath, __FILE__ );
$font_version = filemtime( __DIR__ . '/' . $filepath );

return plugins_url( "{$filepath}?ver={$font_version}", __FILE__ );
}
Loading