Skip to content

Commit

Permalink
fix(@angular/build): invalidate component template updates with dev-s…
Browse files Browse the repository at this point in the history
…erver SSR

To ensure that the Vite-based dev-server SSR uses updated component template update
modules, the server module graph is invalidated when component updates are sent.
This currently does a full invalidation but in the future this could potentially be
optimized to only update the relevant modules. A fix is also present to correct the
component update identifier usage to prevent lookup misses.
  • Loading branch information
clydin authored and dgp1130 committed Dec 17, 2024
1 parent 19bb2d4 commit 3b7e6a8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
16 changes: 16 additions & 0 deletions packages/angular/build/src/builders/dev-server/vite-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,12 @@ export async function* serveWithVite(
assetFiles.set('/' + normalizePath(outputPath), normalizePath(file.inputPath));
}
}

// Invalidate SSR module graph to ensure that only new rebuild is used and not stale component updates
if (server && browserOptions.ssr && templateUpdates.size > 0) {
server.moduleGraph.invalidateAll();
}

// Clear stale template updates on code rebuilds
templateUpdates.clear();

Expand All @@ -256,6 +262,16 @@ export async function* serveWithVite(
'Builder must provide an initial full build before component update results.',
);

// Invalidate SSR module graph to ensure that new component updates are used
// TODO: Use fine-grained invalidation of only the component update modules
if (browserOptions.ssr) {
server.moduleGraph.invalidateAll();
const { ɵresetCompiledComponents } = (await server.ssrLoadModule('/main.server.mjs')) as {
ɵresetCompiledComponents: () => void;
};
ɵresetCompiledComponents();
}

for (const componentUpdate of result.updates) {
if (componentUpdate.type === 'template') {
templateUpdates.set(componentUpdate.id, componentUpdate.content);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,9 @@ export function createServerMainCodeBundleOptions(
ɵgetOrCreateAngularServerApp,
} from '@angular/ssr';`,

// Need for HMR
`export { ɵresetCompiledComponents } from '@angular/core';`,

// Re-export all symbols including default export from 'main.server.ts'
`export { default } from '${mainServerEntryPointJsImport}';`,
`export * from '${mainServerEntryPointJsImport}';`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export async function createAngularMemoryPlugin(
const requestUrl = new URL(id.slice(1), 'http://localhost');
const componentId = requestUrl.searchParams.get('c');

return (componentId && options.templateUpdates?.get(componentId)) ?? '';
return (componentId && options.templateUpdates?.get(encodeURIComponent(componentId))) ?? '';
}

const [file] = id.split('?', 1);
Expand Down

0 comments on commit 3b7e6a8

Please sign in to comment.