Skip to content

Commit aed49dd

Browse files
clydinkirjs
authored andcommittedJan 9, 2025·
fix(compiler): use chunk origin in template HMR request URL (#59459)
The URL that is dynamically imported to fetch a potential component update for HMR is now based on the value of `import.meta.url`. This ensures that the request is sent to the same location that was used to retrieve the application code. For some development server setups the HTML base HREF may not be the location of the Angular development server. By using the application code location which was generated by the development server, HMR requests can continue to work as expected in these scenarios. In most common cases, this change will not have any effect as the HTML base HREF aligns with the location of the application code files. PR Close #59459
1 parent 4d9bfab commit aed49dd

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed
 

‎packages/compiler-cli/test/ngtsc/hmr_spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ runInEachFileSystem(() => {
106106
expect(jsContents).toContain(`import * as i0 from "@angular/core";`);
107107
expect(jsContents).toContain('function Cmp_HmrLoad(t) {');
108108
expect(jsContents).toContain(
109-
'import(/* @vite-ignore */\n"/@ng/component?c=test.ts%40Cmp&t=" + encodeURIComponent(t))',
109+
'import(/* @vite-ignore */\nnew URL("/@ng/component?c=test.ts%40Cmp&t=" + encodeURIComponent(t), import.meta.url).href)',
110110
);
111111
expect(jsContents).toContain(
112112
').then(m => m.default && i0.ɵɵreplaceMetadata(Cmp, m.default, [i0], ' +
@@ -173,7 +173,7 @@ runInEachFileSystem(() => {
173173
expect(jsContents).toContain(`import * as i1 from "./dep";`);
174174
expect(jsContents).toContain('function Cmp_HmrLoad(t) {');
175175
expect(jsContents).toContain(
176-
'import(/* @vite-ignore */\n"/@ng/component?c=test.ts%40Cmp&t=" + encodeURIComponent(t))',
176+
'import(/* @vite-ignore */\nnew URL("/@ng/component?c=test.ts%40Cmp&t=" + encodeURIComponent(t), import.meta.url).href)',
177177
);
178178
expect(jsContents).toContain(
179179
').then(m => m.default && i0.ɵɵreplaceMetadata(Cmp, m.default, [i0, i1], ' +

‎packages/compiler/src/render3/r3_hmr_compiler.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,14 @@ export function compileHmrInitializer(meta: R3HmrMetadata): o.Expression {
8080
.literal(urlPartial)
8181
.plus(o.variable('encodeURIComponent').callFn([o.variable(timestampName)]));
8282

83+
// import.meta.url
84+
const urlBase = o.variable('import').prop('meta').prop('url');
85+
86+
// new URL(urlValue, urlBase).href
87+
const urlHref = new o.InstantiateExpr(o.variable('URL'), [urlValue, urlBase]).prop('href');
88+
8389
// function Cmp_HmrLoad(t) {
84-
// import(/* @vite-ignore */ url).then((m) => m.default && replaceMetadata(...));
90+
// import(/* @vite-ignore */ urlHref).then((m) => m.default && replaceMetadata(...));
8591
// }
8692
const importCallback = new o.DeclareFunctionStmt(
8793
importCallbackName,
@@ -90,7 +96,7 @@ export function compileHmrInitializer(meta: R3HmrMetadata): o.Expression {
9096
// The vite-ignore special comment is required to prevent Vite from generating a superfluous
9197
// warning for each usage within the development code. If Vite provides a method to
9298
// programmatically avoid this warning in the future, this added comment can be removed here.
93-
new o.DynamicImportExpr(urlValue, null, '@vite-ignore')
99+
new o.DynamicImportExpr(urlHref, null, '@vite-ignore')
94100
.prop('then')
95101
.callFn([replaceCallback])
96102
.toStmt(),

0 commit comments

Comments
 (0)
Please sign in to comment.