Skip to content

Commit 9e1bd67

Browse files
fix: [#1851] Moves URL resolution to after checking if module preloading is enabled to prevent URL errors to be thrown when unresolvable (#1851)
Currently, when using relative paths in `<link rel="modulepreload" href="/dist/..." />`, we will get a stable error from `new URL`. I moved the URL creation below, similar to `preloadResource`. In addition, there is no actual point in creating a `URL` if the `disableJavaScriptFileLoading` or `disableJavaScriptEvaluation` options are enabled. Co-authored-by: David Ortner <david@ortner.se>
1 parent 620fb2f commit 9e1bd67

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

packages/happy-dom/src/nodes/html-link-element/HTMLLinkElement.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,6 @@ export default class HTMLLinkElement extends HTMLElement {
295295
* @param url URL.
296296
*/
297297
async #preloadModule(url: string): Promise<void> {
298-
const absoluteURL = new URL(url, this[PropertySymbol.ownerDocument].location.href);
299298
const window = this[PropertySymbol.window];
300299
const browserFrame = new WindowBrowserContext(window).getBrowserFrame();
301300
const browserSettings = new WindowBrowserContext(window).getSettings();
@@ -310,6 +309,8 @@ export default class HTMLLinkElement extends HTMLElement {
310309
return;
311310
}
312311

312+
const absoluteURL = new URL(url, this[PropertySymbol.ownerDocument].location.href);
313+
313314
if (
314315
browserSettings.disableErrorCapturing ||
315316
browserSettings.errorCapture !== BrowserErrorCaptureEnum.tryAndCatch

packages/happy-dom/test/nodes/html-link-element/HTMLLinkElement.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,32 @@ describe('HTMLLinkElement', () => {
354354
expect((<Event>(<unknown>loadEvent)).type).toBe('load');
355355
});
356356

357+
it('Skips preloading when "rel" is set to "modulepreload" and the Happy DOM setting "disableJavaScriptFileLoading" is set to "true"', async () => {
358+
window = new Window({
359+
settings: { disableJavaScriptFileLoading: true }
360+
});
361+
document = window.document;
362+
const link = document.createElement('link');
363+
364+
expect(() => {
365+
link.rel = 'modulepreload';
366+
link.href = '/base/js/TestModuleElement.js';
367+
}).not.toThrow();
368+
});
369+
370+
it('Skips preloading when "rel" is set to "modulepreload" and the Happy DOM setting "disableJavaScriptEvaluation" is set to "true"', async () => {
371+
window = new Window({
372+
settings: { disableJavaScriptEvaluation: true }
373+
});
374+
document = window.document;
375+
const link = document.createElement('link');
376+
377+
expect(() => {
378+
link.rel = 'modulepreload';
379+
link.href = '/base/js/TestModuleElement.js';
380+
}).not.toThrow();
381+
});
382+
357383
it('Preloads modules when "rel" is set to "modulepreload" and only fetches once when preload is ongoing', async () => {
358384
const requests: string[] = [];
359385
const window = new Window({

0 commit comments

Comments
 (0)