Skip to content

Commit c9e6b13

Browse files
ENGCOM-7299: Load storage polyfill conditionally #27619
- Merge Pull Request #27619 from krzksz/magento2:performance-storage-polyfill - Merged commits: 1. cba9d46 2. f81f12e 3. e1a98e2 4. f58a127 5. e8b245e 6. fbbd5ae
2 parents 7f2272d + fbbd5ae commit c9e6b13

File tree

3 files changed

+50
-28
lines changed

3 files changed

+50
-28
lines changed

app/code/Magento/Theme/view/base/requirejs-config.js

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@
44
*/
55

66
var config = {
7-
'waitSeconds': 0,
8-
'map': {
7+
waitSeconds: 0,
8+
map: {
99
'*': {
1010
'ko': 'knockoutjs/knockout',
1111
'knockout': 'knockoutjs/knockout',
1212
'mageUtils': 'mage/utils/main',
1313
'rjsResolver': 'mage/requirejs/resolver'
1414
}
1515
},
16-
'shim': {
16+
shim: {
1717
'jquery/jquery-migrate': ['jquery'],
1818
'jquery/jstree/jquery.hotkeys': ['jquery'],
1919
'jquery/hover-intent': ['jquery'],
@@ -28,7 +28,7 @@ var config = {
2828
},
2929
'magnifier/magnifier': ['jquery']
3030
},
31-
'paths': {
31+
paths: {
3232
'jquery/validate': 'jquery/jquery.validate',
3333
'jquery/hover-intent': 'jquery/jquery.hoverIntent',
3434
'jquery/file-uploader': 'jquery/fileUploader/jquery.fileupload-fp',
@@ -40,26 +40,47 @@ var config = {
4040
'tinycolor': 'jquery/spectrum/tinycolor',
4141
'jquery-ui-modules': 'jquery/ui-modules'
4242
},
43-
'deps': [
43+
deps: [
4444
'jquery/jquery-migrate'
4545
],
46-
'config': {
47-
'mixins': {
46+
config: {
47+
mixins: {
4848
'jquery/jstree/jquery.jstree': {
4949
'mage/backend/jstree-mixin': true
5050
},
5151
'jquery': {
5252
'jquery/patches/jquery': true
5353
}
5454
},
55-
'text': {
55+
text: {
5656
'headers': {
5757
'X-Requested-With': 'XMLHttpRequest'
5858
}
5959
}
6060
}
6161
};
6262

63+
/* eslint-disable max-depth */
64+
/**
65+
* Adds polyfills only for browser contexts which prevents bundlers from including them.
66+
*/
67+
if (typeof window !== 'undefined' && window.document) {
68+
/**
69+
* Polyfill localStorage and sessionStorage for browsers that do not support them.
70+
*/
71+
try {
72+
if (!window.localStorage || !window.sessionStorage) {
73+
throw new Error();
74+
}
75+
76+
localStorage.setItem('storage_test', 1);
77+
localStorage.removeItem('storage_test');
78+
} catch (e) {
79+
config.deps.push('mage/polyfill');
80+
}
81+
}
82+
/* eslint-enable max-depth */
83+
6384
require(['jquery'], function ($) {
6485
'use strict';
6586

app/code/Magento/Theme/view/frontend/layout/default_head_blocks.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
<meta name="viewport" content="width=device-width, initial-scale=1"/>
1111
<css src="mage/calendar.css"/>
1212
<script src="requirejs/require.js"/>
13-
<script src="mage/polyfill.js"/>
1413
</head>
1514
<body>
1615
<referenceBlock name="head.additional">

lib/web/mage/polyfill.js

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
1-
try {
2-
if (!window.localStorage || !window.sessionStorage) {
3-
throw new Error();
4-
}
1+
(function (root, doc) {
2+
'use strict';
3+
4+
var Storage;
55

6-
localStorage.setItem('storage_test', 1);
7-
localStorage.removeItem('storage_test');
8-
} catch (e) {
9-
(function () {
10-
'use strict';
6+
try {
7+
if (!root.localStorage || !root.sessionStorage) {
8+
throw new Error();
9+
}
1110

11+
localStorage.setItem('storage_test', 1);
12+
localStorage.removeItem('storage_test');
13+
} catch (e) {
1214
/**
1315
* Returns a storage object to shim local or sessionStorage
1416
* @param {String} type - either 'local' or 'session'
1517
*/
16-
var Storage = function (type) {
18+
Storage = function (type) {
1719
var data;
1820

1921
/**
@@ -32,7 +34,7 @@ try {
3234
} else {
3335
expires = '';
3436
}
35-
document.cookie = name + '=' + value + expires + '; path=/';
37+
doc.cookie = name + '=' + value + expires + '; path=/';
3638
}
3739

3840
/**
@@ -41,7 +43,7 @@ try {
4143
*/
4244
function readCookie(name) {
4345
var nameEQ = name + '=',
44-
ca = document.cookie.split(';'),
46+
ca = doc.cookie.split(';'),
4547
i = 0,
4648
c;
4749

@@ -70,11 +72,11 @@ try {
7072
return 'localstorage';
7173
}
7274

73-
if (!window.name) {
74-
window.name = new Date().getTime();
75+
if (!root.name) {
76+
root.name = new Date().getTime();
7577
}
7678

77-
return 'sessionStorage' + window.name;
79+
return 'sessionStorage' + root.name;
7880
}
7981

8082
/**
@@ -170,7 +172,7 @@ try {
170172
};
171173
};
172174

173-
window.localStorage.prototype = window.localStorage = new Storage('local');
174-
window.sessionStorage.prototype = window.sessionStorage = new Storage('session');
175-
})();
176-
}
175+
root.localStorage.prototype = root.localStorage = new Storage('local');
176+
root.sessionStorage.prototype = root.sessionStorage = new Storage('session');
177+
}
178+
})(window, document);

0 commit comments

Comments
 (0)