Skip to content

Do not fatal when trying to enqueue asset from missing manifest location #57

Closed
@kadamwhite

Description

@kadamwhite

This plugin will currently throw a hard error when we try to load the page if we try to enqueue an asset from a manifest that doesn't exist. We should adjust this behavior so that it logs a warning, but does not actually stop loading the page, because having it completely error prevents quick bisecting between branches that need builds (among other things).

The error we see sometimes:

 Fatal error: Uncaught Error: Asset_Loader\enqueue_asset(): Argument #1 ($manifest_path) must be of type string, null given, called in /wp/wp-content/themes/themename/functions.php on line 139
in /wp/wp-content/plugins/asset-loader/inc/namespace.php on line 170

This is a consequence of themes using this pattern:

/**
 * Check through the available manifests to find the first which includes the
 * target asset. This allows some assets to be loaded from a running DevServer
 * while others load from production files on disk.
 *
 * @param string $target_asset Desired asset within the manifest.
 * @return string|null
 */
function myproject_custom_get_manifest_path( $target_asset ) {
	$manifests = [
		get_template_directory() . '/assets/dist/development-asset-manifest.json',
		get_template_directory() . '/assets/dist/production-asset-manifest.json',
	];
	foreach ( $manifests as $manifest_path ) {
		$asset_uri = \Asset_Loader\Manifest\get_manifest_resource( $manifest_path, $target_asset );
		if ( ! empty( $asset_uri ) ) {
			return $manifest_path;
		}
	}
}


function mytheme_enqueue_scripts() {
	Asset_Loader\enqueue_asset(
		myproject_custom_get_manifest_path( 'my-script-bundle.js' ),
		'my-script-bundle.js',
		[
			'dependencies' => [],
			'handle' => 'my-script-bundle',
		]
	);
}

This code should be improved -- The function which looks up a resource in a series of manifests should not return null if no matching asset is found. But that theme code quality concern still shouldn't cause a hard error because of this plugin.

Proposed change

Within Asset Loader, we should alter the enqueue_asset and register_asset functions to allow them to receive a ?string instead of a string, and add an if ( empty( $manifest_path ) ) { at the start of register_asset so that it will detect missing manifests before we try to register the asset. If the manifest is missing, we should log an informational error to the logs, and return an empty array from register_asset(). (Returning an empty array from that method will cause enqueue_asset to take no further action.)

Steps to reproduce

  • In a vip dev-env environment that is using a theme using this library, (contact me for an example, if HM-internal),
  • ensure the built files are not yet present (e.g. delete build directory or check out non-release branch)
  • try to load a page on the site
  • observe that the error shown above is thrown after no manifest can be found

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions