layout: true
M2 Private Data via JS Components
--- class: center, middle, world, bgimage # Magento 2 Private Data ## via JS Components Meet Magento Netherlands 2017 --- class: center, middle ### This slide is made possible with RemarkJS ??? Reminder: Make a silly joke on that this slide is using RemarkJS and refer to these student notes as an example. --- class: center, middle ### This slide is also made possible with RemarkJS --- class: orange # About me - Jisse Reitsma -- - Founder and lead developer of Yireo -- - Trainer, enterpreneur, coder -- - Magento 2 Master "Mover" (2017) -- - Knockout JiSse (I don't why) -- - Loving Magento 2 --- # My talk - Private Content - Hole punching in FPC (M2) -- - JS Components - RequireJS - KnockoutJS --- # Full Page Cache -- - Shipped out of the box with M2 -- - For both CE and EE -- - Native Magento cache or Varnish Cache --- # Optimizations -- - Make sure no blocks are using `cacheable=false` -- - Enable FPC with Magento caching - 50-100 ms -- - Set caching handler to Redis (or memcache) - 10-50 ms -- - Switch to Varnish - 5-10 ms --- # cacheable=false ```xml
false
``` -- If you do this, you are a bastard --- class: orange, center, middle # `cacheable=false` # is in top 5 issues for Magento Marketplace extensions --- # Checking for FPC - Enable FPC - Open up Error Console and check for HTTP headers - FPC is working - `X-Magento-Cache-Control: MISS` - `X-Magento-Cache-Control: HIT` - FPC is not working - `X-Magento-Cache-Control: MISS` - `X-Magento-Cache-Control: MISS` --- # Reasons `cacheable=false` - Bad third party modules - Module `Magento_Captcha` is enabled - You are in Catalog Search result pages - You are comparing products - You are in the checkout or in the cart - You are in customer pages --- # Hole punching with M2 - No ESI Includes, no VCL tricks - Just plain JavaScript lazy loading --- # Private Content - Print dummy HTML in PHTML template - Load JS component in RequireJS - Extend from `UiComponent` - Load data from `customerData` component - Let Knockout replace data in PHTML template --- # Module files - `etc/module.xml` + `registration.php` - `view/frontend/layout/foobar.xml` - `Block/Foobar.php` - `view/frontend/template/foobar.phtml` - `view/frontend/requirejs-config.js` - `view/frontend/web/js/foobarComp.js` - `etc/di.xml` - `CustomerData/PersonalizedFoobar.php` --- class: orange, center, middle ### Disclaimer: This code is not 100% halal --- # Private content File `Block/Foobar.php` ```php namespace Yireo\Example\Block; class Foobar extends \Magento\Framework\View\Element\Template { protected $_template = 'foobar.phtml'; } ``` --- # Private content File `view/frontend/layout/foobar.xml` ```xml
foobarComp
``` --- # Private content File `view/frontend/template/foobar.phtml` ```html
0
``` ```php ``` --- # Private content File `view/frontend/requirejs-config.js`: ```js var config = { path: { '*': { foobarComp: 'Yireo_Example/js/foobarComp', } } }; ``` --- # Private content File `view/frontend/web/js/foobarComp.js` ```js define([ 'uiComponent', 'Magento_Customer/js/customer-data' ], function (Component, customerData) { ... return Component.extend({ initialize: function () { this._super(); this.fooBarComp = customerData.get('fooBarData'); } }); }); ``` --- # Private content File `etc/di.xml` ```xml
Yireo\Example\CustomerData\PersonalizedFoobar
``` --- # Private content File `CustomerData/PersonalizedFoobar.php` ```php namespace Yireo\Example\CustomerData; use Magento\Customer\CustomerData\SectionSourceInterface; class PersonalizedFoobar implements SectionSourceInterface { public function getSectionData() { return ['secret_number' => '42']; } } ``` --- class: orange, center, middle # ... ---
So, we have defined a module with a simple module.xml and a registration.php file, to call upon a regular flow of XML layout, to define a Block class that is outputted into a PHTML template, which is then outputting a JSON configuration array, that was inserted into the Block class, using an XML argument-array, and which is then used to initialize a custom AMD-style JS component, initialized through another component Magento_Ui/js/core/app which connects our JS component to our KnockoutJS component. Our JS component then injects the customer-data component, to collect a thing called foobarData, which is fetched through a kind of localStorage-cached AJAX-call to the backend which is configured through a DI type that injects an array of section sources into a backend class, that fetches data from various sources, including our own, to then send back a bundled response, if possible, to the JS component, with the major feature being JavaScript AJAX lazy loading.
--- class: orange, center, middle # WTF --- class: orange, center, middle # (W)onderful. (T)echnology. (F)un. --- # Tips - Learn about RequireJS & KnockoutJS - Take your time to read through devdocs - Or learn from a training - Do not use extensions with `cacheable=false` --- class: orange, center, middle # WTF --- class: orange, center, middle # MTF --- class: orange, center, middle
Mage
Test
Fest
---
Mage Test Fest
-- - Magento. Software Testing. Party. -- - November 15th-18th 2017 -- - Somewhere in The Netherlands -- - 5 awesome speakers -- -
1st announcement
Vinai Kopp
-- - [magetestfest.nl](https://magetestfest.nl/) --- class: center, middle, world # Any questions? ### @yireo ### @jissereitsma