Skip to content

Commit

Permalink
fix(@angular/build): handle empty module case to avoid TypeError
Browse files Browse the repository at this point in the history
Ensure that accessing the module after invalidation doesn't result in an empty module, which causes a `TypeError: ɵgetOrCreateAngularServerApp is not a function`.

Closes angular#29458
  • Loading branch information
alan-agius4 committed Jan 23, 2025
1 parent 96490cd commit 8e884a0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
27 changes: 11 additions & 16 deletions packages/angular/build/src/builders/dev-server/vite-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -501,35 +501,30 @@ async function invalidateUpdatedFiles(
}

// Invalidate any updated files
let destroyAngularServerAppCalled = false;
let serverApplicationChanged = false;
for (const [file, record] of generatedFiles) {
if (!record.updated) {
continue;
}
record.updated = false;

if (record.type === BuildOutputFileType.ServerApplication && !destroyAngularServerAppCalled) {
// Clear the server app cache
// This must be done before module invalidation.
const { ɵdestroyAngularServerApp } = (await server.ssrLoadModule('/main.server.mjs')) as {
ɵdestroyAngularServerApp: typeof destroyAngularServerApp;
};

ɵdestroyAngularServerApp();
destroyAngularServerAppCalled = true;
}

record.updated = false;
updatedFiles.push(file);
serverApplicationChanged ||= record.type === BuildOutputFileType.ServerApplication;

const updatedModules = server.moduleGraph.getModulesByFile(
normalizePath(join(server.config.root, file)),
);
updatedModules?.forEach((m) => server.moduleGraph.invalidateModule(m));
}

if (destroyAngularServerAppCalled) {
// Trigger module evaluation before reload to initiate dependency optimization.
await server.ssrLoadModule('/main.server.mjs');
if (serverApplicationChanged) {
// Clear the server app cache and
// trigger module evaluation before reload to initiate dependency optimization.
const { ɵdestroyAngularServerApp } = (await server.ssrLoadModule('/main.server.mjs')) as {
ɵdestroyAngularServerApp: typeof destroyAngularServerApp;
};

ɵdestroyAngularServerApp();
}

return updatedFiles;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ export function createAngularSsrInternalMiddleware(
const { writeResponseToNodeResponse, createWebRequestFromNodeRequest } =
await loadEsmModule<typeof import('@angular/ssr/node')>('@angular/ssr/node');

// The following is necessary because accessing the module after invalidation may result in an empty module,
// which can trigger a `TypeError: ɵgetOrCreateAngularServerApp is not a function` error.
// TODO: look into why.
await server.ssrLoadModule('/main.server.mjs');

const { ɵgetOrCreateAngularServerApp } = (await server.ssrLoadModule('/main.server.mjs')) as {
ɵgetOrCreateAngularServerApp: typeof getOrCreateAngularServerApp;
};
Expand Down

0 comments on commit 8e884a0

Please sign in to comment.