Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Building AMP-first Sites, the WordPress Way

Building AMP-first Sites, the WordPress Way

Avatar for Salvatore Denaro

Salvatore Denaro

September 15, 2019
Tweet

Other Decks in Programming

Transcript

  1. The people who make the web work. The people who

    make the web look good. The people who make the web profitable. Who is this talk for?
  2. • I’ve been writing code for 20+ years • I’ve

    been tech leader for 10 years • I make excellent espresso
  3. Our mission: Organize the world’s information and make it universally

    accessible and useful. https://www.google.com/search/howsearchworks/mission/
  4. AMP HTML All AMP pages are HTML pages but not

    all HTML pages are AMP pages. AMP • No special plugins • Uses existing browser
  5. AMP HTML • Large library of components for static and

    dynamic web sites • Framework providing lists, templating, images, media embeds • Rules that enforce the best practices needed to keep things fast.
  6. AMP HTML • Static Layout, dynamic content • 50k css-inlined

    in the page header • You must specify element sizes • Strict rules on the DOM.
  7. AMP HTML If you are using iframes for reuse you

    are already using web components. Iframes tend to be ad hoc components. They tend to be superset of all reuse cases.
  8. AMP Cache • Not just a CDN • AMP Cache

    is a specification, others can (and have) built their own AMP Cache
  9. Pre-Rendering If the HTML page is the recipe and the

    page elements are the ingredients, pre-rendering is the difference between sending pizza dough...
  10. Serving AMP from WordPress before plugin • Handwritten custom themes

    • M-dot sites and AMP-dot sites • This was difficult • We have an alternative
  11. 40

  12. CSS Tree Shaking This is possible because AMP pages will

    have known components and known component sizes. No new styles will be added, we can know what styles will be on the page in advance.
  13. AMP Validation • Validation in the browser • In the

    WP Dashboard • In bulk from the command line • From the search console • All using the same AMP Validation
  14. AMP Stories • Create beautiful and engaging content • Sharable

    and linkable on the open web • Fast loading times • Immersive
  15. AMP Stories • Stories are surfaced via Search, News and

    Images • Best used “To Tell a Story” • Call to action
  16. What if your theme isn’t working? add_theme_support( 'amp', array( 'template_dir'

    => 'amp-templates', ) ); • You can fork your theme and fix the AMP issues then merge those changes. • Develop your AMP theme as a child theme.
  17. Save this snippet <?php if ( function_exists( 'is_amp_endpoint' ) &&

    is_amp_endpoint() ) : ?> <?php else : ?> <?php endif; ?>
  18. When you need AMP and HTML to differ <?php if

    ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() ) : ?> <amp-social-share type="twitter"></amp-social-share> <?php else : ?> <a href="https://twitter.com/intent/tweet/?url=http%3A%2F%2Fexample.com">Share</a> <?php endif; ?>
  19. You need to add to the AMP header or footer

    add_action( 'amp_post_template_footer', 'xyz_amp_add_pixel' ); function xyz_amp_add_pixel( $amp_template ) { $post_id = $amp_template->get( 'post_id' ); ?> <amp-pixel src="https://example.com/hi.gif?x=RANDOM"></amp-pixel> <?php }
  20. Working with a JS Nav <!-- 1. Define the state

    --> <amp-state id="navMenuExpanded"> <script type="application/json">false</script> </amp-state>
  21. Working with a JS Nav <!-- 2. Mutate the state

    --> <button class="menu-toggle" on="tap:AMP.setState( { navMenuExpanded: ! navMenuExpanded } )" [class]="'menu-toggle' + ( navMenuExpanded ? ' toggled-on' : '' )" aria-expanded="false" [aria-expanded]="navMenuExpanded ? 'true' : 'false'" > <?php _e( 'Menu', 'example' ); ?> </button>
  22. Working with a JS Nav <!-- 3. React to state

    changes --> <nav class="site-header-menu" [class]="'site-header-menu' + ( navMenuExpanded ? ' toggled-on' : '' )" aria-expanded="false" [aria-expanded]="navMenuExpanded ? 'true' : 'false'" > <?php wp_nav_menu( /* ... */ ); ?> </nav>
  23. Disable a script function example_javascript_detection() { if (function_exists( 'is_amp_endpoint' )

    && is_amp_endpoint()) { return; // or AMP replacement. } wp_enqueue_scripts('example_javascripts.js'); } }
  24. • AMP can be embedded anywhere the them allows you

    to embed HTML • AMP Components will render on AMP and Non-AMP pages • You’ll need to import the component in a custom header • You might need to “fix” minor issues • This is still experimental • Please go to https://amp.dev • Please file bug reports on GitHub
  25. Widgets Theme features such as menu, widgets, HTML snippets etc,

    can be rendered as AMP on the fly. Be aware that Widgets or HTML snippets that break the AMP rules will prevent the page from rendering as AMP
  26. What is WP Rig It’s a WordPress starter Theme that

    is: • Progressive • Mobile First • AMP-Ready You can compare it to underscore
  27. WP Rig : AMP Ready • In WordPress, open up

    AMP Menu • You’ll see your Theme has built-in AMP support
  28. WP Rig : PWA Support • Service workers ◦ PWA

    plugin adds workbox ◦ And an API to extend the service work ◦ PHP extensions that compile to the WB API
  29. WP Rig : PWA Support • Service Worker created by

    PHP on on the fly ◦ Routing, Caching, Offline comments ◦ enqueue javascript to run in the service worker • Add features to your theme that run locally
  30. WP Rig • Compare to underscore • Progressive Web AMP

    • Good alternative to headless CMS + React
  31. Q&A