1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
|
<!DOCTYPE html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/common/get-host-info.sub.js"></script>
<body>
<script>
host_info = get_host_info();
function verifyNumberOfDownloads(url, number, allowTransferSizeOfZero = false) {
var numDownloads = 0;
let absoluteURL = new URL(url, location.href).href;
performance.getEntriesByName(absoluteURL).forEach(entry => {
if (entry.transferSize > 0 || allowTransferSizeOfZero) {
numDownloads++;
}
});
assert_equals(numDownloads, number, url);
}
function attachAndWaitForLoad(element) {
return new Promise((resolve, reject) => {
element.onload = resolve;
element.onerror = reject;
document.body.appendChild(element);
});
}
function attachAndWaitForError(element) {
return new Promise((resolve, reject) => {
element.onload = reject;
element.onerror = resolve;
document.body.appendChild(element);
});
}
function attachAndWaitForTimeout(element, t) {
return new Promise((resolve, reject) => {
element.onload = reject;
element.onerror = reject;
t.step_timeout(resolve, 1000);
document.body.appendChild(element);
});
}
promise_test(function(t) {
var link = document.createElement('link');
link.rel = 'modulepreload';
link.href = 'resources/dummy.js?unique';
return attachAndWaitForLoad(link).then(() => {
verifyNumberOfDownloads('resources/dummy.js?unique', 1);
// Verify that <script> doesn't fetch the module again.
var script = document.createElement('script');
script.type = 'module';
script.src = 'resources/dummy.js?unique';
return attachAndWaitForLoad(script);
}).then(() => {
verifyNumberOfDownloads('resources/dummy.js?unique', 1);
});
}, 'link rel=modulepreload');
/**
* Begin tests to ensure crossorigin value behaves the same on
* link rel=modulepreload as it does script elements.
*/
promise_test(function(t) {
document.cookie = 'same=1';
var link = document.createElement('link');
link.rel = 'modulepreload';
link.crossOrigin = 'anonymous';
link.href = 'resources/dummy.js?sameOriginAnonymous';
return attachAndWaitForLoad(link).then(() => {
verifyNumberOfDownloads('resources/dummy.js?sameOriginAnonymous', 1);
// Verify that <script> doesn't fetch the module again.
var script = document.createElement('script');
script.type = 'module';
script.crossOrigin = 'anonymous';
script.src = 'resources/dummy.js?sameOriginAnonymous';
return attachAndWaitForLoad(script);
}).then(() => {
verifyNumberOfDownloads('resources/dummy.js?sameOriginAnonymous', 1);
});
}, 'same-origin link rel=modulepreload crossorigin=anonymous');
promise_test(function(t) {
var link = document.createElement('link');
link.rel = 'modulepreload';
link.crossOrigin = 'use-credentials';
link.href = 'resources/dummy.js?sameOriginUseCredentials';
return attachAndWaitForLoad(link).then(() => {
verifyNumberOfDownloads('resources/dummy.js?sameOriginUseCredentials', 1);
// Verify that <script> doesn't fetch the module again.
var script = document.createElement('script');
script.type = 'module';
script.crossOrigin = 'use-credentials';
script.src = 'resources/dummy.js?sameOriginUseCredentials';
return attachAndWaitForLoad(script);
}).then(() => {
verifyNumberOfDownloads('resources/dummy.js?sameOriginUseCredentials', 1);
});
}, 'same-origin link rel=modulepreload crossorigin=use-credentials');
promise_test(function(t) {
const setCookiePromise = fetch(
`${host_info.HTTP_REMOTE_ORIGIN}/cookies/resources/set-cookie.py?name=cross&path=/preload/`,
{
mode: 'no-cors',
credentials: 'include',
});
return setCookiePromise.then(() => {
var link = document.createElement('link');
link.rel = 'modulepreload';
link.href = `${host_info.HTTP_REMOTE_ORIGIN}/preload/resources/cross-origin-module.py?crossOriginNone`;
return attachAndWaitForLoad(link);
}).then(() => {
verifyNumberOfDownloads(`${host_info.HTTP_REMOTE_ORIGIN}/preload/resources/cross-origin-module.py?crossOriginNone`, 1, true);
// Verify that <script> doesn't fetch the module again.
var script = document.createElement('script');
script.type = 'module';
script.src = `${host_info.HTTP_REMOTE_ORIGIN}/preload/resources/cross-origin-module.py?crossOriginNone`;
return attachAndWaitForLoad(script);
}).then(() => {
verifyNumberOfDownloads(`${host_info.HTTP_REMOTE_ORIGIN}/preload/resources/cross-origin-module.py?crossOriginNone`, 1, true);
});
}, 'cross-origin link rel=modulepreload');
promise_test(function(t) {
var link = document.createElement('link');
link.rel = 'modulepreload';
link.crossOrigin = 'anonymous';
link.href = `${host_info.HTTP_REMOTE_ORIGIN}/preload/resources/cross-origin-module.py?crossOriginAnonymous`;
return attachAndWaitForLoad(link).then(() => {
verifyNumberOfDownloads(`${host_info.HTTP_REMOTE_ORIGIN}/preload/resources/cross-origin-module.py?crossOriginAnonymous`, 1, true);
// Verify that <script> doesn't fetch the module again.
var script = document.createElement('script');
script.type = 'module';
script.crossOrigin = 'anonymous';
script.src = `${host_info.HTTP_REMOTE_ORIGIN}/preload/resources/cross-origin-module.py?crossOriginAnonymous`;
return attachAndWaitForLoad(script);
}).then(() => {
verifyNumberOfDownloads(`${host_info.HTTP_REMOTE_ORIGIN}/preload/resources/cross-origin-module.py?crossOriginAnonymous`, 1, true);
});
}, 'cross-origin link rel=modulepreload crossorigin=anonymous');
promise_test(function(t) {
var link = document.createElement('link');
link.rel = 'modulepreload';
link.crossOrigin = 'use-credentials';
link.href = `${host_info.HTTP_REMOTE_ORIGIN}/preload/resources/cross-origin-module.py?crossOriginUseCredentials`;
return attachAndWaitForLoad(link).then(() => {
verifyNumberOfDownloads(`${host_info.HTTP_REMOTE_ORIGIN}/preload/resources/cross-origin-module.py?crossOriginUseCredentials`, 1, true);
// Verify that <script> doesn't fetch the module again.
var script = document.createElement('script');
script.type = 'module';
script.crossOrigin = 'use-credentials';
script.src = `${host_info.HTTP_REMOTE_ORIGIN}/preload/resources/cross-origin-module.py?crossOriginUseCredentials`;
return attachAndWaitForLoad(script);
}).then(() => {
verifyNumberOfDownloads(`${host_info.HTTP_REMOTE_ORIGIN}/preload/resources/cross-origin-module.py?crossOriginUseCredentials`, 1, true);
});
}, 'cross-origin link rel=modulepreload crossorigin=use-credentials');
/**
* End link rel=modulepreload crossorigin attribute tests.
*/
promise_test(function(t) {
var link = document.createElement('link');
link.rel = 'modulepreload';
link.href = 'resources/module1.js?submodule';
return attachAndWaitForLoad(link).then(() => {
verifyNumberOfDownloads('resources/module1.js?submodule', 1);
// The load event fires before (optional) submodules fetch.
verifyNumberOfDownloads('resources/module2.js', 0);
var script = document.createElement('script');
script.type = 'module';
script.src = 'resources/module1.js?submodule';
return attachAndWaitForLoad(script);
}).then(() => {
verifyNumberOfDownloads('resources/module1.js?submodule', 1);
verifyNumberOfDownloads('resources/module2.js', 1);
});
}, 'link rel=modulepreload with submodules');
promise_test(function(t) {
var link = document.createElement('link');
link.rel = 'modulepreload';
link.href = 'resources/syntax-error.js';
return attachAndWaitForLoad(link);
}, 'link rel=modulepreload for a module with syntax error');
promise_test(function(t) {
var link = document.createElement('link');
link.rel = 'modulepreload';
link.href = 'resources/not-exist.js';
return attachAndWaitForError(link);
}, 'link rel=modulepreload for a module with network error');
promise_test(function(t) {
var link = document.createElement('link');
link.rel = 'modulepreload';
link.href = null;
return attachAndWaitForError(link);
}, 'link rel=modulepreload with bad href attribute');
promise_test(function(t) {
var link = document.createElement('link');
link.rel = 'modulepreload';
link.href = 'resources/module1.js?as-script';
link.as = 'script'
return attachAndWaitForLoad(link);
}, 'link rel=modulepreload as=script');
promise_test(function(t) {
var link = document.createElement('link');
link.rel = 'modulepreload';
link.href = 'resources/module1.js?as-image';
link.as = 'image'
return attachAndWaitForError(link);
}, 'link rel=modulepreload with non-script-like as= value (image)');
promise_test(function(t) {
var link = document.createElement('link');
link.rel = 'modulepreload';
link.href = 'resources/module1.js?as-xslt';
link.as = 'xslt'
return attachAndWaitForError(link);
}, 'link rel=modulepreload with non-script-like as= value (xslt)');
promise_test(function(t) {
var link = document.createElement('link');
link.rel = 'modulepreload';
link.href = 'resources/module1.js?integrity-match';
link.integrity = 'sha256-+Ks3iNIiTq2ujlWhvB056cmXobrCFpU9hd60xZ1WCaA='
return attachAndWaitForLoad(link);
}, 'link rel=modulepreload with integrity match');
promise_test(function(t) {
var link = document.createElement('link');
link.rel = 'modulepreload';
link.href = 'resources/module1.mjs?integrity-match';
link.integrity = 'sha256-+Ks3iNIiTq2ujlWhvB056cmXobrCFpU9hd60xZ1WCaA='
return attachAndWaitForLoad(link);
}, 'link rel=modulepreload with integrity match2');
promise_test(function(t) {
var link = document.createElement('link');
link.rel = 'modulepreload';
link.href = 'resources/module1.js?integrity-doesnotmatch';
link.integrity = 'sha384-doesnotmatch'
return attachAndWaitForError(link);
}, 'link rel=modulepreload with integrity mismatch');
promise_test(function(t) {
var link = document.createElement('link');
link.rel = 'modulepreload';
link.href = 'resources/module1.mjs?integrity-doesnotmatch';
link.integrity = 'sha256-dOxReWMnMSPfUvxEbBqIrjNh8ZN8n05j7h3JmhF8gQc='
return attachAndWaitForError(link);
}, 'link rel=modulepreload with integrity mismatch2');
promise_test(function(t) {
var link = document.createElement('link');
link.rel = 'modulepreload';
link.href = 'resources/module1.mjs?integrity-invalid';
link.integrity = 'sha256-dOxReWMnMSPfUvxEbBqIrjNh8ZN8n05j7h3JmhF8gQc=%'
return attachAndWaitForError(link);
}, 'link rel=modulepreload with integrity mismatch3');
promise_test(function(t) {
var link1 = document.createElement('link');
var link2 = document.createElement('link');
link1.rel = 'modulepreload';
link2.rel = 'modulepreload';
link1.href = 'resources/module1.js?same-url';
link2.href = 'resources/module1.js?same-url';
return Promise.all([
attachAndWaitForLoad(link1),
attachAndWaitForLoad(link2),
]);
}, 'multiple link rel=modulepreload with same href');
promise_test(function(t) {
var link1 = document.createElement('link');
var link2 = document.createElement('link');
link1.rel = 'modulepreload';
link2.rel = 'modulepreload';
link1.href = 'resources/module2.js?child-before';
link2.href = 'resources/module1.js?child-before';
return attachAndWaitForLoad(link1)
.then(() => attachAndWaitForLoad(link2))
.then(() => new Promise(r => t.step_timeout(r, 1000)))
.then(() => {
verifyNumberOfDownloads('resources/module2.js?child-before', 1);
});
}, 'multiple link rel=modulepreload with child module before parent');
promise_test(function(t) {
var link = document.createElement('link');
link.rel = 'modulepreload';
link.href = 'resources/module1.mjs?matching-media';
link.media = 'all';
return attachAndWaitForLoad(link);
}, 'link rel=modulepreload with matching media');
promise_test(function(t) {
var link = document.createElement('link');
link.rel = 'modulepreload';
link.href = 'resources/module1.mjs?non-matching-media';
link.media = 'not all';
return attachAndWaitForTimeout(link, t);
}, 'link rel=modulepreload with non-matching media');
promise_test(function(t) {
var link = document.createElement('link');
link.rel = 'modulepreload';
link.href = 'resources/module1.mjs?empty-media';
link.media = '';
return attachAndWaitForLoad(link);
}, 'link rel=modulepreload with empty media');
promise_test(function(t) {
var link = document.createElement('link');
link.rel = 'modulepreload';
link.href = '';
return attachAndWaitForTimeout(link, t);
}, 'link rel=modulepreload with empty href');
promise_test(function(t) {
var link = document.createElement('link');
link.rel = 'modulepreload';
link.href = '';
link.as = 'fetch';
return attachAndWaitForTimeout(link, t);
}, 'link rel=modulepreload with empty href and invalid as= value');
promise_test(function(t) {
var link = document.createElement('link');
var script = document.createElement('script');
link.rel = 'modulepreload';
script.type = 'module';
link.href = 'resources/module1.mjs?non-matching-crossorigin';
script.src = link.href;
script.crossOrigin = 'anonymous';
document.body.append(link);
return attachAndWaitForLoad(script);
}, 'link rel=modulepreload and script with non-matching crossorigin values');
promise_test(function(t) {
var link = document.createElement('link');
var script = document.createElement('script');
link.rel = 'modulepreload';
script.type = 'module';
link.href = 'resources/module1.mjs?non-matching-crossorigin';
script.src = link.href;
link.crossOrigin = 'anonymous';
script.crossOrigin = 'use-credentials';
document.body.append(link);
return attachAndWaitForLoad(script);
}, 'link rel=modulepreload and script with non-matching crossorigin values2');
promise_test(function(t) {
var link = document.createElement('link');
var moduleScript = document.createElement('script');
var classicScript = document.createElement('script');
link.rel = 'modulepreload';
moduleScript.type = 'module';
link.href = 'resources/dummy.js?non-module script';
classicScript.src = link.href;
moduleScript.src = link.href;
document.body.append(link);
document.body.append(classicScript);
return attachAndWaitForLoad(moduleScript);
}, 'link rel=modulepreload and non-module script');
</script>
</body>
|