Improving application performance
When creating a top-performing site, performance optimizations are crucial. Below are some ideas and tools so you can improve the performance of your projects. While we try to implement as much as possible in the framework itself, there are some decisions only you can make.
Deferred components
You can reduce the overall build size of your JavaScript as some Frontastic components can be deferred since they're not relevant to search engines.
To defer the loading of a Frontastic component you can use our asyncComponent
helper inside the tastics.js
file.
To do this, find the usual import of a Frontastic component which looks like this:
import CheckoutTastic from './checkout/tastic.jsx'
And replace with the following lines:
import asyncComponent from 'frontastic-catwalk/src/js/component/asyncComponent'
const CheckoutTastic = asyncComponent({
import: () => {
return import('./checkout/tastic.jsx')
},
height: { desktop: 690, tablet: 530, mobile: 542 },
})
Everything will stay the same. The height will be used to reserve some space for the Frontastic component during asynchronous rendering to reduce the screen flickering. Frontastic components wrapped into asyncComponent
will be put into their own chunks by the default webpack configuration.
To analyze the build size and find out which Frontastic components have the most effect on the build size you can analyze the build size yourself:
- In a shell, go into the project folder you want to analyze
- Run
ant package
- Inspect
build/bundleSize.html
(HTML report) orbuild/bundleStats.json
(webpack bundle statistics)
With this, it should be possible for you to optimize imports, libraries, and decide which Frontastic components should be deferred.
Stream data optimizations
Data fetched from backend systems as configured in the Frontastic studio are crucial to Frontastic as we don't store any data ourselves but load all data from third-party systems. We already implement some optimizations ourselves:
- Only load data sources assigned to Frontastic components
- Load all <glossary:data sources>> in parallel
- Optionally cache data sources
- Limit fetched item count
Still, the data fetched has to be transmitted to the Server Side Rendering and to the Client, so it can make sense to optimize the data. This can already be done using an API decorator but we have an additional concept that knows more about the context of the data source, the StreamOptimizer
.
Stream optimizer
A stream optimizer will receive the data of all data sources and some context information:
- The current page folder and page version
- The current context (customer, project, session, …)
- The Frontastic components currently using this data source
- The data source itself and its parameters
Based on this information you can programmatically decide if you want to remove data from the data source.
It's very common, for example, that everything which refers to product lists needs a lot less data about a product than a Product Detail Page needs. While Frontastic by default also provides all product lists with all the product data, we can use a stream optimizer to limit the transmitted data. For this we must do 2 things:
- Create the optimizer
- Register the optimizer
1. Create the optimizer
The optimizer is a PHP class that will receive all data from all data sources and can return updated data based on this. In this case, we remove a lot of information from the products in a product-list
stream because those aren't necessary for any of the product lists in our project:
namespace My\StreamOptimizer;
use Frontastic\Catwalk\FrontendBundle\Domain\StreamOptimizer;
use Frontastic\Catwalk\FrontendBundle\Domain\StreamContext;
use Frontastic\Catwalk\FrontendBundle\Domain\Stream;
use Frontastic\Common\ProductApiBundle\Domain\Product;
use Frontastic\Common\ProductApiBundle\Domain\Variant;
class MinimalProduct implements StreamOptimizer
{
/**
* @return mixed
*/
public function optimizeStreamData(
Stream $stream,
StreamContext $streamContext,
$data
) {
//Ignore all streams but product lists
if ($stream->type !== 'product-list') {
return $data;
}
foreach ($data->items as $index => $product) {
$data->items[$index] = new Product([
'productId' => $product->productId,
'slug' => $product->slug,
'name' => $product->name,
'variants' => [
new Variant([
'sku' => $product->variants[0]->sku,
'price' => $product->variants[0]->price,
'images' => $product->variants[0]->images,
'attributes' => array_intersect_key(
$product->variants[0]->attributes,
//Product attributes we want to keep
array_flip(['designer', 'badges'])
),
])
]
]);
}
return $data;
}
}
In this stream optimizer, we create new products with a very small data subset from the existing products. While this seems irrelevant, stripping data will help a lot because it reduces time spent in encoding and decoding data and the amount of data transmitted over the network.
Based on the Frontastic components using the current data source, you can decide which attributes you want to keep. Take a look at the StreamContext
for all this context information.
2. Register the optimizer
The next thing you need to do is register the optimizer inside the dependency injection container using the service tag frontend.streamOptimizer
.
<service id="My\StreamOptimizer\MinimalProduct">
<tag name="frontend.streamOptimizer" />
</service>
Now, all product lists will be stripped down on the server. You should see less data in the frontend and have increased performance overall.
Default implementation
Since the optimizer implemented above is a very common case, we provide you with a simple default implementation that you can use by adding the following to your dependency injection container configuration:
<service id="Frontastic\Catwalk\FrontendBundle\Domain\StreamOptimizer\MinimalProduct">
<argument type="collection">
<argument>designer</argument>
<argument>badges</argument>
</argument>
<tag name="frontend.streamOptimizer" />
</service>
Updated over 2 years ago