Skip to content

Commit 6b1af52

Browse files
authored
Merge pull request #6037 from ampproject/add/rewrite-amp-urls-transformer-from-library
Integrate RewriteAmpUrls transformer from toolbox library
2 parents 81b9df7 + f78bcab commit 6b1af52

File tree

7 files changed

+83
-71
lines changed

7 files changed

+83
-71
lines changed

amp.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@
6464
'DOMXPath',
6565
),
6666
),
67+
'filter' => array(
68+
'functions' => array(
69+
'filter_var',
70+
),
71+
),
6772
// Required by PHP-CSS-Parser.
6873
'iconv' => array(
6974
'functions' => array( 'iconv' ),

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@
99
"ext-curl": "*",
1010
"ext-date": "*",
1111
"ext-dom": "*",
12+
"ext-filter": "*",
1213
"ext-iconv": "*",
1314
"ext-json": "*",
1415
"ext-libxml": "*",
1516
"ext-spl": "*",
16-
"ampproject/amp-toolbox": "^0.3",
17+
"ampproject/amp-toolbox": "dev-main",
1718
"cweagans/composer-patches": "1.7.0",
1819
"fasterimage/fasterimage": "1.5.0",
1920
"sabberworm/php-css-parser": "dev-master#bfdd976"

composer.lock

Lines changed: 51 additions & 33 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: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -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
]

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)