Skip to content
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

<script type=webbundle>: Resolve a relative URL on the bundle's URL #31395

Merged
merged 1 commit into from Oct 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,81 @@
<!DOCTYPE html>
<title>Subresource loading using relative URLs in the 'resources'</title>
<link
rel="help"
href="https://github.com/WICG/webpackage/blob/main/explainers/subresource-loading.md"
/>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="../resources/test-helpers.js"></script>

<script>
window.TEST_WEB_BUNDLE_ELEMENT_TYPE = "script";
setup(() => {
assert_true(HTMLScriptElement.supports("webbundle"));
});
</script>
<body>
<script>
let script;

function cleanUp() {
if (script) {
script.remove();
}
}

function createScriptWebBundle(resource) {
return createWebBundleElement(
"../resources/wbn/subresource.wbn",
/*resources=*/ [resource]
);
}

async function assertResourceCanBeFetched() {
const response = await fetch(`../resources/wbn/root.js`);
const text = await response.text();
assert_equals(text, "export * from './submodule.js';\n");
}

async function assertResourceNotFound() {
const response = await fetch(`../resources/wbn/root.js`);
const status = await response.status;
assert_equals(status, 404);
}

promise_test(async (t) => {
t.add_cleanup(cleanUp);
script = createScriptWebBundle("root.js");
document.body.append(script);
await assertResourceCanBeFetched();
}, "A relative URL, 'root.js', should be resolved on the bundle's URL");

promise_test(async (t) => {
t.add_cleanup(cleanUp);
script = createScriptWebBundle("./root.js");
document.body.append(script);
await assertResourceCanBeFetched();
}, "Use './root.js', starting with dot");

promise_test(async (t) => {
t.add_cleanup(cleanUp);
script = createScriptWebBundle("../wbn/root.js");
document.body.append(script);
await assertResourceCanBeFetched();
}, "Use '../wbn/root.js', starting with two dots");

promise_test(async (t) => {
t.add_cleanup(cleanUp);
script = createScriptWebBundle("/web-bundle/resources/wbn/root.js");
document.body.append(script);
await assertResourceCanBeFetched();
}, "Use '/web-bundle/resources/wbn/root.js', starting with slash");

promise_test(async (t) => {
t.add_cleanup(cleanUp);
script = createScriptWebBundle("unrelated-relative-url.js");
document.body.append(script);
await assertResourceNotFound();
}, "A resource should not be found");
</script>
</body>
@@ -0,0 +1,84 @@
<!DOCTYPE html>
<title>
Subresource loading using relative URLs in the 'scopes'
</title>
<link
rel="help"
href="https://github.com/WICG/webpackage/blob/main/explainers/subresource-loading.md"
/>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="../resources/test-helpers.js"></script>

<script>
window.TEST_WEB_BUNDLE_ELEMENT_TYPE = "script";
setup(() => {
assert_true(HTMLScriptElement.supports("webbundle"));
});
</script>
<body>
<script>
let script;

function cleanUp() {
if (script) {
script.remove();
}
}

function createScriptWebBundle(scope) {
return createWebBundleElement(
"../resources/wbn/relative-url.wbn",
/*resources=*/ [],
{ scopes: [scope] }
);
}

async function assertResourceCanBeFetched() {
const response = await fetch("../resources/wbn/relative-url/subdirectory-path.js");
const text = await response.text();
assert_equals(text, "scriptLoaded('subdirectory-path.js');");
}

async function assertResourceNotFound() {
const response = await fetch("../resources/wbn/relative-url/subdirectory-path.js");
const status = await response.status;
assert_equals(status, 404);
}

promise_test(async (t) => {
t.add_cleanup(cleanUp);
script = createScriptWebBundle("relative-url");
document.body.append(script);
await assertResourceCanBeFetched();
}, "A relative URL, 'relative-url', should be resolved on the bundle's URL");

promise_test(async (t) => {
t.add_cleanup(cleanUp);
script = createScriptWebBundle("./relative-url");
document.body.append(script);
await assertResourceCanBeFetched();
}, "Use './relative-url', starting with dot");

promise_test(async (t) => {
t.add_cleanup(cleanUp);
script = createScriptWebBundle("../wbn/relative-url");
document.body.append(script);
await assertResourceCanBeFetched();
}, "Use '../wbn/relative-url', starting with two dots");

promise_test(async (t) => {
t.add_cleanup(cleanUp);
script = createScriptWebBundle("/web-bundle/resources/wbn/relative-url");
document.body.append(script);
await assertResourceCanBeFetched();
}, "Use '/web-bundle/resources/wbn/relative-url', starting with slash");

promise_test(async (t) => {
t.add_cleanup(cleanUp);
script = createScriptWebBundle("unrelated-scope");
document.body.append(script);
await assertResourceNotFound();
}, "A resource should not be found");
</script>
</body>
@@ -0,0 +1,34 @@
<!DOCTYPE html>
<title>WebBundle subresource loading with relative URLs for static elements</title>
<link
rel="help"
href="https://github.com/WICG/webpackage/blob/main/explainers/subresource-loading.md"
/>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<body>
<script type="webbundle">
{
"source": "../resources/wbn/static-element.wbn",
"resources": [
"static-element/resources/script.js"
],
"scopes": [
"static-element/scopes"
]
}
</script>
<script src="../resources/wbn/static-element/resources/script.js"></script>
<script src="../resources/wbn/static-element/scopes/script.js"></script>
<script src="../resources/wbn/static-element/out-of-scope/script.js"></script>
<script>
setup(() => {
assert_true(HTMLScriptElement.supports('webbundle'));
});
promise_test(async () => {
assert_equals(resources_script_result, 'loaded from webbundle');
assert_equals(scopes_script_result, 'loaded from webbundle');
assert_equals(out_of_scope_script_result, 'loaded from network');
}, 'Subresources from static elements should be loaded from web bundle.');
</script>
</body>

This file was deleted.

Expand Up @@ -16,8 +16,11 @@
});

const wbn_url = "../resources/wbn/subresource.wbn";
const resource1 = "../resources/wbn/root.js";
const resource2 = "../resources/wbn/submodule.js";
const resource1 = "root.js";
const resource2 = "submodule.js";

const resource1_url = `../resources/wbn/${resource1}`;
const resource2_url = `../resources/wbn/${resource2}`;

let script1;
let script2;
Expand All @@ -32,18 +35,18 @@
}

async function assertResource1CanBeFetched() {
const response = await fetch(resource1);
const response = await fetch(resource1_url);
const text = await response.text();
assert_equals(text, "export * from './submodule.js';\n");
}

async function assertResource1CanNotBeFetched() {
const response = await fetch(resource1);
const response = await fetch(resource1_url);
assert_equals(response.status, 404);
}

async function assertResource2CanBeFetched() {
const response = await fetch(resource2);
const response = await fetch(resource2_url);
const text = await response.text();
assert_equals(text, "export const result = 'OK';\n");
}
Expand Down