Skip to content

Commit 8b8cf85

Browse files
committed
Integrate RewriteAmpUrls transformer
1 parent 2d0de18 commit 8b8cf85

File tree

4 files changed

+32
-29
lines changed

4 files changed

+32
-29
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"ext-json": "*",
1414
"ext-libxml": "*",
1515
"ext-spl": "*",
16-
"ampproject/amp-toolbox": "^0.3",
16+
"ampproject/amp-toolbox": "dev-add/20-rewrite-amp-urls-transformer",
1717
"cweagans/composer-patches": "1.7.0",
1818
"fasterimage/fasterimage": "1.5.0",
1919
"sabberworm/php-css-parser": "dev-master#bfdd976"

composer.lock

Lines changed: 7 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

includes/class-amp-theme-support.php

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -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
)

src/ConfigurationArgument.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
interface ConfigurationArgument {
1818

19+
const ENABLE_ESM = 'enable_esm';
1920
const ENABLE_OPTIMIZER = 'enable_optimizer';
2021
const ENABLE_SSR = 'enable_ssr';
2122
}

0 commit comments

Comments
 (0)