Repository navigation
perf(@angular/build): reuse precomputed hash for in-memory build asset ETags #34641
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
alan-agius4
merged 1 commit into
angular:main
from
alan-agius4:perf/build-assets-middleware-etag
Nov 3, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
162 changes: 162 additions & 0 deletions
162
packages/angular/build/src/tools/vite/middlewares/assets-middleware_spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| /** | ||
| * @license | ||
| * Copyright Google LLC All Rights Reserved. | ||
| * | ||
| * Use of this source code is governed by an MIT-style license that can be | ||
| * found in the LICENSE file at https://angular.dev/license | ||
| */ | ||
|
|
||
| import type { IncomingMessage, ServerResponse } from 'node:http'; | ||
| import type { ResultFile } from '../../../builders/application/results'; | ||
| import { calculateHash, initializeHash } from '../../../utils/hash'; | ||
| import { BuildOutputFileType } from '../../esbuild/bundler-files'; | ||
| import { createBuildAssetsMiddleware } from './assets-middleware'; | ||
|
|
||
| describe('createBuildAssetsMiddleware', () => { | ||
| beforeAll(async () => { | ||
| await initializeHash(); | ||
| }); | ||
|
|
||
| function createMockReqRes( | ||
| url: string, | ||
| headers: Record<string, string> = {}, | ||
| ): { | ||
| req: IncomingMessage; | ||
| res: ServerResponse & { setHeader: jasmine.Spy; end: jasmine.Spy }; | ||
| } { | ||
| const req = { | ||
| url, | ||
| method: 'GET', | ||
| headers, | ||
| } as unknown as IncomingMessage; | ||
|
|
||
| const res = { | ||
| statusCode: 200, | ||
| writableEnded: false, | ||
| setHeader: jasmine.createSpy('setHeader'), | ||
| end: jasmine.createSpy('end'), | ||
| } as unknown as ServerResponse & { setHeader: jasmine.Spy; end: jasmine.Spy }; | ||
|
|
||
| return { req, res }; | ||
| } | ||
|
|
||
| it('uses precomputed outputFile.hash for memory files instead of recalculating hash', () => { | ||
| const contents = new TextEncoder().encode('body { color: red; }'); | ||
| const precomputedHash = 'precomputed-memory-hash'; | ||
| const buildResultFiles = new Map<string, ResultFile>([ | ||
| [ | ||
| 'styles.css', | ||
| { | ||
| origin: 'memory', | ||
| path: 'styles.css', | ||
| contents, | ||
| hash: precomputedHash, | ||
| type: BuildOutputFileType.Browser, | ||
| }, | ||
| ], | ||
| ]); | ||
|
|
||
| const readHandler = jasmine.createSpy('readHandler'); | ||
| const middleware = createBuildAssetsMiddleware('/', buildResultFiles, readHandler); | ||
| const { req, res } = createMockReqRes('/styles.css'); | ||
| const next = jasmine.createSpy('next'); | ||
|
|
||
| middleware(req, res, next); | ||
|
|
||
| expect(next).not.toHaveBeenCalled(); | ||
| expect(readHandler).not.toHaveBeenCalled(); | ||
| expect(res.setHeader).toHaveBeenCalledWith('Content-Type', 'text/css'); | ||
| expect(res.setHeader).toHaveBeenCalledWith('ETag', `W/"${precomputedHash}"`); | ||
| expect(res.setHeader).toHaveBeenCalledWith('Cache-Control', 'no-cache'); | ||
| expect(res.end).toHaveBeenCalledWith(contents); | ||
| }); | ||
|
|
||
| it('returns 304 for memory files when If-None-Match matches precomputed outputFile.hash', () => { | ||
| const contents = new TextEncoder().encode('body { color: red; }'); | ||
| const precomputedHash = 'precomputed-memory-hash'; | ||
| const buildResultFiles = new Map<string, ResultFile>([ | ||
| [ | ||
| 'styles.css', | ||
| { | ||
| origin: 'memory', | ||
| path: 'styles.css', | ||
| contents, | ||
| hash: precomputedHash, | ||
| type: BuildOutputFileType.Browser, | ||
| }, | ||
| ], | ||
| ]); | ||
|
|
||
| const middleware = createBuildAssetsMiddleware('/', buildResultFiles); | ||
| const { req, res } = createMockReqRes('/styles.css', { | ||
| 'if-none-match': `W/"${precomputedHash}"`, | ||
| }); | ||
| const next = jasmine.createSpy('next'); | ||
|
|
||
| middleware(req, res, next); | ||
|
|
||
| expect(next).not.toHaveBeenCalled(); | ||
| expect(res.statusCode).toBe(304); | ||
| expect(res.end).toHaveBeenCalledWith(); | ||
| }); | ||
|
|
||
| it('calculates hash from disk contents when outputFile.origin is disk', () => { | ||
| const diskBuffer = Buffer.from('console.log("asset");', 'utf-8'); | ||
| const expectedHash = calculateHash(diskBuffer); | ||
| const buildResultFiles = new Map<string, ResultFile>([ | ||
| [ | ||
| 'assets/data.json', | ||
| { | ||
| origin: 'disk', | ||
| path: 'assets/data.json', | ||
| source: '/workspace/src/assets/data.json', | ||
| type: BuildOutputFileType.Browser, | ||
| }, | ||
| ], | ||
| ]); | ||
|
|
||
| const readHandler = jasmine.createSpy('readHandler').and.returnValue(diskBuffer); | ||
| const middleware = createBuildAssetsMiddleware('/', buildResultFiles, readHandler); | ||
| const { req, res } = createMockReqRes('/assets/data.json'); | ||
| const next = jasmine.createSpy('next'); | ||
|
|
||
| middleware(req, res, next); | ||
|
|
||
| expect(next).not.toHaveBeenCalled(); | ||
| expect(readHandler).toHaveBeenCalledOnceWith('/workspace/src/assets/data.json'); | ||
| expect(res.setHeader).toHaveBeenCalledWith('Content-Type', 'application/json'); | ||
| expect(res.setHeader).toHaveBeenCalledWith('ETag', `W/"${expectedHash}"`); | ||
| expect(res.setHeader).toHaveBeenCalledWith('Cache-Control', 'no-cache'); | ||
| expect(res.end).toHaveBeenCalledWith(diskBuffer); | ||
| }); | ||
|
|
||
| it('returns 304 for disk files when If-None-Match matches calculated hash', () => { | ||
| const diskBuffer = Buffer.from('{"ok":true}', 'utf-8'); | ||
| const expectedHash = calculateHash(diskBuffer); | ||
| const buildResultFiles = new Map<string, ResultFile>([ | ||
| [ | ||
| 'assets/data.json', | ||
| { | ||
| origin: 'disk', | ||
| path: 'assets/data.json', | ||
| source: '/workspace/src/assets/data.json', | ||
| type: BuildOutputFileType.Browser, | ||
| }, | ||
| ], | ||
| ]); | ||
|
|
||
| const readHandler = jasmine.createSpy('readHandler').and.returnValue(diskBuffer); | ||
| const middleware = createBuildAssetsMiddleware('/', buildResultFiles, readHandler); | ||
| const { req, res } = createMockReqRes('/assets/data.json', { | ||
| 'if-none-match': `W/"${expectedHash}"`, | ||
| }); | ||
| const next = jasmine.createSpy('next'); | ||
|
|
||
| middleware(req, res, next); | ||
|
|
||
| expect(next).not.toHaveBeenCalled(); | ||
| expect(readHandler).toHaveBeenCalledOnceWith('/workspace/src/assets/data.json'); | ||
| expect(res.statusCode).toBe(304); | ||
| expect(res.end).toHaveBeenCalledWith(); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.