Skip to content

Commit 9525eee

Browse files
committed
refactor(@angular/build): allow component update invalidation from client
If HMR is enabled, a component update has the potential to be unsupported at runtime or may cause an exception. While build time analysis attempts to verify that an update is possible, there could be cases that are as of yet unknown. For those cases, the runtime can now signal this information back to the development server which will clear the errant component update and trigger a full page reload. This action will be logged to the development server console along with an optional message from the client.
1 parent 25dbe7c commit 9525eee

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

packages/angular/build/src/builders/dev-server/vite-server.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,41 @@ export async function* serveWithVite(
457457
}
458458
});
459459

460+
// Setup component HMR invalidation
461+
// Invalidation occurs when the runtime cannot update a component
462+
server.hot.on(
463+
'angular:invalidate',
464+
(data: { id: string; message?: string; error?: boolean }) => {
465+
if (typeof data?.id !== 'string') {
466+
context.logger.warn(
467+
'Development server client sent invalid internal invalidate event.',
468+
);
469+
}
470+
471+
// Clear invalid template update
472+
templateUpdates.delete(data.id);
473+
474+
// Some cases are expected unsupported update scenarios but some may be errors.
475+
// If an error occurred, log the error in addition to the invalidation.
476+
if (data.error) {
477+
context.logger.error(
478+
`Component update failed${data.message ? `: ${data.message}` : '.'}` +
479+
'\nPlease consider reporting the error at https://github.com/angular/angular-cli/issues',
480+
);
481+
} else {
482+
context.logger.warn(
483+
`Component update unsupported${data.message ? `: ${data.message}` : '.'}`,
484+
);
485+
}
486+
487+
server?.ws.send({
488+
type: 'full-reload',
489+
path: '*',
490+
});
491+
context.logger.info('Page reload sent to client(s).');
492+
},
493+
);
494+
460495
const urls = server.resolvedUrls;
461496
if (urls && (urls.local.length || urls.network.length)) {
462497
serverUrl = new URL(urls.local[0] ?? urls.network[0]);

0 commit comments

Comments
 (0)