Skip to content

Commit 1721d83

Browse files
committed
MAGETWO-59003: Js file version regenerated in browser on every page reload in develop mode
1 parent bc9df94 commit 1721d83

File tree

6 files changed

+48
-128
lines changed

6 files changed

+48
-128
lines changed

app/code/Magento/Customer/view/frontend/web/template/authentication-popup.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@
7878
<!-- ko foreach: getRegion('additional-login-form-fields') -->
7979
<!-- ko template: getTemplate() --><!-- /ko -->
8080
<!-- /ko -->
81-
</div>
8281
<div class="actions-toolbar">
8382
<input name="context" type="hidden" value="checkout" />
8483
<div class="primary">

dev/tools/grunt/configs/clean.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ _.each(themes, function(theme, name) {
2121
"<%= path.tmp %>/cache/**/*",
2222
"<%= combo.autopath(\""+name+"\", path.pub ) %>**/*",
2323
"<%= combo.autopath(\""+name+"\", path.tmpLess) %>**/*",
24-
"<%= combo.autopath(\""+name+"\", path.tmpSource) %>**/*"
24+
"<%= combo.autopath(\""+name+"\", path.tmpSource) %>**/*",
25+
"<%= path.deployedVersion %>"
2526
]
2627
}
2728
]
@@ -56,7 +57,8 @@ var cleanOptions = {
5657
"dot": true,
5758
"src": [
5859
"<%= path.pub %>frontend/**/*",
59-
"<%= path.pub %>adminhtml/**/*"
60+
"<%= path.pub %>adminhtml/**/*",
61+
"<%= path.deployedVersion %>"
6062
]
6163
}
6264
]
@@ -73,7 +75,8 @@ var cleanOptions = {
7375
"<%= path.pub %>frontend/**/*.less",
7476
"<%= path.pub %>frontend/**/*.css",
7577
"<%= path.pub %>adminhtml/**/*.less",
76-
"<%= path.pub %>adminhtml/**/*.css"
78+
"<%= path.pub %>adminhtml/**/*.css",
79+
"<%= path.deployedVersion %>"
7780
]
7881
}
7982
]
@@ -102,7 +105,8 @@ var cleanOptions = {
102105
"src": [
103106
"<%= path.pub %>**/*.js",
104107
"<%= path.pub %>**/*.html",
105-
"<%= path.pub %>_requirejs/**/*"
108+
"<%= path.pub %>_requirejs/**/*",
109+
"<%= path.deployedVersion %>"
106110
]
107111
}
108112
]

dev/tools/grunt/configs/path.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ module.exports = {
1313
tmpLess: 'var/view_preprocessed/less/',
1414
tmpSource: 'var/view_preprocessed/source/',
1515
tmp: 'var',
16+
deployedVersion: 'pub/static/deployed_version.txt',
1617
css: {
1718
setup: 'setup/pub/styles',
1819
updater: '../magento2-updater/pub/css'

lib/internal/Magento/Framework/App/Test/Unit/View/Deployment/VersionTest.php

Lines changed: 28 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,6 @@ protected function setUp()
4545
$objectManager->setBackwardCompatibleProperty($this->object, 'logger', $this->loggerMock);
4646
}
4747

48-
public function testGetValueDeveloperMode()
49-
{
50-
$this->appStateMock
51-
->expects($this->once())
52-
->method('getMode')
53-
->will($this->returnValue(\Magento\Framework\App\State::MODE_DEVELOPER));
54-
$this->versionStorageMock->expects($this->never())->method($this->anything());
55-
$this->assertInternalType('integer', $this->object->getValue());
56-
$this->object->getValue(); // Ensure computation occurs only once and result is cached in memory
57-
}
58-
5948
/**
6049
* @param string $appMode
6150
* @dataProvider getValueFromStorageDataProvider
@@ -81,106 +70,45 @@ public function getValueFromStorageDataProvider()
8170
];
8271
}
8372

84-
/**
85-
* $param bool $isUnexpectedValueExceptionThrown
86-
* $param bool $isFileSystemExceptionThrown
87-
* @dataProvider getValueDefaultModeDataProvider
88-
*/
89-
public function testGetValueDefaultMode(
90-
$isUnexpectedValueExceptionThrown,
91-
$isFileSystemExceptionThrown = null
92-
) {
93-
$versionType = 'integer';
94-
$this->appStateMock
95-
->expects($this->once())
96-
->method('getMode')
97-
->willReturn(\Magento\Framework\App\State::MODE_DEFAULT);
98-
if ($isUnexpectedValueExceptionThrown) {
99-
$storageException = new \UnexpectedValueException('Does not exist in the storage');
100-
$this->versionStorageMock
101-
->expects($this->once())
102-
->method('load')
103-
->will($this->throwException($storageException));
104-
$this->versionStorageMock->expects($this->once())
105-
->method('save')
106-
->with($this->isType($versionType));
107-
if ($isFileSystemExceptionThrown) {
108-
$fileSystemException = new FileSystemException(
109-
new \Magento\Framework\Phrase('Can not load static content version')
110-
);
111-
$this->versionStorageMock
112-
->expects($this->once())
113-
->method('save')
114-
->will($this->throwException($fileSystemException));
115-
$this->loggerMock->expects($this->once())
116-
->method('critical')
117-
->with('Can not save static content version.');
118-
} else {
119-
$this->loggerMock->expects($this->never())
120-
->method('critical');
121-
}
122-
} else {
123-
$this->versionStorageMock
124-
->expects($this->once())
125-
->method('load')
126-
->willReturn(1475779229);
127-
$this->loggerMock->expects($this->never())
128-
->method('critical');
129-
}
130-
$this->assertInternalType($versionType, $this->object->getValue());
73+
public function testGetValueInNonProductionMode() {
74+
$version = 123123123123;
75+
$this->versionStorageMock->expects($this->once())
76+
->method('load')
77+
->willReturn($version);
78+
79+
$this->assertEquals($version, $this->object->getValue());
13180
$this->object->getValue();
13281
}
13382

13483
/**
135-
* @return array
84+
* @expectedException \UnexpectedValueException
13685
*/
137-
public function getValueDefaultModeDataProvider()
86+
public function testGetValueWithProductionModeAndException()
13887
{
139-
return [
140-
[false],
141-
[true, false],
142-
[true, true]
143-
];
144-
}
145-
146-
/**
147-
* @param bool $isUnexpectedValueExceptionThrown
148-
* @dataProvider getValueProductionModeDataProvider
149-
*/
150-
public function testGetValueProductionMode(
151-
$isUnexpectedValueExceptionThrown
152-
) {
153-
$this->appStateMock
154-
->expects($this->once())
88+
$this->versionStorageMock->expects($this->once())
89+
->method('load')
90+
->willReturn(false);
91+
$this->appStateMock->expects($this->once())
15592
->method('getMode')
15693
->willReturn(\Magento\Framework\App\State::MODE_PRODUCTION);
157-
if ($isUnexpectedValueExceptionThrown) {
158-
$storageException = new \UnexpectedValueException('Does not exist in the storage');
159-
$this->versionStorageMock
160-
->expects($this->once())
161-
->method('load')
162-
->will($this->throwException($storageException));
163-
$this->loggerMock->expects($this->once())
164-
->method('critical')
165-
->with('Can not load static content version.');
166-
} else {
167-
$this->versionStorageMock
168-
->expects($this->once())
169-
->method('load')
170-
->willReturn(1475779229);
171-
}
172-
$this->assertInternalType('integer', $this->object->getValue());
94+
$this->loggerMock->expects($this->once())
95+
->method('critical')
96+
->with('Can not load static content version.');
97+
17398
$this->object->getValue();
17499
}
175100

176-
/**
177-
* @return array
178-
*/
179-
public function getValueProductionModeDataProvider()
101+
public function testGetValueWithProductionMode()
180102
{
181-
return [
182-
[false],
183-
[true],
184-
];
103+
$this->versionStorageMock->expects($this->once())
104+
->method('load')
105+
->willReturn(false);
106+
$this->appStateMock->expects($this->once())
107+
->method('getMode')
108+
->willReturn(\Magento\Framework\App\State::MODE_DEFAULT);
109+
$this->versionStorageMock->expects($this->once())
110+
->method('save');
111+
112+
$this->assertNotNull($this->object->getValue());
185113
}
186114
}

lib/internal/Magento/Framework/App/View/Deployment/Version.php

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -67,23 +67,16 @@ public function getValue()
6767
*/
6868
protected function readValue($appMode)
6969
{
70-
if ($appMode == \Magento\Framework\App\State::MODE_DEVELOPER) {
71-
$result = $this->generateVersion();
72-
} else {
73-
try {
74-
$result = $this->versionStorage->load();
75-
} catch (\UnexpectedValueException $e) {
76-
$result = $this->generateVersion();
77-
if ($appMode == \Magento\Framework\App\State::MODE_DEFAULT) {
78-
try {
79-
$this->versionStorage->save($result);
80-
} catch (FileSystemException $e) {
81-
$this->getLogger()->critical('Can not save static content version.');
82-
}
83-
} else {
84-
$this->getLogger()->critical('Can not load static content version.');
85-
}
70+
$result = $this->versionStorage->load();
71+
if (!$result) {
72+
if ($appMode == \Magento\Framework\App\State::MODE_PRODUCTION) {
73+
$this->getLogger()->critical('Can not load static content version.');
74+
throw new \UnexpectedValueException(
75+
__('Unable to retrieve deployment version of static files from the file system.')
76+
);
8677
}
78+
$result = $this->generateVersion();
79+
$this->versionStorage->save($result);
8780
}
8881
return $result;
8982
}

lib/internal/Magento/Framework/App/View/Deployment/Version/Storage/File.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,10 @@ public function __construct(
4040
*/
4141
public function load()
4242
{
43-
try {
43+
if ($this->directory->isReadable($this->fileName)) {
4444
return $this->directory->readFile($this->fileName);
45-
} catch (\Magento\Framework\Exception\FileSystemException $e) {
46-
throw new \UnexpectedValueException(
47-
'Unable to retrieve deployment version of static files from the file system.',
48-
0,
49-
$e
50-
);
5145
}
46+
return false;
5247
}
5348

5449
/**

0 commit comments

Comments
 (0)