• Resolved Alex Kozack

    (@cawa-93)


    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.
    • This topic was modified 7 years, 2 months ago by Alex Kozack.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Optimizing Matters

    (@optimizingmatters)

    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

    (@cawa-93)

    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

    (@optimizingmatters)

    re performance; parsing HTML _can_ be slow-ish, AO uses regexes to do so 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.