-
Notifications
You must be signed in to change notification settings - Fork 12k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(@angular/build): utilize
ssr.entry
during prerendering to enab…
…le access to local API routes The `ssr.entry` (server.ts file) is now utilized during prerendering, allowing access to locally defined API routes for improved data fetching and rendering.
- Loading branch information
1 parent
3a3be8b
commit f630726
Showing
9 changed files
with
283 additions
and
29 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
64 changes: 64 additions & 0 deletions
64
packages/angular/build/src/utils/server-rendering/launch-server.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,64 @@ | ||
/** | ||
* @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 assert from 'node:assert'; | ||
import { createServer } from 'node:http'; | ||
import { loadEsmModule } from '../load-esm'; | ||
import { loadEsmModuleFromMemory } from './load-esm-from-memory'; | ||
import { isSsrNodeRequestHandler, isSsrRequestHandler } from './utils'; | ||
|
||
export const DEFAULT_URL = new URL('http://ng-localhost/'); | ||
|
||
/** | ||
* Launches a server that handles local requests. | ||
* | ||
* @returns A promise that resolves to the URL of the running server. | ||
*/ | ||
export async function launchServer(): Promise<URL> { | ||
const { default: handler } = await loadEsmModuleFromMemory('./server.mjs'); | ||
const { createWebRequestFromNodeRequest, writeResponseToNodeResponse } = | ||
await loadEsmModule<typeof import('@angular/ssr/node')>('@angular/ssr/node'); | ||
|
||
if (!isSsrNodeRequestHandler(handler) && !isSsrRequestHandler(handler)) { | ||
return DEFAULT_URL; | ||
} | ||
|
||
const server = createServer((req, res) => { | ||
(async () => { | ||
// handle request | ||
if (isSsrNodeRequestHandler(handler)) { | ||
await handler(req, res, (e) => { | ||
throw e; | ||
}); | ||
} else { | ||
const webRes = await handler(createWebRequestFromNodeRequest(req)); | ||
if (webRes) { | ||
await writeResponseToNodeResponse(webRes, res); | ||
} else { | ||
res.statusCode = 501; | ||
res.end('Not Implemented.'); | ||
} | ||
} | ||
})().catch((e) => { | ||
res.statusCode = 500; | ||
res.end('Internal Server Error.'); | ||
// eslint-disable-next-line no-console | ||
console.error(e); | ||
}); | ||
}); | ||
|
||
server.unref(); | ||
|
||
await new Promise<void>((resolve) => server.listen(0, 'localhost', resolve)); | ||
|
||
const serverAddress = server.address(); | ||
assert(serverAddress, 'Server address should be defined.'); | ||
assert(typeof serverAddress !== 'string', 'Server address should not be a string.'); | ||
|
||
return new URL(`http://localhost:${serverAddress.port}/`); | ||
} |
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
21 changes: 21 additions & 0 deletions
21
packages/angular/build/src/utils/server-rendering/utils.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,21 @@ | ||
/** | ||
* @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 { createRequestHandler } from '@angular/ssr'; | ||
import type { createNodeRequestHandler } from '@angular/ssr/node'; | ||
|
||
export function isSsrNodeRequestHandler( | ||
value: unknown, | ||
): value is ReturnType<typeof createNodeRequestHandler> { | ||
return typeof value === 'function' && '__ng_node_request_handler__' in value; | ||
} | ||
export function isSsrRequestHandler( | ||
value: unknown, | ||
): value is ReturnType<typeof createRequestHandler> { | ||
return typeof value === 'function' && '__ng_request_handler__' in value; | ||
} |
Oops, something went wrong.