@@ -58,8 +58,8 @@ sub vcl_recv {
58
58
return (pass );
59
59
}
60
60
61
- # Bypass shopping cart and checkout
62
- if (req.url ~ " /checkout" ) {
61
+ # Bypass customer, shopping cart, checkout
62
+ if (req.url ~ " /customer " || req.url ~ " / checkout" ) {
63
63
return (pass );
64
64
}
65
65
@@ -137,21 +137,6 @@ sub vcl_recv {
137
137
# collect all cookies
138
138
std.collect (req.http.Cookie );
139
139
140
- # Compression filter. See https://www.varnish-cache.org/trac/wiki/FAQ/Compression
141
- if (req.http.Accept-Encoding ) {
142
- if (req.url ~ " \.(jpg|jpeg|png|gif|gz|tgz|bz2|tbz|mp3|ogg|swf|flv)$" ) {
143
- # No point in compressing these
144
- unset req.http.Accept-Encoding ;
145
- } elsif (req.http.Accept-Encoding ~ " gzip" ) {
146
- set req.http.Accept-Encoding = " gzip" ;
147
- } elsif (req.http.Accept-Encoding ~ " deflate" && req.http.user-agent !~ " MSIE" ) {
148
- set req.http.Accept-Encoding = " deflate" ;
149
- } else {
150
- # unknown algorithm
151
- unset req.http.Accept-Encoding ;
152
- }
153
- }
154
-
155
140
# Remove all marketing get parameters to minimize the cache objects
156
141
if (req.url ~ " (\?|&)(gclid|cx|ie|cof|siteurl|zanpid|origin|fbclid|mc_[a-z]+|utm_[a-z]+|_bta_[a-z]+)=" ) {
157
142
set req.url = regsuball (req.url , " (gclid|cx|ie|cof|siteurl|zanpid|origin|fbclid|mc_[a-z]+|utm_[a-z]+|_bta_[a-z]+)=[-_A-z0-9+()%.]+&?" , " " );
@@ -169,34 +154,43 @@ sub vcl_recv {
169
154
#unset req.http.Cookie;
170
155
}
171
156
172
- # Authenticated GraphQL requests should not be cached by default
173
- if (req.url ~ " /graphql" && req.http.Authorization ~ " ^Bearer" ) {
157
+ # Bypass authenticated GraphQL requests without a X-Magento-Cache-Id
158
+ if (req.url ~ " /graphql" && ! req.http.X-Magento-Cache-Id && req.http.Authorization ~ " ^Bearer" ) {
174
159
return (pass );
175
160
}
176
161
177
162
return (hash );
178
163
}
179
164
180
165
sub vcl_hash {
181
- if (req.http.cookie ~ " X-Magento-Vary=" ) {
166
+ if (( req.url !~ " /graphql " || ! req.http.X-Magento-Cache-Id ) && req.http.cookie ~ " X-Magento-Vary=" ) {
182
167
hash_data (regsub (req.http.cookie , " ^.*?X-Magento-Vary=([^;]+);*.*$" , " \1" ));
183
168
}
184
169
185
170
# To make sure http users don't see ssl warning
186
171
if (req.http.X-Forwarded-Proto ) {
187
172
hash_data (req.http.X-Forwarded-Proto );
188
173
}
189
-
190
174
191
175
if (req.url ~ " /graphql" ) {
192
176
call process_graphql_headers;
193
177
}
194
178
}
195
179
196
180
sub process_graphql_headers {
181
+ if (req.http.X-Magento-Cache-Id ) {
182
+ hash_data (req.http.X-Magento-Cache-Id );
183
+
184
+ # When the frontend stops sending the auth token, make sure users stop getting results cached for logged-in users
185
+ if (req.http.Authorization ~ " ^Bearer" ) {
186
+ hash_data (" Authorized" );
187
+ }
188
+ }
189
+
197
190
if (req.http.Store ) {
198
191
hash_data (req.http.Store );
199
192
}
193
+
200
194
if (req.http.Content-Currency ) {
201
195
hash_data (req.http.Content-Currency );
202
196
}
@@ -218,53 +212,58 @@ sub vcl_backend_response {
218
212
set beresp.http.X-Magento-Cache-Control = beresp.http.Cache-Control ;
219
213
}
220
214
221
- # cache only successfully responses and 404s
222
- if (beresp.status != 200 && beresp.status != 404 ) {
223
- set beresp.ttl = 0s ;
224
- set beresp.uncacheable = true ;
225
- return (deliver );
226
- } elsif (beresp.http.Cache-Control ~ " private" ) {
215
+ # cache only successfully responses and 404s that are not marked as private
216
+ if ((beresp.status != 200 && beresp.status != 404 ) || beresp.http.Cache-Control ~ " private" ) {
227
217
set beresp.uncacheable = true ;
228
218
set beresp.ttl = 86400s ;
229
219
return (deliver );
230
220
}
231
221
232
222
# validate if we need to cache it and prevent from setting cookie
233
223
if (beresp.ttl > 0s && (bereq.method == " GET" || bereq.method == " HEAD" )) {
224
+ # Collapse beresp.http.set-cookie in order to merge multiple set-cookie headers
225
+ # Although it is not recommended to collapse set-cookie header,
226
+ # it is safe to do it here as the set-cookie header is removed below
227
+ std.collect (beresp.http.set-cookie );
228
+ # Do not cache the response under current cache key (hash),
229
+ # if the response has X-Magento-Vary but the request does not.
230
+ if ((bereq.url !~ " /graphql" || !bereq.http.X-Magento-Cache-Id )
231
+ && bereq.http.cookie !~ " X-Magento-Vary="
232
+ && beresp.http.set-cookie ~ " X-Magento-Vary=" ) {
233
+ set beresp.ttl = 0s ;
234
+ set beresp.uncacheable = true ;
235
+ }
234
236
unset beresp.http.set-cookie ;
235
237
}
236
238
237
- # If page is not cacheable then bypass varnish for 2 minutes as Hit-For-Pass
238
- if (beresp.ttl <= 0s ||
239
- beresp.http.Surrogate-control ~ " no-store" ||
240
- (!beresp.http.Surrogate-Control &&
241
- beresp.http.Cache-Control ~ " no-cache|no-store" ) ||
242
- beresp.http.Vary == " *" ) {
239
+ # If page is not cacheable then bypass varnish for 2 minutes as Hit-For-Pass
240
+ if (beresp.ttl <= 0s ||
241
+ beresp.http.Surrogate-control ~ " no-store" ||
242
+ (!beresp.http.Surrogate-Control &&
243
+ beresp.http.Cache-Control ~ " no-cache|no-store" ) ||
244
+ beresp.http.Vary == " *" ) {
243
245
# Mark as Hit-For-Pass for the next 2 minutes
244
246
set beresp.ttl = 120s ;
245
247
set beresp.uncacheable = true ;
246
248
}
247
249
250
+ # If the cache key in the Magento response doesn't match the one that was sent in the request, don't cache under the request's key
251
+ if (bereq.url ~ " /graphql" && bereq.http.X-Magento-Cache-Id && bereq.http.X-Magento-Cache-Id != beresp.http.X-Magento-Cache-Id ) {
252
+ set beresp.ttl = 0s ;
253
+ set beresp.uncacheable = true ;
254
+ }
255
+
248
256
return (deliver );
249
257
}
250
258
251
259
sub vcl_deliver {
252
- if (resp.http.X-Magento-Debug ) {
253
- if (resp.http.x-varnish ~ " " ) {
254
- set resp.http.X-Magento-Cache-Debug = " HIT" ;
255
- set resp.http.Grace = req.http.grace ;
256
- } else {
257
- set resp.http.X-Magento-Cache-Debug = " MISS" ;
258
- }
260
+ if (obj.uncacheable ) {
261
+ set resp.http.X-Magento-Cache-Debug = " UNCACHEABLE" ;
262
+ } else if (obj.hits ) {
263
+ set resp.http.X-Magento-Cache-Debug = " HIT" ;
264
+ set resp.http.Grace = req.http.grace ;
259
265
} else {
260
- unset resp.http.Age ;
261
- unset resp.http.X-Magento-Debug ;
262
- unset resp.http.X-Magento-Tags ;
263
- unset resp.http.X-Powered-By ;
264
- unset resp.http.Server ;
265
- unset resp.http.X-Varnish ;
266
- unset resp.http.Via ;
267
- unset resp.http.Link ;
266
+ set resp.http.X-Magento-Cache-Debug = " MISS" ;
268
267
}
269
268
270
269
# Not letting browser to cache non-static files.
@@ -273,6 +272,17 @@ sub vcl_deliver {
273
272
set resp.http.Expires = " -1" ;
274
273
set resp.http.Cache-Control = " no-store, no-cache, must-revalidate, max-age=0" ;
275
274
}
275
+
276
+ if (!resp.http.X-Magento-Debug ) {
277
+ unset resp.http.Age ;
278
+ }
279
+ unset resp.http.X-Magento-Debug ;
280
+ unset resp.http.X-Magento-Tags ;
281
+ unset resp.http.X-Powered-By ;
282
+ unset resp.http.Server ;
283
+ unset resp.http.X-Varnish ;
284
+ unset resp.http.Via ;
285
+ unset resp.http.Link ;
276
286
}
277
287
278
288
sub vcl_hit {
0 commit comments