Title: How do I get the JavaScript optimized file name?
Last modified: April 12, 2019

---

# How do I get the JavaScript optimized file name?

 *  Resolved [Alex Kozack](https://wordpress.org/support/users/cawa-93/)
 * (@cawa-93)
 * [7 years, 2 months ago](https://wordpress.org/support/topic/how-do-i-get-the-javascript-optimized-file-name/)
 * I am creating a plugin that adds link[rel=preload] for scripts.
 * His behavior is very simple:
    It reads a `global $wp_scripts` variable, defines
   all enqueued files and displays the link[rel=preload] of each of them.
 * I want to implement compatibility with your plugin. To do this, I need to somehow
   determine which files from the attached your plugin replaced, and the URL to 
   the optimized file to add link[rel=preload] for them.
    -  This topic was modified 7 years, 2 months ago by [Alex Kozack](https://wordpress.org/support/users/cawa-93/).
    -  This topic was modified 7 years, 2 months ago by [Alex Kozack](https://wordpress.org/support/users/cawa-93/).

Viewing 3 replies - 1 through 3 (of 3 total)

 *  Plugin Author [Optimizing Matters](https://wordpress.org/support/users/optimizingmatters/)
 * (@optimizingmatters)
 * [7 years, 2 months ago](https://wordpress.org/support/topic/how-do-i-get-the-javascript-optimized-file-name/#post-11421111)
 * Noon Alex;
    Getting a list of replace plugins is not possible I’m afraid, but
   getting the autoptimized files can be done with the `autoptimize_filter_cache_getname`
   filter 🙂
 * hope this helps,
    frank
 *  Thread Starter [Alex Kozack](https://wordpress.org/support/users/cawa-93/)
 * (@cawa-93)
 * [7 years, 2 months ago](https://wordpress.org/support/topic/how-do-i-get-the-javascript-optimized-file-name/#post-11421356)
 * Sadly. This is not a good fit for me, since there are files that are no longer
   needed. The browser will still load them.
 * I think that I could use the method, from the example below. However, I’m not
   very sure how this will affect the performance of the web server.
 *     ```
       add_filter( 'autoptimize_html_after_minify', function ( $content ) {
       	$doc = new DOMDocument();
       	$doc->loadHTML( $content );
   
       	$str = '';
   
       	foreach ( $doc->getElementsByTagName( 'script' ) as $script ) {
       		$src = $script->getAttribute( 'src' );
       		if ( ! empty( $src ) ) {
       			$str .= "<link rel='preload' as='script' href='{$script->getAttribute( 'src' )}'>\n";
       		}
       	}
   
       	return str_replace( '<!--preload_scripts_placement-->', $str, $content );
       } )
       ```
   
 *  Plugin Author [Optimizing Matters](https://wordpress.org/support/users/optimizingmatters/)
 * (@optimizingmatters)
 * [7 years, 2 months ago](https://wordpress.org/support/topic/how-do-i-get-the-javascript-optimized-file-name/#post-11421598)
 * re performance; parsing HTML _can_ be slow-ish, [AO uses regexes to do so](https://github.com/futtta/autoptimize/blob/master/classes/autoptimizeScripts.php#L147)
   and has become pretty reliable in doing so, so maybe use something like that 
   instead?
 * but given the fact that AO is supposed to aggregate as much JS as possible, one
   _could_ preload just the AO’ed JS & jquery (which is excluded by default) and
   be done with it in which case you could use the filter?

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘How do I get the JavaScript optimized file name?’ is closed to new replies.

 * ![](https://ps.w.org/autoptimize/assets/icon-256X256.png?rev=2211608)
 * [Autoptimize](https://wordpress.org/plugins/autoptimize/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/autoptimize/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/autoptimize/)
 * [Active Topics](https://wordpress.org/support/plugin/autoptimize/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/autoptimize/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/autoptimize/reviews/)

## Tags

 * [api](https://wordpress.org/support/topic-tag/api/)
 * [preload](https://wordpress.org/support/topic-tag/preload/)

 * 3 replies
 * 2 participants
 * Last reply from: [Optimizing Matters](https://wordpress.org/support/users/optimizingmatters/)
 * Last activity: [7 years, 2 months ago](https://wordpress.org/support/topic/how-do-i-get-the-javascript-optimized-file-name/#post-11421598)
 * Status: resolved