44E.g. when I used ` docker network create some-net ` , I have network with name ` vuestorefrontapi_some-net `
553 . Open docker-compose.yml:
66At the end:
7- ```
7+ ``` yaml
88networks :
99 vuestorefrontapi_some-net :
1010 external : true
@@ -13,7 +13,7 @@ Set vuestorefrontapi_some-net to your network name
1313
14144. Check each ` docker-compose` file and set proper network name.
15155. In the docker-compose.nodejs.yml it should not have a prefix, e.g :
16- ```
16+ ` ` ` yaml
1717 networks:
1818 - some-net
1919
@@ -24,17 +24,17 @@ networks:
2424
2525# ## How does it work?
26261. I add output tags to the VSF-API response :
27- ```
27+ ` ` ` js
2828const tagsHeader = output.tags.join(' ')
2929res.setHeader('X-VS-Cache-Tag', tagsHeader)
3030` ` `
3131
32322. After it invalidates cache in the Redis. I forward request to the :
33- ```
33+ ` ` ` js
3434http://${config.varnish.host}:${config.varnish.port}/
3535` ` `
3636With invalidate tag in headers :
37- ```
37+ ` ` ` js
3838headers: {
3939 "X-VS-Cache-Tag": tag
4040}
@@ -44,15 +44,15 @@ I set Varnish invalidate method to `BAN` but you can change it in your config +
4444
45453. Configuration of BANning we have inside `docker/varnish/config.vcl` in `vcl_recv`.
4646It tries to BAN resource which has `X-VS-Cache-Tag` header :
47- ```
47+ ` ` ` vcl
4848# Logic for the ban, using the X-Cache-Tag header.
4949if (req.http.X-VS-Cache-Tag) {
5050 ban("obj.http.X-VS-Cache-Tag ~ " + req.http.X-VS-Cache-Tag);
5151}
5252` ` `
5353
5454Below under BANning logic. I have to tell Varnish what to cache.
55- ```
55+ ` ` ` vcl
5656if (req.url ~ "^\/ api\/ catalog\/ ") {
5757 if (req.method == "POST") {
5858 # It will allow me to cache by req body in the vcl_hash
@@ -70,12 +70,12 @@ I am caching request that starts with `/api/catalog/`. As you can see I cache bo
7070This is because in my project I use huge ES requests to compute Facted Filters. I would exceed HTTP GET limit.
7171
7272Thanks to this line and `bodyaccess`, I can distinguish requests to the same URL by their body!
73- ```
73+ ` ` ` vcl
7474std.cache_req_body(500KB);
7575` ` `
7676
7777Then in `vcl_hash` I create hash for POST requests with `bodyaccess.hash_req_body()` :
78- ```
78+ ` ` ` vcl
7979sub vcl_hash {
8080 # To cache POST and PUT requests
8181 if (req.http.X-Body-Len) {
@@ -88,7 +88,7 @@ sub vcl_hash {
8888
8989By default, Varnish change each request to HTTP GET. We need to tell him to send POST requests to the VSF-API as POST - not GET.
9090We will do it like that :
91- ```
91+ ` ` ` vcl
9292sub vcl_backend_fetch {
9393 if (bereq.http.X-Body-Len) {
9494 set bereq.method = "POST";
@@ -101,7 +101,7 @@ sub vcl_backend_fetch {
101101It might be a good idea to cache stock requests if you check it lifetime in VSF-PWA in visiblityChanged hook (product listing).
102102In one project when I have slow Magento - it reduced Time-To-Response from ~2s to ~70ms.
103103
104- ```
104+ ` ` ` vcl
105105if (req.url ~ "^\/ api\/ stock\/ ") {
106106 if (req.method == "GET") {
107107 # M2 Stock
@@ -111,7 +111,7 @@ if (req.url ~ "^\/api\/stock\/") {
111111` ` `
112112
113113Then in `vcl_backend_response` you should set safe TTL (Time to live) for your stock cache. I've set 15 minutes (900 seconds)
114- ```
114+ ` ` ` vcl
115115sub vcl_backend_response {
116116 # Set ban-lurker friendly custom headers.
117117 if (beresp.http.X-VS-Cache && beresp.http.X-VS-Cache ~ "Miss") {
@@ -135,7 +135,7 @@ to around ~50ms.
135135
136136How to do that?
137137Inside `vcl_recv` add :
138- ```
138+ ` ` ` vcl
139139// As in my case I want to cache only GET requests
140140if (req.method == "GET") {
141141 # Countries for storecode GET - M2 - /directory/countries
@@ -173,15 +173,15 @@ If you would provide here `product` it would cache product's catalog. You should
173173
174174# ## Banning permissions
175175It will be allowed only from certain IPs. In my case I put here only VSF-API IP. But here we have `app` as Docker will resolve it as VSF-API IP :
176- ```
176+ ` ` ` vcl
177177acl purge {
178178 "app"; // IP which can BAN cache - it should be VSF-API's IP
179179}
180180` ` `
181181
182182# ## What to cache
183183We should provide to Varnish - IP & Port to cache, there we have it :
184- ```
184+ ` ` ` vcl
185185backend default {
186186 .host = "app";
187187 .port = "8080";
0 commit comments