Skip to content

Commit 84a2749

Browse files
committed
Enqueue amp-shadow and amp-wp-app-shell script in non-inner requests
1 parent 8cca74c commit 84a2749

File tree

4 files changed

+39
-31
lines changed

4 files changed

+39
-31
lines changed

.eslintrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
"react"
1717
],
1818
"env": {
19-
"browser": true
19+
"browser": true,
20+
"es6": true
2021
},
2122
"globals": {
2223
"wp": true,

assets/js/amp-app-shell.js renamed to assets/js/amp-wp-app-shell.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* global CONTENT_ELEMENT_ID, Promise */
1+
/* global ampWpAppShell */
22
/* eslint-disable no-console */
33

44
( window.AMP = window.AMP || [] ).push( ( AMP ) => {
@@ -25,7 +25,7 @@
2525

2626
if ( parseInt( currentUrl.searchParams.get( 'amp_shadow_doc_populate' ) ) ) {
2727
currentUrl.searchParams.set( 'amp_app_shell_component', 'inner' );
28-
const container = document.getElementById( CONTENT_ELEMENT_ID );
28+
const container = document.getElementById( ampWpAppShell.contentElementId );
2929

3030
fetchDocument( currentUrl ).then( ( doc ) => {
3131
const shadowDoc = AMP.attachShadowDoc( container, doc, currentUrl );

includes/amp-helper-functions.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,18 @@ function amp_register_default_scripts( $wp_scripts ) {
358358
'async' => true,
359359
) );
360360

361+
// App shell library.
362+
$handle = 'amp-wp-app-shell';
363+
$wp_scripts->add(
364+
$handle,
365+
amp_get_asset_url( 'js/amp-wp-app-shell.js' ),
366+
array( 'amp-shadow' ),
367+
null
368+
);
369+
$wp_scripts->add_data( $handle, 'amp_script_attributes', array(
370+
'async' => true,
371+
) );
372+
361373
// Get all AMP components as defined in the spec.
362374
$extensions = array();
363375
foreach ( AMP_Allowed_Tags_Generated::get_allowed_tag( 'script' ) as $script_spec ) {

includes/class-amp-theme-support.php

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -284,35 +284,30 @@ public static function init_app_shell() {
284284

285285
$requested_app_shell_component = self::get_requested_app_shell_component();
286286

287-
if ( ! is_amp_endpoint() ) {
288-
289-
if ( 'outer' === $requested_app_shell_component ) {
290-
wp_enqueue_script( 'amp-shadow' );
291-
// @todo Enqueue script which hooks uses AMP Shadow API (assets/js/amp-app-shell.js).
292-
// @todo Prevent showing admin bar?
293-
// @todo For non-outer
294-
} elseif ( 'inner' === $requested_app_shell_component ) {
295-
wp_die(
296-
esc_html__( 'Inner app shell can only be requested of the AMP version (thus requires paired mode).', 'amp' ),
297-
esc_html__( 'AMP Inner App Shell Problem', 'amp' ),
298-
array( 'response' => 400 )
299-
);
300-
}
287+
// @todo Prevent showing admin bar in outer app shell?
288+
if ( ! is_amp_endpoint() && 'inner' === $requested_app_shell_component ) {
289+
// @todo For non-outer
290+
wp_die(
291+
esc_html__( 'Inner app shell can only be requested of the AMP version (thus requires paired mode).', 'amp' ),
292+
esc_html__( 'AMP Inner App Shell Problem', 'amp' ),
293+
array( 'response' => 400 )
294+
);
295+
} elseif ( is_amp_endpoint() && 'outer' === $requested_app_shell_component ) {
296+
wp_die(
297+
esc_html__( 'Outer app shell can only be requested of the non-AMP version (thus requires paired mode).', 'amp' ),
298+
esc_html__( 'AMP Outer App Shell Problem', 'amp' ),
299+
array( 'response' => 400 )
300+
);
301+
}
301302

302-
// @todo Is this right? It should really be enqueued regardless. It's not about whether the current template has AMP available, but _other_ URLs.
303-
$template_availability = self::get_template_availability();
304-
if ( ! empty( $template_availability['supported'] ) ) {
305-
wp_enqueue_script( 'amp-shadow' );
306-
// @todo Enqueue other required scripts.
307-
}
308-
} else {
309-
if ( 'outer' === $requested_app_shell_component ) {
310-
wp_die(
311-
esc_html__( 'Outer app shell can only be requested of the non-AMP version (thus requires paired mode).', 'amp' ),
312-
esc_html__( 'AMP Outer App Shell Problem', 'amp' ),
313-
array( 'response' => 400 )
314-
);
315-
}
303+
// Enqueue scripts for (outer) app shell, including precached app shell and normal site navigation prior to service worker installation.
304+
if ( 'inner' !== $requested_app_shell_component ) {
305+
wp_enqueue_script( 'amp-shadow' );
306+
wp_enqueue_script( 'amp-wp-app-shell' );
307+
$exports = array(
308+
'contentElementId' => self::APP_SHELL_CONTENT_ELEMENT_ID,
309+
);
310+
wp_add_inline_script( 'amp-wp-app-shell', sprintf( 'var ampWpAppShell = %s;', wp_json_encode( $exports ) ), 'before' );
316311
}
317312
}
318313

0 commit comments

Comments
 (0)