Skip to content

Commit

Permalink
Asset Manifest Bug Fix (#8)
Browse files Browse the repository at this point in the history
* change stylesheet directory uri to just directory to get path

* adjust asset loader to be able to default to file path

* removes testing URL

* removes check of filename in assetLoader

* - Retrofit the asset loader configuration to provide a production file path
- The WordPress loader will add the absolute theme path to files if not provided

* - Add a shorter timeout for manifest requests over HTTP

* - Update code documentation for timeout

---------

Co-authored-by: Miriam Goldman <miriam@kanopi.com>
  • Loading branch information
rleeson and Miriam Goldman authored Apr 26, 2023
1 parent e70874c commit c9891c3
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 26 deletions.
10 changes: 7 additions & 3 deletions src/AssetLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,18 @@ protected function starts_with( string $_subject, string $_test ): bool {
}

protected function asset_manifest() {
$base = $this->use_production ? $this->_base_url : $this->_development_url_base;
$base = $this->use_production ? $this->_configuration->production_file_path() : $this->_development_url_base;
$filename = $base . $this->_configuration->asset_manifest_path();
$manifest = null;
$use_context = $this->starts_with( $filename, 'http' );

if ( file_exists( $filename ) || $use_context ) {
// Set timeout to 15 seconds for HTTP requests
$context_arguments = [
'http' => [ 'ignore_errors' => true ]
'http' => [
'ignore_errors' => true,
'timeout' => 15,
]
];

if ( $this->starts_with( $filename, 'https' ) ) {
Expand Down Expand Up @@ -457,4 +461,4 @@ public function static_assets_url( string $_file_path ): string {
. $this->_configuration->static_path()
. $_file_path;
}
}
}
39 changes: 28 additions & 11 deletions src/Model/LoaderConfiguration.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php
/**
* Webpack Asset Loader Configuration
*/
Expand Down Expand Up @@ -29,6 +29,13 @@ class LoaderConfiguration {
*/
protected array $_production_domains;

/**
* Root file path to the files in production, to find the asset manifest
*
* @var string
*/
protected string $_production_file_path;

/**
* @var string
*/
Expand Down Expand Up @@ -59,6 +66,7 @@ class LoaderConfiguration {
* @param ?string $_script_path
* @param ?string $_style_path
* @param ?string $_static_path
* @param ?string $_production_file_path
*/
public function __construct(
?string $_version = null,
Expand All @@ -67,15 +75,17 @@ public function __construct(
?string $_handle_prefix = null,
?string $_script_path = null,
?string $_style_path = null,
?string $_static_path = null
?string $_static_path = null,
?string $_production_file_path = null
) {
$this->_version = $this->check_string( $_version, null );
$this->_asset_manifest_path = $this->check_string( $_asset_manifest_path, '' );
$this->_handle_prefix = $this->check_string( $_handle_prefix, self::DEFAULT_PREFIX );
$this->_script_path = $this->check_string( $_script_path, self::DEFAULT_SCRIPT_PATH );
$this->_static_path = $this->check_string( $_static_path, self::DEFAULT_STATIC_PATH );
$this->_style_path = $this->check_string( $_style_path, self::DEFAULT_STYLE_PATH );
$this->_production_domains = is_array( $_production_domains ) ? $_production_domains : [];
$this->_version = $this->check_string( $_version, null );
$this->_asset_manifest_path = $this->check_string( $_asset_manifest_path, '' );
$this->_handle_prefix = $this->check_string( $_handle_prefix, self::DEFAULT_PREFIX );
$this->_script_path = $this->check_string( $_script_path, self::DEFAULT_SCRIPT_PATH );
$this->_static_path = $this->check_string( $_static_path, self::DEFAULT_STATIC_PATH );
$this->_style_path = $this->check_string( $_style_path, self::DEFAULT_STYLE_PATH );
$this->_production_domains = is_array( $_production_domains ) ? $_production_domains : [];
$this->_production_file_path = $this->check_string( $_production_file_path, '' );
}

/**
Expand Down Expand Up @@ -111,6 +121,13 @@ public function production_domains() : array {
return $this->_production_domains;
}

/**
* Url path fragment before scripts
*/
public function production_file_path() : string {
return $this->_production_file_path;
}

/**
* Url path fragment before scripts
*/
Expand All @@ -131,11 +148,11 @@ public function static_path() : string {
public function style_path() : string {
return $this->_style_path;
}

/**
* Asset version passed to enqueue calls
*/
public function version() : string {
return $this->_version;
}
}
}
38 changes: 26 additions & 12 deletions src/Registry/WordPress.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class WordPress {

/**
* Build an asset loader instance with the provided configuration
*
*
* @param LoaderConfiguration $_configuration Configuration for the AssetLoader instance
* @param ?string $_production_url (Optional) Production URL base path, defaults to theme directory
* @param ?string $_development_url (Optional) Development URL base path, defaults to KANOPI_DEVELOPMENT_ASSET_URL or empty
Expand All @@ -53,19 +53,33 @@ public function __construct(
?string $_development_url = null
) {
$this->_configuration = $_configuration;
$this->_production_url = !empty( $$_production_url ) ? $_production_url : get_stylesheet_directory_uri();
$this->_development_url = !empty( $_development_url )
$this->_production_url = !empty( $_production_url ) ? $_production_url : get_stylesheet_directory_uri();
$this->_development_url = !empty( $_development_url )
? $_development_url
: ( defined( 'KANOPI_DEVELOPMENT_ASSET_URL' ) ? KANOPI_DEVELOPMENT_ASSET_URL : '' );

// Bugfix to workaround limitations with current loader to ensure the manifest is loaded via file path in production
if ( empty( $this->_configuration->production_file_path() ) ) {
$this->_configuration = new LoaderConfiguration(
$this->_configuration->version(),
$this->_configuration->production_domains(),
$this->_configuration->asset_manifest_path(),
$this->_configuration->handle_prefix(),
$this->_configuration->script_path(),
$this->_configuration->style_path(),
$this->_configuration->static_path(),
get_stylesheet_directory()
);
}

$this->register_loader();
}

/**
* Register an asset loader instance with the following configuration and instance name
* - Multiple registrations are possible, for multiple Kanopi Pack instances, specify different $_instance_names
* - Once registered, you cannot overwrite a named instance, it will return the current registration
*
*
* @param LoaderConfiguration $_configuration Configuration for the AssetLoader instance
* @param string $_instance_name (Optional) Registered instance handle/name
* @param ?string $_production_url (Optional) Production URL base path, defaults to theme directory
Expand All @@ -86,10 +100,10 @@ public static function register(

/**
* Find an instance by registered handle/name
*
*
* @param string $_instance_name (Optional) Registered instance handle/name
*
* @return ?WordPress
*
* @return ?WordPress
*/
public static function instance( string $_instance_name = self::DEFAULT_INSTANCE_NAME ): ?WordPress {
if ( empty( self::$_instances ) || empty( self::$_instances[ $_instance_name ] ) ) {
Expand Down Expand Up @@ -145,7 +159,7 @@ public function asset_loader(): ?AssetLoader {

/**
* Current environment URL path to static assets
*
*
* @param string $_file_path
*
* @return string
Expand All @@ -156,7 +170,7 @@ public function static_asset_url( string $_file_path ): string {

/**
* Register front-end scripts
*
*
* @param callable $_script_registration Callable function passed the current WordPress instance
*/
public function register_frontend_scripts( callable $_script_registration ) {
Expand All @@ -168,14 +182,14 @@ function () use ( $_script_registration ) {

/**
* Register block editor scripts
*
*
* @param callable $_script_registration Callable function passed the current WordPress instance
*/
public function register_block_editor_scripts( callable $_script_registration ) {
add_action( 'enqueue_block_editor_assets',
function () use ( $_script_registration ) {
call_user_func_array( $_script_registration, [ $this ] );
}
}
);
}
}
}

0 comments on commit c9891c3

Please sign in to comment.