-
Notifications
You must be signed in to change notification settings - Fork 9.3k
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
Fixes race condition when building merged css/js file during simultaneous requests #21756
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,16 +30,23 @@ class Direct implements \Magento\Framework\View\Asset\MergeStrategyInterface | |
*/ | ||
private $cssUrlResolver; | ||
|
||
/** | ||
* @var \Magento\Framework\Math\Random | ||
*/ | ||
protected $mathRandom; | ||
|
||
/** | ||
* @param \Magento\Framework\Filesystem $filesystem | ||
* @param \Magento\Framework\View\Url\CssResolver $cssUrlResolver | ||
*/ | ||
public function __construct( | ||
\Magento\Framework\Filesystem $filesystem, | ||
\Magento\Framework\View\Url\CssResolver $cssUrlResolver | ||
\Magento\Framework\View\Url\CssResolver $cssUrlResolver, | ||
\Magento\Framework\Math\Random $mathRandom | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Due to Magento backward-compatible guide we can't add required parameters to the constructor. Please look Adding a constructor parameter section to do it in the correct way. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Resolved in latest commit. |
||
) { | ||
$this->filesystem = $filesystem; | ||
$this->cssUrlResolver = $cssUrlResolver; | ||
$this->mathRandom = $mathRandom; | ||
} | ||
|
||
/** | ||
|
@@ -49,17 +56,18 @@ public function merge(array $assetsToMerge, Asset\LocalInterface $resultAsset) | |
{ | ||
$mergedContent = $this->composeMergedContent($assetsToMerge, $resultAsset); | ||
$filePath = $resultAsset->getPath(); | ||
$tmpFilePath = $filePath . $this->mathRandom->getUniqueHash('_'); | ||
$staticDir = $this->filesystem->getDirectoryWrite(DirectoryList::STATIC_VIEW); | ||
$tmpDir = $this->filesystem->getDirectoryWrite(DirectoryList::TMP); | ||
$tmpDir->writeFile($filePath, $mergedContent); | ||
$tmpDir->renameFile($filePath, $filePath, $staticDir); | ||
$tmpDir->writeFile($tmpFilePath, $mergedContent); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like the file always will have a different name. I'm sure that resolves the problem, but the file will not caching and always will regenerate. If I am right that is not pretty cool. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're correct that each temporary file will have a different filename. However, the next line not commented is For example, in my two request example:
Request B does override the Request A file that was built, but it is the same file so it doesn't matter. The important part is that Request A didn't copy over a truncated file as Request A was building it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That last sentence should read: |
||
$tmpDir->renameFile($tmpFilePath, $filePath, $staticDir); | ||
} | ||
|
||
/** | ||
* Merge files together and modify content if needed | ||
* | ||
* @param \Magento\Framework\View\Asset\MergeableInterface[] $assetsToMerge | ||
* @param \Magento\Framework\View\Asset\LocalInterface $resultAsset | ||
* @param \Magento\ Framework\View\Asset\LocalInterface $resultAsset | ||
* @return string | ||
* @throws \Magento\Framework\Exception\LocalizedException | ||
*/ | ||
|
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.
Hi @Ian410. Thanks for collaboration. According to [Magento Technical Guidelines] we can provide new public/protected properties (https://devdocs.magento.com/guides/v2.3/coding-standards/technical-guidelines.html)
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.
Resolved in latest commit.