@@ -1401,6 +1401,7 @@ public static function ensure_required_markup( Document $dom, $script_handles =
14011401 ),
14021402 ],
14031403 ];
1404+
14041405 $ link_elements = $ dom ->head ->getElementsByTagName ( Tag::LINK );
14051406 /**
14061407 * Link element.
@@ -1500,32 +1501,16 @@ public static function ensure_required_markup( Document $dom, $script_handles =
15001501 }
15011502 }
15021503
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}
1504+ /*
1505+ * "3. If your page includes render-delaying extensions (e.g., amp-experiment, amp-dynamic-css-classes, amp-story),
1506+ * preload those extensions as they're required by the AMP runtime for rendering the page."
1507+ * @TODO: Move into RewriteAmpUrls transformer, as that will support self-hosting as well.
15091508 */
15101509 $ prioritized_preloads = [];
15111510 if ( ! isset ( $ links [ Attribute::REL_PRELOAD ] ) ) {
15121511 $ links [ Attribute::REL_PRELOAD ] = [];
15131512 }
15141513
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- */
15291514 $ amp_script_handles = array_keys ( $ amp_scripts );
15301515 foreach ( array_intersect ( Amp::RENDER_DELAYING_EXTENSIONS , $ amp_script_handles ) as $ script_handle ) {
15311516 if ( ! in_array ( $ script_handle , Amp::RENDER_DELAYING_EXTENSIONS , true ) ) {
@@ -1975,6 +1960,8 @@ public static function prepare_response( $response, $args = [] ) {
19751960 }
19761961 }
19771962
1963+ self ::ensure_required_markup ( $ dom , array_keys ( $ amp_scripts ) );
1964+
19781965 $ enable_optimizer = array_key_exists ( ConfigurationArgument::ENABLE_OPTIMIZER , $ args )
19791966 ? $ args [ ConfigurationArgument::ENABLE_OPTIMIZER ]
19801967 : true ;
@@ -2033,8 +2020,6 @@ static function( Optimizer\Error $error ) {
20332020 do_action ( 'amp_server_timing_stop ' , 'amp_optimizer ' );
20342021 }
20352022
2036- self ::ensure_required_markup ( $ dom , array_keys ( $ amp_scripts ) );
2037-
20382023 $ can_serve = AMP_Validation_Manager::finalize_validation ( $ dom );
20392024
20402025 // Redirect to the non-AMP version if not on an AMP-first site.
@@ -2094,6 +2079,10 @@ private static function get_optimizer( $args ) {
20942079 private static function get_optimizer_configuration ( $ args ) {
20952080 $ transformers = Optimizer \Configuration::DEFAULT_TRANSFORMERS ;
20962081
2082+ $ enable_esm = array_key_exists ( ConfigurationArgument::ENABLE_ESM , $ args )
2083+ ? $ args [ ConfigurationArgument::ENABLE_ESM ]
2084+ : true ;
2085+
20972086 $ enable_ssr = array_key_exists ( ConfigurationArgument::ENABLE_SSR , $ args )
20982087 ? $ args [ ConfigurationArgument::ENABLE_SSR ]
20992088 : true ;
@@ -2107,6 +2096,15 @@ private static function get_optimizer_configuration( $args ) {
21072096 */
21082097 $ enable_ssr = apply_filters ( 'amp_enable_ssr ' , $ enable_ssr );
21092098
2099+ /**
2100+ * Filter whether the AMP Optimizer should use ES modules for the runtime and extensions.
2101+ *
2102+ * @since 2.1.0
2103+ *
2104+ * @param bool $enable_esm Whether the AMP Optimizer should use ES modules for the runtime and extensions.
2105+ */
2106+ $ enable_esm = apply_filters ( 'amp_enable_esm ' , $ enable_esm );
2107+
21102108 // In debugging mode, we don't use server-side rendering, as it further obfuscates the HTML markup.
21112109 if ( ! $ enable_ssr ) {
21122110 $ transformers = array_diff (
@@ -2137,6 +2135,9 @@ private static function get_optimizer_configuration( $args ) {
21372135 Optimizer \Transformer \PreloadHeroImage::class => [
21382136 Optimizer \Configuration \PreloadHeroImageConfiguration::INLINE_STYLE_BACKUP_ATTRIBUTE => 'data-amp-original-style ' ,
21392137 ],
2138+ Optimizer \Transformer \RewriteAmpUrls::class => [
2139+ Optimizer \Configuration \RewriteAmpUrlsConfiguration::ESM_MODULES_ENABLED => $ enable_esm ,
2140+ ],
21402141 ],
21412142 $ args
21422143 )
0 commit comments