13. PHP 7
Magento2 is built to support PHP 7, so we strongly recommend the use of the PHP version 7.1.x. There were some small issues with Magento 2.1 and PHP 7.1, but they were fixed in the 2.2 version.
14. cacheable=”false”
is both a blessing and a curse. This flag will prevent your block from caching. This is great for development since you don’t have to flush your cache every time you make a change to your template. For example, cacheable=”false” will allow you to make changes to foobar.phtml and have them immediately show on frontend after reloading. The catch is that whenever a block appears that has cacheable=”false”, the entire page is not cached in the Full Page Cache. Always search your codebase for these and remove them before deploying your code as this will slow down your site significantly. However, make sure that you leave certain pages uncacheable (like compare products, cart, checkout, etc.). More details can be found here.
15. Custom cache type
When developing some custom functionalities for our clients we try to utilize Magento features as much as possible.
One of the key things in developing custom frontend functionalities is to use Magento custom cache types. You can read a more detailed tutorial from Urban here.
16. Product page appears slow because it takes forever to load the image (gallery)
We can improve the speed of the product page or rather make it seem much faster by displaying the base image to the user immediately. Normally, Magento will load the gallery library first and build it and only then display it.
My solution displays the base image as a background to the loader element and then hides it and displays the gallery. Please note that:
– there is a slight delay between the loader and the gallery (roughly .5s).
– this might only work on Magento/luma theme
Steps:
1. Copy vendor/magento/module-catalog/view/frontend/template/product/view/gallery.phtml
to your theme
2. Add the following code somewhere at the top. This will get the base image url.
$images = $block->getGalleryImagesJson();
$imagesData = json_decode($images,true);
$mainImage = '';
if(count($imagesData) > 0) {
foreach($imagesData as $key=>$value){
if(isset($value['isMain']) && $value['isMain'] == 1){
$mainImage = $value['img'];
break;
}
}
}
3. Add this style to the .loading-mask
element (inside div.gallery-placeholder
)
style="background-image: url(/* @noEscape */ $mainImage)"
4. Add this styling to any of your scss
files
.gallery-placeholder .loading-mask {
background: 75% center /contain no-repeat;
@media (max-width: 767px) {
margin-bottom : 75px;
background-position: center center;
}
}
5. Adjust the positioning to your case using background-position and margins. This worked on my project but might not align so well on yours.
Cronjobs
One interesting issue we encountered with Magento 2 production environment is the cron_schedule table growth. Tilen has written an article about this topic and you can find it here.
CDN
Using a content delivery network (CDN) will cache static assets (such as images, CSS and Javascript) and serve the files to users from a node that is geographically closest to them. There are a number of CDN providers, some of the main players being Cloudflare, Amazon AWS CloudFront, BunnyCDN and Fastly.
Our friends at Inchoo wrote a great blog post about the implementation of Fastly with Magento2: http://inchoo.net/magento-2
PWA
Nowadays, one of the buzzwords that everybody is talking about is PWA (Progressive Web Apps). PWA is a web application that makes use of the latest web technologies with the intention of making a web application act and feel like an app. Optiweb has partnered up with Divante on the awesome project that brings PWA to Magento. This technology can enable a store to be rendered within milliseconds and it is immune to traffic overloads. You can read more about it here: http://www.vuestorefront.io/
Watch the Video Demo here.
Every developer sooner or later faces questions such as: “Why is this page taking so long to load?” or “Why is this operation taking such a long time?”. When you face a question like this, it is important to figure out the exact moment when it’s slowing down. Is it every time the page loads or when something specific occurs – like when a user is logged in, when you add 3 or more products to cart, etc. When you figure that out, you are only left with profiling.
That’s where Blackfire comes in. Install Blackfire on your dev environment. Next to that, install also the chrome plugin so you can trigger profiling of the page. Don’t forget to put Magento into production mode and optimise the composer autoload file or you can get significantly different results. Open the page that runs slow, click on Blackfire and voila – you get the performance profile of the page. Open the timeline view and check what is taking so long.