-
Notifications
You must be signed in to change notification settings - Fork 17
New features, enhancements and refactoring #25
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
Open
itaymesh
wants to merge
23
commits into
m2-boilerplate:master
Choose a base branch
from
studioraz:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
feddb35
delete cache clearing command during generation flow
itaymesh 14a3cfa
skip Magneto Critical CSS flag during CSS file generation flow
itaymesh ab37186
M2CRIT-1: Skip Magento native Critical CSS flow
d10nZ 97ec6a6
restore providers
itaymesh b0ba43c
deprecate caching flushing during generate command
itaymesh 43c3211
move asyncCssLoad to frontend/di.xml
itaymesh 317fd36
add ingore-rule option to critical cli to ignore font-face
itaymesh b993057
M2CRIT-2: add random query string param
d10nZ 0da31e4
add penthouse force include CSS selectors option
itaymesh dfd7805
Merge remote-tracking branch 'origin/feature/skip-critical-css-flag-o…
itaymesh 44713fb
Merge pull request #1 from studioraz/feature/skip-critical-css-flag-o…
itaymesh 0afa7b0
CLI command info output rephrasing
itaymesh 90ba3f5
deprecate DefaultProvider
itaymesh 8688a29
Merge remote-tracking branch 'origin/master'
itaymesh 11edaa7
Merge pull request #2 from studioraz/feature/skip-critical-css-flag-o…
itaymesh 2fa822a
M2CRIT-6: Skip Critical CSS Magento native flow
d10nZ 7e8bb7a
M2CRIT-8: add store-id InputOption for cli command
d10nZ 8673ecc
Merge branch 'feature/skip-critical-css-flag-on-generartion'
d10nZ 0b02dce
inject only generated css code
itaymesh 2289c53
Update composer.json
itaymesh 1f60846
Update composer.json
itaymesh 3652bd0
Update composer.json
itaymesh fcab705
added php 8.x compatibility
MaxMage 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
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,125 @@ | ||
| <?php | ||
|
|
||
| namespace M2Boilerplate\CriticalCss\Plugin; | ||
|
|
||
| use Magento\Framework\App\Config\ScopeConfigInterface; | ||
| use Magento\Framework\App\ResponseInterface; | ||
| use Magento\Framework\HTTP\Header; | ||
| use Magento\Framework\View\Result\Layout; | ||
| use Magento\Store\Model\ScopeInterface; | ||
|
|
||
| class AsyncCssPlugin extends \Magento\Theme\Controller\Result\AsyncCssPlugin | ||
| { | ||
| private ScopeConfigInterface $scopeConfig; | ||
| private Header $httpHeader; | ||
|
|
||
| /** | ||
| * @param ScopeConfigInterface $scopeConfig | ||
| * @param Header $httpHeader | ||
| */ | ||
| public function __construct( | ||
| ScopeConfigInterface $scopeConfig, | ||
| Header $httpHeader | ||
| ) { | ||
| parent::__construct($scopeConfig); | ||
| $this->scopeConfig = $scopeConfig; | ||
| $this->httpHeader = $httpHeader; | ||
| } | ||
|
|
||
| /** | ||
| * @inheritDoc | ||
| */ | ||
| public function afterRenderResult(Layout $subject, Layout $result, ResponseInterface $httpResponse) | ||
| { | ||
| if ($this->canBeProcessed($httpResponse)) { | ||
| return parent::afterRenderResult($subject, $result, $httpResponse); | ||
| } | ||
|
|
||
| return $result; | ||
| } | ||
|
|
||
| /** | ||
| * @return bool | ||
| */ | ||
| private function canBeProcessed(ResponseInterface $httpResponse): bool | ||
| { | ||
| // NOTE: validate Critical Css activity Flag | ||
| if (!$this->isCssCriticalEnabled()) { | ||
| return false; | ||
| } | ||
|
|
||
| // NOTE: validate Request, it MUST NOT be initiated by NPM CRITICAL-CSS | ||
| // check on user-agent value | ||
| if ($this->httpHeader->getHttpUserAgent() === 'got (https://github.com/sindresorhus/got)') { | ||
| return false; | ||
| } | ||
|
|
||
| // NOTE: validate if CriticalCss node includes not-empty content | ||
| $content = (string)$httpResponse->getContent(); | ||
| if ($this->isCriticalCssNodeEmpty($content)) { | ||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * NOTE: | ||
| * @see \Magento\Theme\Controller\Result\AsyncCssPlugin::isCssCriticalEnabled | ||
| * | ||
| * Returns information whether css critical path is enabled | ||
| * | ||
| * @return bool | ||
| */ | ||
| private function isCssCriticalEnabled(): bool | ||
| { | ||
| return $this->scopeConfig->isSetFlag( | ||
| 'dev/css/use_css_critical_path', | ||
| ScopeInterface::SCOPE_STORE | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Validates if STYLE CriticalCss-node exists and is NOT empty | ||
| * | ||
| * @param string $content | ||
| * @return bool | ||
| */ | ||
| private function isCriticalCssNodeEmpty(string $content): bool | ||
| { | ||
| $styles = ''; | ||
| $styleOpen = '<style'; | ||
| $styleClose = '/style>'; | ||
| $styleOpenPos = strpos($content, $styleOpen); | ||
|
|
||
| $headClosePos = strpos($content, '</head>'); | ||
|
|
||
| while ($styleOpenPos !== false) { | ||
| // NOTE: no need to proceed in case lines on HEAD-node have been processed | ||
| if ($styleOpenPos >= $headClosePos) { | ||
| break; | ||
| } | ||
|
|
||
| $styleClosePos = strpos($content, $styleClose, $styleOpenPos); | ||
| $style = substr($content, $styleOpenPos, $styleClosePos - $styleOpenPos + strlen($styleClose)); | ||
|
|
||
| // NOTE: validation of STYLE-node string (tags exactly and node's inner content). | ||
| // in case match - fetch the styles and filter the string | ||
| if (preg_match('@<style.+data-type=["\']criticalCss["\'](.+)?>(.+)</style>@s', $style, $matches)) { | ||
| $styles = str_replace( | ||
| ["\n","\r\n","\r"], | ||
| '', | ||
| trim((string)($matches[2] ?? null))); | ||
| break; | ||
| } | ||
|
|
||
| // NOTE: remove processed style-node from HTML. | ||
| $content = str_replace($style, '', $content); | ||
|
|
||
| // NOTE: style-node was cut out, search for the next one at its former position. | ||
| $styleOpenPos = strpos($content, $styleOpen, $styleOpenPos); | ||
| } | ||
|
|
||
| return empty($styles); | ||
| } | ||
| } |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm pretty sure we only need to flush the full page cache. It would be much better