Basketpreview cache and user-specific data¶
eZ Commerce uses a lot of dynamic data which has to be displayed in the shop:
- basket preview shows the number of products and a list of products
- the name of the customer
- the number of items in the wishlist
To improve the caching and speed up page loading, a special route is used to provide the dynamic data using a REST call.
This enables you to cache pages in a more efficient way and place dynamic attributes with JavaScript.
The REST route¶
The route silversolutions_session_data
is used to send a call to the server to get the updated data for the dynamic attributes.
By default the following attributes are returned:
The response is cached via HTTP cache. It is purged whenever a basket is updated or a customer logs in. After page load JavaScript fetches the current data from the server and updates:
- the logged-in user
- the basket preview
JS Event¶
An event is trigged after the data has been received via REST:
1 |
|
You can subscribe to the event inside a vue app using this.$on
:
1 2 3 4 |
|
Extending the REST call¶
The modules section can be used for the project-specific data. It uses a tagged service that has to implement a service via service tag:
1 |
|
The alias mydata
is used as a key in the modules section.
Working with HTML fragments¶
Each service implementing SessionDataInterface
can return HTML fragments.
The keys should use the ID of a container inside your site. eZ Commerce automatically replaces this container with the indicated HTML.
Example for providing the data in a service:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
The corresponding div in the website needs to use the key headerLoginDesktop
, e.g. as an ID:
1 2 3 |
|
Updating existing projects¶
1. Allow accessing cookies via JS:
1 2 3 4 5 |
|
2. Add vue.js
in pagelayout.html.twig
within the header:
1 |
|
3. Add the routeconfig
to the pagelayout.html.twig
at the beginning of the javascripts
block:
1 2 3 4 5 6 7 8 |
|
4. Add three JavaScript files to pagelayout.html.twig
:
1 2 3 4 5 6 7 8 9 |
|
5. Change the Twig block basket_preview
in pagelayout.html.twig
using a static HTML markup which is replaced by JavaScript after loading the session data:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|