Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SPA with amp shadow DOM extras #4055

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions assets/js/amp-wp-app-shell.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@
*/
function isHeaderVisible() {
const element = document.querySelector( '.site-branding' );
if ( ! element ) {
return;
}
const clientRect = element.getBoundingClientRect();
return clientRect.height + clientRect.top >= 0;
}
Expand All @@ -124,7 +127,8 @@
*/
function fetchShadowDocResponse( url ) {
const ampUrl = new URL( url );
ampUrl.searchParams.set( ampAppShell.componentQueryVar, 'inner' );
const pathSuffix = '_' + ampAppShell.componentQueryVar + '_inner';
ampUrl.pathname = ampUrl.pathname + pathSuffix;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this not require the addition of new rewrite rules?


return fetch( ampUrl.toString(), {
method: 'GET',
Expand Down Expand Up @@ -236,8 +240,14 @@
}

if ( scrollIntoView && ! isHeaderVisible() ) {
const siteContent = document.querySelector( '.site-content-contain' );

if ( ! siteContent ) {
return;
}

// @todo The scroll position is not correct when admin bar is used. Consider scrolling to Y coordinate smoothly instead.
document.querySelector( '.site-content-contain' ).scrollIntoView( {
siteContent.scrollIntoView( {
block: 'start',
inline: 'start',
behavior: 'smooth'
Expand Down
2 changes: 1 addition & 1 deletion includes/amp-helper-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ function amp_register_default_scripts( $wp_scripts ) {
$handle,
sprintf(
'if ( ! Element.prototype.attachShadow ) { const script = document.createElement( "script" ); script.src = %s; script.async = true; document.head.appendChild( script ); }',
wp_json_encode( 'https://cdnjs.cloudflare.com/ajax/libs/webcomponentsjs/1.3.3/webcomponents-sd-ce.js' )
wp_json_encode( 'https://cdnjs.cloudflare.com/ajax/libs/webcomponentsjs/2.4.1/webcomponents-bundle.js' )
),
'after'
);
Expand Down