Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .changeset/link-loading-errors.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"html-include-element": patch
---

When included HTML files are loaded, their subresources (defined with `<link>`
elements) are loaded as well. If one of those `<link>` elements fails to load,
`<html-include>` will now gracefully fail, logging the failed url and
continuing to load the rest of the HTML file.
11 changes: 9 additions & 2 deletions html-include-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ async function linkLoaded(link) {
else if (isLinkAlreadyLoaded(link)) resolve(link.sheet);
else {
link.addEventListener('load', () => resolve(link.sheet), { once: true });
link.addEventListener('error', reject, { once: true });
link.addEventListener('error', () => reject({ link }), { once: true });
}
});
}
Expand Down Expand Up @@ -139,7 +139,14 @@ export class HTMLIncludeElement extends HTMLElement {
// If we're not using shadow DOM, then the consuming root
// is responsible to load its own resources
if (!this.noShadow) {
await Promise.all([...this.shadowRoot.querySelectorAll('link')].map(linkLoaded));
const results = await Promise.allSettled([...this.shadowRoot.querySelectorAll('link')].map(linkLoaded));
for (const result of results) {
if (result.status === 'rejected') {
const { link } = result.reason;
const message = `Could not load ${link.href}`;
console.error(message);
}
}
}

this.dispatchEvent(new Event('load'));
Expand Down
15 changes: 14 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
],
"devDependencies": {
"@esm-bundle/chai": "^4.3.4-fix.0",
"@web/test-runner": "^0.13.27"
"@web/test-runner": "^0.13.27",
"hanbi": "^1.0.1"
}
}
13 changes: 13 additions & 0 deletions test/html-include-element_test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {assert} from '@esm-bundle/chai';
import {stubMethod, restore} from 'hanbi';

import '../html-include-element.js';

Expand Down Expand Up @@ -63,6 +64,18 @@ suite('html-include-element', () => {
});
});

test('gracefully handles link loading errors', async () => {
const stub = stubMethod(console, 'error');
container.innerHTML = `
<html-include src="./test/test-link-errors.html">TEST</html-include>
`;
const include = container.querySelector('html-include');
await new Promise((res) => include.addEventListener('load', res));
assert.isNotNull(include.shadowRoot.querySelector('h1'));
assert.isTrue([...stub.calls].some(call => call.args[0]?.match(/^Could not load/)));
restore();
});

// TODO: tests for mode & changing src attribute

});
4 changes: 4 additions & 0 deletions test/test-link-errors.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<link rel="stylesheet" href="./test/styles.css">
<link rel="stylesheet" href="./test/error.error">
<h1>TEST</h1>