Skip to content

feat (Svg): add hash to filename #423

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

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 1 addition & 1 deletion config/loaders.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ module.exports = {
options: {
extract: true,
publicPath: 'icons/',
spriteFilename: (svgPath) => `${/icons([\\|/])(.*?)\1/gm.exec(svgPath)[2]}.svg`,
spriteFilename: (svgPath) => `${/icons([\\|/])(.*?)\1/gm.exec(svgPath)[2]}.[hash].svg`,
symbolId: (filePath) => `icon-${path.basename(filePath).slice(0, -4)}`,
},
},
Expand Down
6 changes: 3 additions & 3 deletions config/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ module.exports = {
new WebpackImageSizesPlugin({
watch: mode !== 'production',
}),
new WebpackManifestPlugin({
fileName: 'assets.json',
}),
]

if (mode === 'production') {
Expand All @@ -47,9 +50,6 @@ module.exports = {
})
)
plugins.push(
new WebpackManifestPlugin({
fileName: 'assets.json',
}),
new MiniCssExtractPlugin({
filename: '[name].[contenthash:8].min.css',
})
Expand Down
14 changes: 13 additions & 1 deletion inc/Services/Svg.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace BEA\Theme\Framework\Services;

use BEA\Theme\Framework\Framework;
use BEA\Theme\Framework\Service;
use BEA\Theme\Framework\Service_Container;

Expand All @@ -11,11 +12,16 @@
* @package BEA\Theme\Framework
*/
class Svg implements Service {
/**
* @var Assets;
*/
private $assets;

/**
* @param Service_Container $container
*/
public function register( Service_Container $container ): void {
$this->assets = Framework::get_container()->get_service( 'assets' );
add_filter( 'wp_kses_allowed_html', [ $this, 'allow_svg_tag' ] );
}

Expand Down Expand Up @@ -54,12 +60,18 @@ public function get_the_icon( string $icon_class, array $additionnal_classes = [
$icon_class = substr( $icon_class, strpos( $icon_class, '/' ) + 1 );
}

$sprite_path = $this->assets->get_min_file( 'icons/' . $sprite_name . '.svg' );

if ( ! file_exists( \get_theme_file_path( '/dist/' . $sprite_path ) ) ) {
return '';
}

$icon_slug = strpos( $icon_class, 'icon-' ) === 0 ? $icon_class : sprintf( 'icon-%s', $icon_class );
$classes = [ 'icon', $icon_slug ];
$classes = array_merge( $classes, $additionnal_classes );
$classes = array_map( 'sanitize_html_class', $classes );

return sprintf( '<svg class="%s" aria-hidden="true" focusable="false"><use href="%s#%s"></use></svg>', implode( ' ', $classes ), \get_theme_file_uri( sprintf( '/dist/icons/%s.svg', $sprite_name ) ), $icon_slug ); //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
return sprintf( '<svg class="%s" aria-hidden="true" focusable="false"><use href="%s#%s"></use></svg>', implode( ' ', $classes ), \get_theme_file_uri( sprintf( '/dist/%s', $sprite_path ) ), $icon_slug ); //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}

/**
Expand Down
Loading