diff --git a/Plugin/Framework/App/Response/Http.php b/Plugin/Framework/App/Response/Http.php
new file mode 100644
index 0000000..c7f2afa
--- /dev/null
+++ b/Plugin/Framework/App/Response/Http.php
@@ -0,0 +1,35 @@
+config->isSetFlag(self::CONFIG_PATH . 'bfcache_enabled')) {
+ $excludedPaths = explode('|', $this->config->getValue(self::CONFIG_PATH . 'excluded_paths'));
+ if (!in_array($this->request->getModuleName(), $excludedPaths)) {
+ $subject->setPublicHeaders($this->pageCacheConfig->getTtl());
+ }
+ }
+ }
+}
diff --git a/Plugin/PageCache/Model/Varnish/VclGenerator.php b/Plugin/PageCache/Model/Varnish/VclGenerator.php
new file mode 100644
index 0000000..07cbf3b
--- /dev/null
+++ b/Plugin/PageCache/Model/Varnish/VclGenerator.php
@@ -0,0 +1,67 @@
+config->isSetFlag(self::CONFIG_PATH . 'bfcache_enabled')) {
+ $excludedPaths = $this->config->getValue(self::CONFIG_PATH . 'excluded_paths');
+ $bfCacheSnippet = str_replace(
+ self::EXCLUDED_PATHS_PLACEHOLDER,
+ $excludedPaths,
+ $this->getBfCacheSnippet()
+ );
+ $result = str_replace($this->getNoCacheSnippet(), $bfCacheSnippet, $result);
+ }
+ return $result;
+ }
+
+ /**
+ * Read content of nocache_snippet.vcl
+ */
+ public function getNoCacheSnippet(): string
+ {
+ $configFilePath = $this->reader->getModuleDir(Dir::MODULE_ETC_DIR, 'MageOS_ThemeOptimization');
+ $directoryRead = $this->readFactory->create($configFilePath);
+ $configFilePath = $directoryRead->getRelativePath($configFilePath . '/' . self::NOCACHE_FILE);
+ return $directoryRead->readFile($configFilePath);
+ }
+
+ /**
+ * Read content of bfcache_snippet.vcl
+ */
+ public function getBfCacheSnippet(): string
+ {
+ $configFilePath = $this->reader->getModuleDir(Dir::MODULE_ETC_DIR, 'MageOS_ThemeOptimization');
+ $directoryRead = $this->readFactory->create($configFilePath);
+ $configFilePath = $directoryRead->getRelativePath($configFilePath . '/' . self::BFCACHE_FILE);
+ return $directoryRead->readFile($configFilePath);
+ }
+}
diff --git a/README.md b/README.md
index 6348e3d..53d11c9 100644
--- a/README.md
+++ b/README.md
@@ -4,6 +4,7 @@ This module provides theme-related features to improve the performance of your M
* Page transitions when navigating between pages on Magento
* Speculative preloading of internal links on hover
+* BF Cache management
## Installation details
@@ -33,7 +34,15 @@ The module provides settings in the Magento Admin Panel under:
• **Exclude Selectors** - CSS selectors for links to never preload. Enter one selector per line. (Default: .do-not-prerender)
-All values can be configured at Default, Website, and Store View scopes.
+**Stores > Configuration > Advanced > System > Full Page Cache**
+
+The module adds two new settings for bf cache management:
+
+• **Enable Back Forward Cache** - Decide if Bf should be enabled or not
+
+• **Excluded paths** - Specify a list of paths separated by "|" that mustn't be cached by the browser.
+
+When "Varnish Cache" is selected as "Caching Application" this module will update the VCL generated inside the "varnish" section below using "Excluded paths".
There is no configuration for the Page Transitions feature. When installed, page transitions are always enabled for all Magento themes (frontend and admin panel).
diff --git a/ViewModel/BFCacheSections.php b/ViewModel/BFCacheSections.php
new file mode 100644
index 0000000..98eb9a3
--- /dev/null
+++ b/ViewModel/BFCacheSections.php
@@ -0,0 +1,29 @@
+ "'$v'", $this->sectionConfig->getSections()[self::SECTION_TO_CHECK])
+ );
+ }
+}
diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml
index 974bf87..3bc456b 100644
--- a/etc/adminhtml/system.xml
+++ b/etc/adminhtml/system.xml
@@ -51,5 +51,21 @@
+
+
+
+
+ Magento\Config\Model\Config\Source\Yesno
+ Back-forward caching (or bfcache) is a browser optimization that allows instant forward and backward navigation. It significantly improves the browsing experience, especially for users with slower networks or devices.
+
+
+
+ Pipe separated controller frontnames to serve with 'Cache-Control: no-store, no-cache, must-revalidate, max-age=0' response headers
+
+ 1
+
+
+
+
diff --git a/etc/bfcache_snippet.vcl b/etc/bfcache_snippet.vcl
new file mode 100644
index 0000000..5a858ab
--- /dev/null
+++ b/etc/bfcache_snippet.vcl
@@ -0,0 +1,6 @@
+ # Not letting browser to cache non-static files and excluded paths by magento bfcache config
+ if (req.url ~ "^/(/* {{ excluded_paths }} */)" && resp.http.Cache-Control !~ "private") {
+ set resp.http.Pragma = "no-cache";
+ set resp.http.Expires = "-1";
+ set resp.http.Cache-Control = "no-store, no-cache, must-revalidate, max-age=0";
+ }
diff --git a/etc/config.xml b/etc/config.xml
index 3dc8215..a58c37c 100644
--- a/etc/config.xml
+++ b/etc/config.xml
@@ -21,5 +21,11 @@ productalert
.do-not-prerender
+
+
+ 0
+ checkout|cart|customer|wishlist|downloadable|sales|vault|review|newsletter
+
+
diff --git a/etc/di.xml b/etc/di.xml
new file mode 100644
index 0000000..22b4304
--- /dev/null
+++ b/etc/di.xml
@@ -0,0 +1,7 @@
+
+
+
+
+
+
+
diff --git a/etc/frontend/di.xml b/etc/frontend/di.xml
new file mode 100644
index 0000000..98fedc4
--- /dev/null
+++ b/etc/frontend/di.xml
@@ -0,0 +1,7 @@
+
+
+
+
+
+
+
diff --git a/etc/frontend/sections.xml b/etc/frontend/sections.xml
new file mode 100644
index 0000000..a36b027
--- /dev/null
+++ b/etc/frontend/sections.xml
@@ -0,0 +1,13 @@
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/etc/module.xml b/etc/module.xml
index 2dd2d99..f74465d 100644
--- a/etc/module.xml
+++ b/etc/module.xml
@@ -4,6 +4,7 @@
+
diff --git a/etc/nocache_snippet.vcl b/etc/nocache_snippet.vcl
new file mode 100644
index 0000000..ae030e5
--- /dev/null
+++ b/etc/nocache_snippet.vcl
@@ -0,0 +1,6 @@
+ # Not letting browser to cache non-static files.
+ if (resp.http.Cache-Control !~ "private" && req.url !~ "^/(pub/)?(media|static)/") {
+ set resp.http.Pragma = "no-cache";
+ set resp.http.Expires = "-1";
+ set resp.http.Cache-Control = "no-store, no-cache, must-revalidate, max-age=0";
+ }
diff --git a/view/frontend/layout/default.xml b/view/frontend/layout/default.xml
index 56a29d7..00a5d1c 100644
--- a/view/frontend/layout/default.xml
+++ b/view/frontend/layout/default.xml
@@ -10,6 +10,14 @@
\MageOS\ThemeOptimization\ViewModel\SpeculationRules
+
+
+ \MageOS\ThemeOptimization\ViewModel\BFCacheSections
+
+