Repository navigation
fix(@angular/ssr): do not render requests sent while rendering on the… #34790
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
Closed
SkyZeroZx
wants to merge
1
commit into
angular:main
from
SkyZeroZx:fix/ssr-requests-sent-while-rendering
Closed
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
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
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,65 @@ | ||
| /** | ||
| * @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 { PlatformLocation } from '@angular/common'; | ||
| import type { HttpInterceptorFn } from '@angular/common/http'; | ||
| import { inject } from '@angular/core'; | ||
|
|
||
| /** | ||
| * The header added to the HTTP requests that an application sends to its own host while it is | ||
| * rendered on the server. The server does not render requests that carry it. | ||
| */ | ||
| export const SSR_REQUEST_HEADER = 'x-angular-ssr-request'; | ||
|
|
||
| /** | ||
| * Adds the {@link SSR_REQUEST_HEADER} header to the requests sent to the host of the page being | ||
| * rendered. Such a request could otherwise be rendered as a page that sends the same request again, | ||
| * without bound. | ||
| * | ||
| * Host names are compared rather than origins: behind a proxy that terminates TLS, the page is | ||
| * rendered for `http:` while the application may call its public `https:` URL. | ||
| * | ||
| * This interceptor must run after the `@angular/platform-server` interceptor that resolves relative | ||
| * URLs. Requests that are not sent through the `HttpClient` interceptors are not marked. | ||
| */ | ||
| export const ssrRequestInterceptorFn: HttpInterceptorFn = (request, next) => { | ||
| const { href, hostname } = inject(PlatformLocation); | ||
| if (!URL.canParse(href)) { | ||
| // The URL of the page is unknown, so requests sent to its host cannot be recognized. | ||
| return next(request); | ||
| } | ||
|
|
||
| // URLs that cannot be parsed are marked too: some HTTP clients still send a request for them. | ||
| if ( | ||
| URL.canParse(request.urlWithParams, href) && | ||
| new URL(request.urlWithParams, href).hostname !== hostname | ||
| ) { | ||
| return next(request); | ||
| } | ||
|
|
||
| return next(request.clone({ setHeaders: { [SSR_REQUEST_HEADER]: '1' } })); | ||
| }; | ||
|
|
||
| /** | ||
| * Creates the response to a request that the application sent to its own host while it was | ||
| * rendered on the server. The request is not rendered. The response is only meaningful to the | ||
| * render that sent the request, so it does not include the headers configured for the route and | ||
| * must not be stored. | ||
| * | ||
| * @returns A `Response` object with a 400 status code. | ||
| */ | ||
| export function createSsrRequestResponse(): Response { | ||
| return new Response('Requests sent while rendering on the server are not rendered.', { | ||
| status: 400, | ||
| statusText: 'Bad Request', | ||
| headers: { | ||
| 'Content-Type': 'text/plain', | ||
| 'Cache-Control': 'no-store', | ||
| }, | ||
| }); | ||
| } | ||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Efficiency & Compatibility Improvements in
ssrRequestInterceptorFnThere are a few areas in
ssrRequestInterceptorFnthat can be optimized for better performance, robustness, and compatibility:URL.canParse(request.urlWithParams, href)is called first, followed bynew URL(request.urlWithParams, href). This parses the URL twice for every outgoing HTTP request during SSR. We can optimize this by using a singletry-catchblock withnew URL()to parse only once.URL.canParseis a relatively new API. While supported in modern Node.js versions, usingnew URLinside atry-catchblock is 100% backward-compatible and avoids any potential runtime compatibility issues in older or custom environments.inject(PlatformLocation)can throw an error ifPlatformLocationis not provided (e.g., in certain test environments or minimal custom platforms). Making it optional viainject(PlatformLocation, { optional: true })makes the interceptor much more robust.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
URL.canParseis already used in this package (utils/validation.tsruns it on every request) and is available in every supported Node version.PlatformLocationisprovidedIn: 'platform'with a factory, and platform-server always providesServerPlatformLocation, soinject()can't throw here.The extra parse happens once per outgoing
HttpClientrequest during SSR, so I'd rather keep it consistent with the rest of the package.