feat(@angular/build): enable chunk optimization for server builds#33602
feat(@angular/build): enable chunk optimization for server builds#33602jsaguet wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Code Review
This pull request enables chunk optimization for server builds by removing the restriction on server entry points during the build execution. It also introduces a new behavior test suite to verify chunk optimization with server entry points and updates existing E2E tests to dynamically validate that preload links are generated and resolve correctly. The review feedback suggests improving the robustness of a regular expression in the new test file by removing a strict trailing comma requirement.
9458788 to
87f0e56
Compare
| const link = text.match(homeMatch)?.[1]; | ||
| const preloadRes = await fetch(`http://localhost:${defaultServerPort}/${link}`); | ||
| assert.equal(preloadRes.status, 200); | ||
| // The lazy route chunk for the root path retains its name through optimization. |
There was a problem hiding this comment.
Isn't this check already done above?
There was a problem hiding this comment.
It was covering the name assertion the loop didn't, but reusing runTests makes it redundant. I dropped it.
| for (const pathname of Object.keys(RESPONSE_EXPECTS)) { | ||
| const res = await fetch(`http://localhost:${defaultServerPort}${pathname}`); | ||
| assert.equal(res.status, 200, `Response for '${pathname}' should be 200.`); | ||
| const text = await res.text(); | ||
|
|
||
| const preloadLinks = [...text.matchAll(/<link rel="modulepreload" href="([^"]+)">/g)].map( | ||
| (match) => match[1], | ||
| ); | ||
| assert.ok( | ||
| preloadLinks.length > 0, | ||
| `Optimized response for '${pathname}' should contain at least one modulepreload link.`, | ||
| ); | ||
|
|
||
| for (const link of preloadLinks) { | ||
| const preloadRes = await fetch(`http://localhost:${defaultServerPort}/${link}`); | ||
| assert.equal( | ||
| preloadRes.status, | ||
| 200, | ||
| `Optimized preload link '${link}' for '${pathname}' should resolve.`, | ||
| ); | ||
| } | ||
| } |
There was a problem hiding this comment.
| for (const pathname of Object.keys(RESPONSE_EXPECTS)) { | |
| const res = await fetch(`http://localhost:${defaultServerPort}${pathname}`); | |
| assert.equal(res.status, 200, `Response for '${pathname}' should be 200.`); | |
| const text = await res.text(); | |
| const preloadLinks = [...text.matchAll(/<link rel="modulepreload" href="([^"]+)">/g)].map( | |
| (match) => match[1], | |
| ); | |
| assert.ok( | |
| preloadLinks.length > 0, | |
| `Optimized response for '${pathname}' should contain at least one modulepreload link.`, | |
| ); | |
| for (const link of preloadLinks) { | |
| const preloadRes = await fetch(`http://localhost:${defaultServerPort}/${link}`); | |
| assert.equal( | |
| preloadRes.status, | |
| 200, | |
| `Optimized preload link '${link}' for '${pathname}' should resolve.`, | |
| ); | |
| } | |
| } | |
| await runTests(await spawnServer()); |
There was a problem hiding this comment.
Feel free to amend the runTests method to include the checks for all the preload links.
The chunk optimization pass was disabled when a server entry point was present due to a concern that renamed browser chunks would cause incorrect preloading. The preload metadata (route tree preload arrays and entryPointToBrowserMapping in the server app manifest) is generated after the optimization pass from the optimized metafile, so renamed and merged chunks stay consistent with the emitted files. A behavior test now asserts that the server manifest only references browser chunks that exist after optimization and that every lazy route retains its entry point mapping. The server-routes-preload-links e2e test now verifies that every route of an optimized build emits preload links that resolve successfully, instead of only checking the root route.
87f0e56 to
1fb5105
Compare
The chunk optimization pass was disabled when a server entry point was present due to a concern that renamed browser chunks would cause incorrect preloading. The preload metadata (route tree preload arrays and entryPointToBrowserMapping in the server app manifest) is generated after the optimization pass from the optimized metafile, so renamed and merged chunks stay consistent with the emitted files.
A behavior test now asserts that the server manifest only references browser chunks that exist after optimization and that every lazy route retains its entry point mapping. The server-routes-preload-links e2e test now verifies that every route of an optimized build emits preload links that resolve successfully, instead of only checking the root route.
Fixes #33438
PR Checklist
Please check to confirm your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
Chunk optimization is never used for SSR builds.
What is the new behavior?
Chunk optimization is used for SSR builds
Does this PR introduce a breaking change?
Other information