forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf(@angular-devkit/build-angular): patch
fetch
to load assets fro…
…m memory This commit refactors the assets SSG asset loading from memory to use a patched version of `fetch` instead of spawning a separate server.
- Loading branch information
1 parent
6fbe520
commit 474768c
Showing
11 changed files
with
169 additions
and
194 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
73 changes: 73 additions & 0 deletions
73
packages/angular_devkit/build_angular/src/utils/server-rendering/fetch-patch.ts
This file contains 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,73 @@ | ||
/** | ||
* @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.io/license | ||
*/ | ||
|
||
import { lookup as lookupMimeType } from 'mrmime'; | ||
import { readFile } from 'node:fs/promises'; | ||
import { extname } from 'node:path'; | ||
import { workerData } from 'node:worker_threads'; | ||
import { Response, fetch } from 'undici'; | ||
|
||
/** | ||
* This is passed as workerData when setting up the worker via the `piscina` package. | ||
*/ | ||
const { assetFiles } = workerData as { | ||
assetFiles: Record</** Destination */ string, /** Source */ string>; | ||
}; | ||
|
||
const assetsCache: Map<string, { headers: undefined | Record<string, string>; content: Buffer }> = | ||
new Map(); | ||
|
||
export function patchFetchToLoadInMemoryAssets(): void { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const global = globalThis as unknown as { fetch: typeof fetch }; | ||
const originalFetch = global.fetch; | ||
const updatedFetch: typeof fetch = async (input, init) => { | ||
let url: URL; | ||
if (input instanceof URL) { | ||
url = input; | ||
} else if (typeof input === 'string') { | ||
url = new URL(input, 'resolve://'); | ||
} else if (typeof input === 'object' && 'url' in input) { | ||
url = new URL(input.url, 'resolve://'); | ||
} else { | ||
return originalFetch(input, init); | ||
} | ||
|
||
const { pathname } = url; | ||
|
||
const cachedAsset = assetsCache.get(pathname); | ||
if (cachedAsset) { | ||
const { content, headers } = cachedAsset; | ||
|
||
return new Response(content, { | ||
headers, | ||
}); | ||
} | ||
|
||
if (assetFiles[pathname]) { | ||
const extension = extname(pathname); | ||
const mimeType = lookupMimeType(extension); | ||
const content = await readFile(assetFiles[pathname]); | ||
const headers = mimeType | ||
? { | ||
'Content-Type': mimeType, | ||
} | ||
: undefined; | ||
|
||
assetsCache.set(pathname, { headers, content }); | ||
|
||
return new Response(content, { | ||
headers, | ||
}); | ||
} | ||
|
||
return originalFetch(input, init); | ||
}; | ||
|
||
global.fetch = updatedFetch; | ||
} |
121 changes: 0 additions & 121 deletions
121
packages/angular_devkit/build_angular/src/utils/server-rendering/prerender-server.ts
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.