recipes
recipes copied to clipboard
feat: add new caching VCL
see core change: https://github.com/shopware/shopware/pull/13180
Thanks for the PR 😍
How to test these changes in your application
-
Add the Shopware flex endpoint in your
composer.jsonto https://raw.githubusercontent.com/shopware/recipes/flex/pull-219/index.json.# When jq is installed jq '.extra.symfony.endpoint |= [ "https://raw.githubusercontent.com/shopware/recipes/flex/pull-219/index.json" ] + .' composer.json > composer.tmp && mv composer.tmp composer.jsonor manually
"endpoint": [ "https://raw.githubusercontent.com/shopware/recipes/flex/pull-219/index.json", "https://raw.githubusercontent.com/shopware/recipes/flex/main/index.json", "flex://defaults" ] # On Unix-like (BSD, Linux and macOS) export SYMFONY_ENDPOINT=https://raw.githubusercontent.com/shopware/recipes/flex/pull-219/index.json # On Windows SET SYMFONY_ENDPOINT=https://raw.githubusercontent.com/shopware/recipes/flex/pull-219/index.json -
Install the package(s) related to this recipe:
composer req 'shopware/fastly-meta:^6.8'
Diff between recipe versions
In order to help with the review stage, I'm in charge of computing the diff between the various versions of patched recipes. I'm going keep this comment up to date with any updates of the attached patch.
shopware/fastly-meta
6.4 vs 6.5
diff --git a/shopware/fastly-meta/6.4/config/packages/fastly.yaml b/shopware/fastly-meta/6.5/config/packages/fastly.yaml
index 704768a..ae3c77d 100644
--- a/shopware/fastly-meta/6.4/config/packages/fastly.yaml
+++ b/shopware/fastly-meta/6.5/config/packages/fastly.yaml
@@ -10,5 +10,4 @@ when@prod:
enabled: true
api_key: '%env(FASTLY_API_TOKEN)%'
service_id: '%env(FASTLY_SERVICE_ID)%'
- # Uncomment to enable soft purge
- # soft_purge: '1'
+ soft_purge: '1'
6.5 vs 6.6
diff --git a/shopware/fastly-meta/6.5/bin/setup-fastly.sh b/shopware/fastly-meta/6.6/bin/setup-fastly.sh
index 2c2f627..95f3e68 100755
--- a/shopware/fastly-meta/6.5/bin/setup-fastly.sh
+++ b/shopware/fastly-meta/6.6/bin/setup-fastly.sh
@@ -1,136 +1,7 @@
#!/usr/bin/env bash
-# fail on error
-set -e
+echo "This script is deprecated and will be removed in the next major release. Please use vendor/bin/shopware-deployment-helper fastly:snippet:deploy directly."
-if [[ -z "$FASTLY_API_TOKEN" ]]; then
- echo "Environment variable FASTLY_API_TOKEN is not set. Skipping"
- exit 0
-fi
+sleep 5
-if [[ -z "$FASTLY_SERVICE_ID" ]]; then
- echo "Environment variable FASTLY_SERVICE_ID is not set. Skipping"
- exit 0
-fi
-
-CWD="$(cd -P -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P)"
-PROJECT_ROOT="${PROJECT_ROOT:-"$(dirname "$CWD")"}"
-
-cd "$PROJECT_ROOT"
-
-created_version=0
-
-create_version_if_not_done() {
- if [[ "$created_version" == "1" ]]; then
- return
- fi
-
- echo "Creating version from active version"
- fastly service-version clone --version=active
- created_version=1
-}
-
-get_md5()
-{
- if builtin command -v md5 > /dev/null; then
- echo "$1" | md5
- elif builtin command -v md5sum > /dev/null ; then
- echo "$1" | md5sum | awk '{print $1}'
- else
- echo "Neither md5 nor md5sum were found in the PATH"
- return 1
- fi
-
- return 0
-}
-
-install_fastly_cli() {
- if [[ -f "/tmp/fastly/fastly" ]]; then
- export PATH="/tmp/fastly:$PATH"
- return
- fi
-
- mkdir -p /tmp/fastly
-
- arch=$(uname -m)
- os="linux"
- version="v10.6.4"
-
- if [[ "$arch" == "x86_64" ]]; then
- arch="amd64"
- fi
-
- if [[ "$OSTYPE" == "darwin"* ]]; then
- os="darwin"
- fi
-
- echo "Detected OS: ${os} and architecture: ${arch}"
-
- file="https://github.com/fastly/cli/releases/download/${version}/fastly_${version}_${os}-${arch}.tar.gz"
-
- echo "Downloading ${file}"
-
- curl -L "${file}" | tar xz -C /tmp/fastly/
- export PATH="/tmp/fastly:$PATH"
-}
-
-install_fastly_cli
-
-# Fastly tries to write into /app on platformsh and this throws an error
-export HOME=/tmp
-
-for sub in ./config/fastly/*; do
- if ! find "$sub" -name '*.vcl' | grep . >/dev/null; then
- # No VCL files in "$sub"; moving on to the next dir
- continue
- fi
- for vcl in "$sub"/*.vcl; do
- trigger=$(basename "$vcl" .vcl)
- priority=$(echo "$trigger" | awk -F '.' '$2 ~ /^[0-9]+$/ { print $2 }')
- if test -z "$priority"; then
- priority="100"
- else
- trigger=$(basename "$trigger" ".$priority")
- fi
- vcl_type=$(basename "$sub")
-
- # For backward compatibility, default VCLs doesn't include trigger in their name
- if [[ "$trigger" == "default" ]]; then
- name="shopware_${vcl_type}"
- else
- name="shopware_${vcl_type}_${trigger}"
- fi
-
- echo "Found VCL snippet $trigger of type $vcl_type with priority $priority"
-
- if fastly vcl snippet describe --version=active "--name=$name" > /dev/null; then
- # The snippet exists on remote
- localContent=$(cat "$vcl")
- localContentMd5=$(get_md5 "$localContent")
-
- remoteContent=$(fastly vcl snippet describe --version=active "--name=$name" --json | jq -r '.Content')
- remoteContentMd5=$(get_md5 "$remoteContent")
-
- if [[ "$localContentMd5" != "$remoteContentMd5" ]]; then
- echo "Snippet ${name} has changed. Updating"
-
- create_version_if_not_done
-
- fastly vcl snippet update "--name=$name" "--content=${vcl}" "--type=${vcl_type}" "--priority=${priority}" --version=latest
- else
- echo "Snippet ${name} is up to date"
- fi
- else
- create_version_if_not_done
-
- fastly vcl snippet create "--name=$name" "--content=${vcl}" "--type=${vcl_type}" "--priority=${priority}" --version=latest
- fi
- done
-done
-
-
-if [[ "$created_version" == "1" ]]; then
- echo "Activating latest version"
-
- fastly service-version activate --version latest
-fi
+php vendor/bin/shopware-deployment-helper fastly:snippet:deploy
diff --git a/shopware/fastly-meta/6.5/config/packages/fastly.yaml b/shopware/fastly-meta/6.6/config/packages/prod/fastly.yaml
similarity index 55%
rename from shopware/fastly-meta/6.5/config/packages/fastly.yaml
rename to shopware/fastly-meta/6.6/config/packages/prod/fastly.yaml
index ae3c77d..3ff54bb 100644
--- a/shopware/fastly-meta/6.5/config/packages/fastly.yaml
+++ b/shopware/fastly-meta/6.6/config/packages/prod/fastly.yaml
@@ -1,8 +1,11 @@
-when@prod:
- storefront:
+# yaml-language-server: $schema=https://raw.githubusercontent.com/shopware/shopware/trunk/config-schema.json
+
+shopware:
+ http_cache:
# Uncomment all to enable soft purge
- # http_cache:
+ # Allow to serve the out-dated cache for 300 seconds
# stale_while_revalidate: 300
+ # Allow to serve the out-dated cache for an hour if the origin server is offline
# stale_if_error: 3600
reverse_proxy:
enabled: true
diff --git a/shopware/fastly-meta/6.5/post-install.txt b/shopware/fastly-meta/6.6/post-install.txt
index 60560a5..396697a 100644
--- a/shopware/fastly-meta/6.5/post-install.txt
+++ b/shopware/fastly-meta/6.6/post-install.txt
@@ -1,2 +1,4 @@
* Add your FASTLY_API_TOKEN and FASTLY_SERVICE_ID into .env
- * Run setup-fastly.sh to deploy the Fastly vcl files
+ * Run vendor/bin/shopware-deployment-helper fastly:snippet:deploy to deploy the Fastly snippets
+ * The Fastly snippets are now deployed and you can start using the Fastly CDN
+ * The snippets are also updated on regular vendor/bin/shopware-deployment-helper run runs
6.6 vs 6.7
diff --git a/shopware/fastly-meta/6.6/bin/setup-fastly.sh b/shopware/fastly-meta/6.6/bin/setup-fastly.sh
deleted file mode 100755
index 95f3e68..0000000
--- a/shopware/fastly-meta/6.6/bin/setup-fastly.sh
+++ /dev/null
@@ -1,7 +0,0 @@
-#!/usr/bin/env bash
-
-echo "This script is deprecated and will be removed in the next major release. Please use vendor/bin/shopware-deployment-helper fastly:snippet:deploy directly."
-
-sleep 5
-
-php vendor/bin/shopware-deployment-helper fastly:snippet:deploy
diff --git a/shopware/fastly-meta/6.6/config/fastly/hash/default.vcl b/shopware/fastly-meta/6.7/config/fastly/hash/default.vcl
index e9e93c5..27b3f37 100644
--- a/shopware/fastly-meta/6.6/config/fastly/hash/default.vcl
+++ b/shopware/fastly-meta/6.7/config/fastly/hash/default.vcl
@@ -1,6 +1,6 @@
# Consider Shopware http cache cookies
-if (req.http.cookie:sw-cache-hash) {
- set req.hash += req.http.cookie:sw-cache-hash;
+if (req.http.sw-cache-hash) {
+ set req.hash += req.http.sw-cache-hash;
} elseif (req.http.cookie:sw-currency) {
set req.hash += req.http.cookie:sw-currency;
}
\ No newline at end of file
diff --git a/shopware/fastly-meta/6.6/config/fastly/recv/default.vcl b/shopware/fastly-meta/6.7/config/fastly/recv/default.vcl
index 31f2bad..1b61b3f 100644
--- a/shopware/fastly-meta/6.6/config/fastly/recv/default.vcl
+++ b/shopware/fastly-meta/6.7/config/fastly/recv/default.vcl
@@ -54,9 +54,24 @@ if (req.http.Authenticate || req.http.Authorization) {
set req.http.x-pass = "1";
}
-# Always pass these paths directly to php without caching
+# Micro-optimization: Always pass these paths directly to php without caching
+# to prevent hashing and cache lookup overhead
# Note: virtual URLs might bypass this rule (e.g. /en/checkout)
-if (req.url.path ~ "^/(checkout|account|admin|api|csrf)(/.*)?$") {
+if (req.url.path ~ "^/(checkout|account|admin|api)(/.*)?$") {
+ set req.http.x-pass = "1";
+}
+
+# set cache-hash cookie value to header for hashing based on vary header
+# if header is provided directly the header will take precedence
+if (std.strlen(req.http.sw-cache-hash) == 0) {
+ set req.http.sw-cache-hash = req.http.cookie:sw-cache-hash;
+}
+
+# immediately pass when hash indicates that the content should not be cached
+# note that cache-hash = "not-cacheable" is used to indicate an application state in which the cache should be passed
+# we can not use cache-control headers in that case, as reverse proxies expect to always get the same cache-control headers based on the route
+# dynamically changing the cache-control header is not supported
+if (req.http.sw-cache-hash == "not-cacheable") {
set req.http.x-pass = "1";
}
diff --git a/shopware/fastly-meta/6.6/manifest.json b/shopware/fastly-meta/6.7/manifest.json
index 26dffc5..44bd3d9 100644
--- a/shopware/fastly-meta/6.6/manifest.json
+++ b/shopware/fastly-meta/6.7/manifest.json
@@ -1,8 +1,7 @@
{
"aliases": ["fastly"],
"copy-from-recipe": {
- "config/": "%CONFIG_DIR%/",
- "bin/": "%BIN_DIR%/"
+ "config/": "%CONFIG_DIR%/"
},
"env": {
"FASTLY_API_TOKEN": "EXAMPLE_TOKEN",
6.7 vs 6.8
diff --git a/shopware/fastly-meta/6.7/config/fastly/deliver/default.vcl b/shopware/fastly-meta/6.8/config/fastly/deliver/default.vcl
index 8e60bce..74b4869 100644
--- a/shopware/fastly-meta/6.7/config/fastly/deliver/default.vcl
+++ b/shopware/fastly-meta/6.8/config/fastly/deliver/default.vcl
@@ -1,12 +1,2 @@
# Remove the exact PHP Version from the response for more security (e.g. 404 pages)
unset resp.http.x-powered-by;
-
-# We use fastly.ff.visits_this_service to avoid running this logic on shield nodes. We only need to
-# run it on edge nodes
-if (fastly.ff.visits_this_service == 0 && resp.http.sw-invalidation-states) {
- # invalidation headers are only for internal use
- unset resp.http.sw-invalidation-states;
-
- ## we don't want the client to cache
- set resp.http.Cache-Control = "no-cache, private";
-}
diff --git a/shopware/fastly-meta/6.7/config/fastly/hash/default.vcl b/shopware/fastly-meta/6.7/config/fastly/hash/default.vcl
deleted file mode 100644
index 27b3f37..0000000
--- a/shopware/fastly-meta/6.7/config/fastly/hash/default.vcl
+++ /dev/null
@@ -1,6 +0,0 @@
-# Consider Shopware http cache cookies
-if (req.http.sw-cache-hash) {
- set req.hash += req.http.sw-cache-hash;
-} elseif (req.http.cookie:sw-currency) {
- set req.hash += req.http.cookie:sw-currency;
-}
\ No newline at end of file
diff --git a/shopware/fastly-meta/6.7/config/fastly/hit/default.vcl b/shopware/fastly-meta/6.7/config/fastly/hit/default.vcl
deleted file mode 100644
index 0d06192..0000000
--- a/shopware/fastly-meta/6.7/config/fastly/hit/default.vcl
+++ /dev/null
@@ -1,11 +0,0 @@
-if (req.http.cookie:sw-states) {
- set req.http.states = req.http.cookie:sw-states;
-
- if (req.http.states ~ "logged-in" && obj.http.sw-invalidation-states ~ "logged-in" ) {
- return (pass);
- }
-
- if (req.http.states ~ "cart-filled" && obj.http.sw-invalidation-states ~ "cart-filled" ) {
- return (pass);
- }
-}
\ No newline at end of file
@shyim any idea how to fix the failing CI job? of course it cannot setup a 6.8 recipe because 6.8 is not yet there, can we just ignore the error?