-
Notifications
You must be signed in to change notification settings - Fork 7
Implement Back/Forward Cache functionality with configuration options… #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a7369e2
Implement Back/Forward Cache functionality with configuration options…
JaJuMa eab926a
Add canRestore attribute to Back/Forward Cache configuration fields
JaJuMa c35d573
Refactor BFCache configuration and functionality for improved clarity
JaJuMa e4767a2
Improved type safety of config load and check
rhoerr fe79eca
Config wording adjustments
rhoerr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| <?php declare(strict_types=1); | ||
|
|
||
| namespace MageOS\ThemeOptimization\Plugin\Framework\App\Response; | ||
|
|
||
| use Magento\Framework\App\Config\ScopeConfigInterface; | ||
| use Magento\Framework\App\Request\Http as HttpRequest; | ||
| use Magento\PageCache\Model\Config; | ||
| use Magento\Store\Model\ScopeInterface; | ||
|
|
||
| /** | ||
| * Plugin to modify cache headers for BFCache functionality | ||
| */ | ||
| class Http | ||
| { | ||
| /** @var string */ | ||
| public const XML_PATH_ENABLE = 'system/bfcache/general/enable'; | ||
|
|
||
| /** @var string */ | ||
| public const XML_PATH_EXCLUDE_URL_PATTERNS = 'system/bfcache/scope/exclude_url_patterns'; | ||
|
|
||
| /** @var bool */ | ||
| private $isRequestCacheable = false; | ||
|
|
||
| /** | ||
| * @param Config $config | ||
| * @param ScopeConfigInterface $scopeConfig | ||
| * @param HttpRequest $request | ||
| */ | ||
| public function __construct( | ||
| private Config $config, | ||
| private ScopeConfigInterface $scopeConfig, | ||
| private HttpRequest $request | ||
| ) { | ||
| } | ||
|
|
||
| /** | ||
| * Intercept before setting no-cache headers to determine if request is cacheable | ||
| * | ||
| * @param \Magento\Framework\App\Response\Http $subject | ||
| * @return void | ||
| */ | ||
| public function beforeSetNoCacheHeaders(\Magento\Framework\App\Response\Http $subject): void | ||
| { | ||
| if ($this->config->getType() !== Config::BUILT_IN || !$this->isEnabled()) { | ||
| return; | ||
| } | ||
|
|
||
| $cacheControlHeader = $subject->getHeader('Cache-Control'); | ||
| if (!$cacheControlHeader) { | ||
| return; | ||
| } | ||
|
|
||
| $cacheControl = $cacheControlHeader->getFieldValue(); | ||
| $requestURI = ltrim($this->request->getRequestURI(), '/'); | ||
|
|
||
| if ($this->isRequestCacheable($cacheControl) && !$this->isRequestInExcludePatterns($requestURI)) { | ||
| $this->isRequestCacheable = true; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Update cache headers after setting no-cache headers | ||
| * | ||
| * @param \Magento\Framework\App\Response\Http $subject | ||
| * @param mixed $result | ||
| * @return mixed | ||
| */ | ||
| public function afterSetNoCacheHeaders(\Magento\Framework\App\Response\Http $subject, $result) | ||
| { | ||
| if ($this->config->getType() !== Config::BUILT_IN || !$this->isEnabled()) { | ||
| return $result; | ||
| } | ||
|
|
||
| $cacheControlHeader = $subject->getHeader('Cache-Control'); | ||
| if (!$cacheControlHeader) { | ||
| return $result; | ||
| } | ||
|
|
||
| if ($this->isRequestCacheable === true) { | ||
| $cacheControlHeader = $subject->getHeader('Cache-Control'); | ||
| $cacheControlHeader->removeDirective('no-store'); | ||
| } | ||
| $this->isRequestCacheable = false; | ||
|
|
||
| return $result; | ||
| } | ||
|
|
||
| /** | ||
| * Check if request is cacheable based on cache control header | ||
| * | ||
| * @param string $cacheControl | ||
| * @return bool | ||
| */ | ||
| private function isRequestCacheable(string $cacheControl): bool | ||
| { | ||
| return (bool) preg_match('/public.*s-maxage=(\d+)/', $cacheControl); | ||
| } | ||
|
|
||
| /** | ||
| * Check if the request URI contains any excluded URL patterns (case-insensitive, partial match). | ||
| * | ||
| * @param string $requestURI | ||
| * @return bool | ||
| */ | ||
| private function isRequestInExcludePatterns(string $requestURI): bool | ||
| { | ||
| $patterns = $this->getConfig(self::XML_PATH_EXCLUDE_URL_PATTERNS); | ||
|
|
||
| if (empty($patterns)) { | ||
| return false; | ||
| } | ||
|
|
||
| foreach ($this->parseExcludePatterns($patterns) as $pattern) { | ||
| if ($pattern !== '' && mb_stripos($requestURI, $pattern, 0, 'UTF-8') !== false) { | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| /** | ||
| * Parse exclude patterns from config string. | ||
| * | ||
| * @param string $patterns | ||
| * @return array | ||
| */ | ||
| private function parseExcludePatterns(string $patterns): array | ||
| { | ||
| return array_filter(array_map('trim', explode("\n", $patterns))); | ||
| } | ||
|
|
||
| /** | ||
| * Check if BFCache is enabled | ||
| * | ||
| * @return bool | ||
| */ | ||
| private function isEnabled(): bool | ||
| { | ||
| return $this->scopeConfig->isSetFlag( | ||
| self::XML_PATH_ENABLE, | ||
| ScopeInterface::SCOPE_STORE | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Get configuration value by path | ||
| * | ||
| * @param string $configPath | ||
| * @param int|string|null $store | ||
| * @return string | ||
| */ | ||
| private function getConfig(string $configPath, $store = null): string | ||
| { | ||
| return (string)$this->scopeConfig->getValue( | ||
| $configPath, | ||
| ScopeInterface::SCOPE_STORE, | ||
| $store | ||
| ); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| <?php declare(strict_types=1); | ||
|
|
||
| namespace MageOS\ThemeOptimization\ViewModel; | ||
|
|
||
| use Magento\Customer\Model\Context as CustomerContext; | ||
| use Magento\Framework\App\Config\ScopeConfigInterface; | ||
| use Magento\Framework\App\Http\Context; | ||
| use Magento\Framework\View\Element\Block\ArgumentInterface; | ||
| use Magento\Store\Model\ScopeInterface; | ||
|
|
||
| /** | ||
| * BFCache ViewModel | ||
| * Provides data and configuration for BFCache templates. | ||
| * Handles configuration management and business logic for frontend templates. | ||
| */ | ||
| class BfCache implements ArgumentInterface | ||
| { | ||
| /** @var string */ | ||
| private const XML_PATH_ENABLE_USER_INTERACTION_RELOAD_MINICART = | ||
| 'system/bfcache/general/enable_user_interaction_reload_minicart'; | ||
|
|
||
| /** @var string */ | ||
| private const XML_PATH_AUTO_CLOSE_MENU_MOBILE = | ||
| 'system/bfcache/general/auto_close_menu_mobile'; | ||
|
|
||
| /** | ||
| * @param ScopeConfigInterface $scopeConfig | ||
| * @param Context $httpContext | ||
| */ | ||
| public function __construct( | ||
| private ScopeConfigInterface $scopeConfig, | ||
| private Context $httpContext | ||
| ) { | ||
| } | ||
|
|
||
| /** | ||
| * Check if mini cart should reload on user interaction | ||
| * | ||
| * @return bool | ||
| */ | ||
| public function isReloadMiniCartOnInteraction(): bool | ||
| { | ||
| return $this->scopeConfig->isSetFlag( | ||
| self::XML_PATH_ENABLE_USER_INTERACTION_RELOAD_MINICART, | ||
| ScopeInterface::SCOPE_STORE | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Check if mobile menu should auto-close | ||
| * | ||
| * @return bool | ||
| */ | ||
| public function autoCloseMenuMobile(): bool | ||
| { | ||
| return $this->scopeConfig->isSetFlag( | ||
| self::XML_PATH_AUTO_CLOSE_MENU_MOBILE, | ||
| ScopeInterface::SCOPE_STORE | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Check if customer is logged in | ||
| * | ||
| * @return bool | ||
| */ | ||
| public function isCustomerLoggedIn(): bool | ||
| { | ||
| return (bool) $this->httpContext->getValue(CustomerContext::CONTEXT_AUTH); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| <?xml version="1.0"?> | ||
| <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> | ||
| <type name="Magento\Framework\App\Response\Http"> | ||
| <plugin name="theme_optimization_bfcache_response_header_plugin" type="MageOS\ThemeOptimization\Plugin\Framework\App\Response\Http"/> | ||
| </type> | ||
| </config> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| <?xml version="1.0"?> | ||
| <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> | ||
| <body> | ||
| <referenceBlock name="before.body.end"> | ||
| <block name="theme-optimization.bfcache.main" | ||
| template="MageOS_ThemeOptimization::hyva/bfcache/handler.phtml" | ||
| ifconfig="system/bfcache/general/enable"> | ||
| <arguments> | ||
| <argument name="bfcache_config" xsi:type="object">\MageOS\ThemeOptimization\ViewModel\BfCache</argument> | ||
| </arguments> | ||
| </block> | ||
| </referenceBlock> | ||
| </body> | ||
| </page> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.