@@ -1388,7 +1388,7 @@ public static function filter_admin_bar_script_loader_tag( $tag, $handle ) {
13881388 */
13891389 public static function ensure_required_markup ( Document $ dom , $ script_handles = [] ) {
13901390 // Gather all links.
1391- $ links = [
1391+ $ links = [
13921392 Attribute::REL_PRECONNECT => [
13931393 // Include preconnect link for AMP CDN for browsers that don't support preload.
13941394 AMP_DOM_Utils::create_node (
@@ -1401,7 +1401,9 @@ public static function ensure_required_markup( Document $dom, $script_handles =
14011401 ),
14021402 ],
14031403 ];
1404+
14041405 $ link_elements = $ dom ->head ->getElementsByTagName ( Tag::LINK );
1406+
14051407 /**
14061408 * Link element.
14071409 *
@@ -1500,32 +1502,16 @@ public static function ensure_required_markup( Document $dom, $script_handles =
15001502 }
15011503 }
15021504
1503- /* phpcs:ignore Squiz.PHP.CommentedOutCode.Found
1504- *
1505- * "2. Next, preload the AMP runtime v0.js <script> tag with <link as=script href=https://cdn.ampproject.org/v0.js rel=preload>.
1506- * The AMP runtime should start downloading as soon as possible because the AMP boilerplate hides the document via body { visibility:hidden }
1507- * until the AMP runtime has loaded. Preloading the AMP runtime tells the browser to download the script with a higher priority."
1508- * {@link https://amp.dev/documentation/guides-and-tutorials/optimize-and-measure/optimize_amp/ Optimize the AMP Runtime loading}
1505+ /*
1506+ * "3. If your page includes render-delaying extensions (e.g., amp-experiment, amp-dynamic-css-classes, amp-story),
1507+ * preload those extensions as they're required by the AMP runtime for rendering the page."
1508+ * @TODO: Move into RewriteAmpUrls transformer, as that will support self-hosting as well.
15091509 */
15101510 $ prioritized_preloads = [];
15111511 if ( ! isset ( $ links [ Attribute::REL_PRELOAD ] ) ) {
15121512 $ links [ Attribute::REL_PRELOAD ] = [];
15131513 }
15141514
1515- $ prioritized_preloads [] = AMP_DOM_Utils::create_node (
1516- $ dom ,
1517- Tag::LINK ,
1518- [
1519- Attribute::REL => Attribute::REL_PRELOAD ,
1520- Attribute::AS_ => RequestDestination::SCRIPT ,
1521- Attribute::HREF => $ runtime_src ,
1522- ]
1523- );
1524-
1525- /*
1526- * "3. If your page includes render-delaying extensions (e.g., amp-experiment, amp-dynamic-css-classes, amp-story),
1527- * preload those extensions as they're required by the AMP runtime for rendering the page."
1528- */
15291515 $ amp_script_handles = array_keys ( $ amp_scripts );
15301516 foreach ( array_intersect ( Amp::RENDER_DELAYING_EXTENSIONS , $ amp_script_handles ) as $ script_handle ) {
15311517 if ( ! in_array ( $ script_handle , Amp::RENDER_DELAYING_EXTENSIONS , true ) ) {
@@ -1991,6 +1977,8 @@ public static function prepare_response( $response, $args = [] ) {
19911977 }
19921978 }
19931979
1980+ self ::ensure_required_markup ( $ dom , array_keys ( $ amp_scripts ) );
1981+
19941982 $ enable_optimizer = array_key_exists ( ConfigurationArgument::ENABLE_OPTIMIZER , $ args )
19951983 ? $ args [ ConfigurationArgument::ENABLE_OPTIMIZER ]
19961984 : true ;
@@ -2049,8 +2037,6 @@ static function( Optimizer\Error $error ) {
20492037 do_action ( 'amp_server_timing_stop ' , 'amp_optimizer ' );
20502038 }
20512039
2052- self ::ensure_required_markup ( $ dom , array_keys ( $ amp_scripts ) );
2053-
20542040 $ can_serve = AMP_Validation_Manager::finalize_validation ( $ dom );
20552041
20562042 // Redirect to the non-AMP version if not on an AMP-first site.
@@ -2130,6 +2116,7 @@ private static function get_optimizer_configuration( $args ) {
21302116 [
21312117 Optimizer \Transformer \AmpRuntimeCss::class,
21322118 Optimizer \Transformer \PreloadHeroImage::class,
2119+ Optimizer \Transformer \RewriteAmpUrls::class,
21332120 Optimizer \Transformer \ServerSideRendering::class,
21342121 Optimizer \Transformer \TransformedIdentifier::class,
21352122 ]
0 commit comments