Skip to content

Commit 6bbec86

Browse files
hayatoitochromium-wpt-export-bot
authored andcommitted
<script type=webbundle>: Resolve a relative URL on the bundle's URL
The explainer PR is: WICG/webpackage#700 One of the gaps between the Subresource Loading with WebBundles and Bundle Preloading proposals is "how to resolve a relative URL of resources". The former is using document's base URL, and the latter is using the bundle's URL. We have to align. At the era of <link>-based API, using a document's base URL might make sense because `resources` are specified in the link element's attribute. Now we use <script>-based APIs, and `resources` is specified in JSON, so we no longer have any strong reason to use a document's base URL. See the spec discussion for details: WICG/webpackage#699 (comment) Bug: 1263807 Change-Id: Ic71f095589daa42db2d4649e2cf77212616b2f25 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3244822 Reviewed-by: Kunihiko Sakamoto <[email protected]> Commit-Queue: Hayato Ito <[email protected]> Cr-Commit-Position: refs/heads/main@{#935333}
1 parent fe8ea31 commit 6bbec86

5 files changed

+207
-29
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<!DOCTYPE html>
2+
<title>Subresource loading using relative URLs in the 'resources'</title>
3+
<link
4+
rel="help"
5+
href="https://github.com/WICG/webpackage/blob/main/explainers/subresource-loading.md"
6+
/>
7+
<script src="/resources/testharness.js"></script>
8+
<script src="/resources/testharnessreport.js"></script>
9+
<script src="../resources/test-helpers.js"></script>
10+
11+
<script>
12+
window.TEST_WEB_BUNDLE_ELEMENT_TYPE = "script";
13+
setup(() => {
14+
assert_true(HTMLScriptElement.supports("webbundle"));
15+
});
16+
</script>
17+
<body>
18+
<script>
19+
let script;
20+
21+
function cleanUp() {
22+
if (script) {
23+
script.remove();
24+
}
25+
}
26+
27+
function createScriptWebBundle(resource) {
28+
return createWebBundleElement(
29+
"../resources/wbn/subresource.wbn",
30+
/*resources=*/ [resource]
31+
);
32+
}
33+
34+
async function assertResourceCanBeFetched() {
35+
const response = await fetch(`../resources/wbn/root.js`);
36+
const text = await response.text();
37+
assert_equals(text, "export * from './submodule.js';\n");
38+
}
39+
40+
async function assertResourceNotFound() {
41+
const response = await fetch(`../resources/wbn/root.js`);
42+
const status = await response.status;
43+
assert_equals(status, 404);
44+
}
45+
46+
promise_test(async (t) => {
47+
t.add_cleanup(cleanUp);
48+
script = createScriptWebBundle("root.js");
49+
document.body.append(script);
50+
await assertResourceCanBeFetched();
51+
}, "A relative URL, 'root.js', should be resolved on the bundle's URL");
52+
53+
promise_test(async (t) => {
54+
t.add_cleanup(cleanUp);
55+
script = createScriptWebBundle("./root.js");
56+
document.body.append(script);
57+
await assertResourceCanBeFetched();
58+
}, "Use './root.js', starting with dot");
59+
60+
promise_test(async (t) => {
61+
t.add_cleanup(cleanUp);
62+
script = createScriptWebBundle("../wbn/root.js");
63+
document.body.append(script);
64+
await assertResourceCanBeFetched();
65+
}, "Use '../wbn/root.js', starting with two dots");
66+
67+
promise_test(async (t) => {
68+
t.add_cleanup(cleanUp);
69+
script = createScriptWebBundle("/web-bundle/resources/wbn/root.js");
70+
document.body.append(script);
71+
await assertResourceCanBeFetched();
72+
}, "Use '/web-bundle/resources/wbn/root.js', starting with slash");
73+
74+
promise_test(async (t) => {
75+
t.add_cleanup(cleanUp);
76+
script = createScriptWebBundle("unrelated-relative-url.js");
77+
document.body.append(script);
78+
await assertResourceNotFound();
79+
}, "A resource should not be found");
80+
</script>
81+
</body>
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<!DOCTYPE html>
2+
<title>
3+
Subresource loading using relative URLs in the 'scopes'
4+
</title>
5+
<link
6+
rel="help"
7+
href="https://github.com/WICG/webpackage/blob/main/explainers/subresource-loading.md"
8+
/>
9+
<script src="/resources/testharness.js"></script>
10+
<script src="/resources/testharnessreport.js"></script>
11+
<script src="../resources/test-helpers.js"></script>
12+
13+
<script>
14+
window.TEST_WEB_BUNDLE_ELEMENT_TYPE = "script";
15+
setup(() => {
16+
assert_true(HTMLScriptElement.supports("webbundle"));
17+
});
18+
</script>
19+
<body>
20+
<script>
21+
let script;
22+
23+
function cleanUp() {
24+
if (script) {
25+
script.remove();
26+
}
27+
}
28+
29+
function createScriptWebBundle(scope) {
30+
return createWebBundleElement(
31+
"../resources/wbn/relative-url.wbn",
32+
/*resources=*/ [],
33+
{ scopes: [scope] }
34+
);
35+
}
36+
37+
async function assertResourceCanBeFetched() {
38+
const response = await fetch("../resources/wbn/relative-url/subdirectory-path.js");
39+
const text = await response.text();
40+
assert_equals(text, "scriptLoaded('subdirectory-path.js');");
41+
}
42+
43+
async function assertResourceNotFound() {
44+
const response = await fetch("../resources/wbn/relative-url/subdirectory-path.js");
45+
const status = await response.status;
46+
assert_equals(status, 404);
47+
}
48+
49+
promise_test(async (t) => {
50+
t.add_cleanup(cleanUp);
51+
script = createScriptWebBundle("relative-url");
52+
document.body.append(script);
53+
await assertResourceCanBeFetched();
54+
}, "A relative URL, 'relative-url', should be resolved on the bundle's URL");
55+
56+
promise_test(async (t) => {
57+
t.add_cleanup(cleanUp);
58+
script = createScriptWebBundle("./relative-url");
59+
document.body.append(script);
60+
await assertResourceCanBeFetched();
61+
}, "Use './relative-url', starting with dot");
62+
63+
promise_test(async (t) => {
64+
t.add_cleanup(cleanUp);
65+
script = createScriptWebBundle("../wbn/relative-url");
66+
document.body.append(script);
67+
await assertResourceCanBeFetched();
68+
}, "Use '../wbn/relative-url', starting with two dots");
69+
70+
promise_test(async (t) => {
71+
t.add_cleanup(cleanUp);
72+
script = createScriptWebBundle("/web-bundle/resources/wbn/relative-url");
73+
document.body.append(script);
74+
await assertResourceCanBeFetched();
75+
}, "Use '/web-bundle/resources/wbn/relative-url', starting with slash");
76+
77+
promise_test(async (t) => {
78+
t.add_cleanup(cleanUp);
79+
script = createScriptWebBundle("unrelated-scope");
80+
document.body.append(script);
81+
await assertResourceNotFound();
82+
}, "A resource should not be found");
83+
</script>
84+
</body>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<!DOCTYPE html>
2+
<title>WebBundle subresource loading with relative URLs for static elements</title>
3+
<link
4+
rel="help"
5+
href="https://github.com/WICG/webpackage/blob/main/explainers/subresource-loading.md"
6+
/>
7+
<script src="/resources/testharness.js"></script>
8+
<script src="/resources/testharnessreport.js"></script>
9+
<body>
10+
<script type="webbundle">
11+
{
12+
"source": "../resources/wbn/static-element.wbn",
13+
"resources": [
14+
"static-element/resources/script.js"
15+
],
16+
"scopes": [
17+
"static-element/scopes"
18+
]
19+
}
20+
</script>
21+
<script src="../resources/wbn/static-element/resources/script.js"></script>
22+
<script src="../resources/wbn/static-element/scopes/script.js"></script>
23+
<script src="../resources/wbn/static-element/out-of-scope/script.js"></script>
24+
<script>
25+
setup(() => {
26+
assert_true(HTMLScriptElement.supports('webbundle'));
27+
});
28+
promise_test(async () => {
29+
assert_equals(resources_script_result, 'loaded from webbundle');
30+
assert_equals(scopes_script_result, 'loaded from webbundle');
31+
assert_equals(out_of_scope_script_result, 'loaded from network');
32+
}, 'Subresources from static elements should be loaded from web bundle.');
33+
</script>
34+
</body>

web-bundle/subresource-loading/script-relative-url.https.tentative.html

Lines changed: 0 additions & 24 deletions
This file was deleted.

web-bundle/subresource-loading/script-reuse-web-bundle-resource.https.tentative.html

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@
1616
});
1717

1818
const wbn_url = "../resources/wbn/subresource.wbn";
19-
const resource1 = "../resources/wbn/root.js";
20-
const resource2 = "../resources/wbn/submodule.js";
19+
const resource1 = "root.js";
20+
const resource2 = "submodule.js";
21+
22+
const resource1_url = `../resources/wbn/${resource1}`;
23+
const resource2_url = `../resources/wbn/${resource2}`;
2124

2225
let script1;
2326
let script2;
@@ -32,18 +35,18 @@
3235
}
3336

3437
async function assertResource1CanBeFetched() {
35-
const response = await fetch(resource1);
38+
const response = await fetch(resource1_url);
3639
const text = await response.text();
3740
assert_equals(text, "export * from './submodule.js';\n");
3841
}
3942

4043
async function assertResource1CanNotBeFetched() {
41-
const response = await fetch(resource1);
44+
const response = await fetch(resource1_url);
4245
assert_equals(response.status, 404);
4346
}
4447

4548
async function assertResource2CanBeFetched() {
46-
const response = await fetch(resource2);
49+
const response = await fetch(resource2_url);
4750
const text = await response.text();
4851
assert_equals(text, "export const result = 'OK';\n");
4952
}

0 commit comments

Comments
 (0)