Skip to content

Commit b3c6c7e

Browse files
committed
fix(@angular/ssr): include Content-Language header when locale is set
The server now includes the `Content-Language` HTTP header in responses whenever a locale is explicitly set. (cherry picked from commit d7214e9)
1 parent 9ae8351 commit b3c6c7e

File tree

3 files changed

+25
-11
lines changed

3 files changed

+25
-11
lines changed

Diff for: packages/angular/ssr/src/app.ts

+15-9
Original file line numberDiff line numberDiff line change
@@ -210,11 +210,16 @@ export class AngularServerApp {
210210
}
211211

212212
const assetPath = this.buildServerAssetPathFromRequest(request);
213-
if (!this.assets.hasServerAsset(assetPath)) {
213+
const {
214+
manifest: { locale },
215+
assets,
216+
} = this;
217+
218+
if (!assets.hasServerAsset(assetPath)) {
214219
return null;
215220
}
216221

217-
const { text, hash, size } = this.assets.getServerAsset(assetPath);
222+
const { text, hash, size } = assets.getServerAsset(assetPath);
218223
const etag = `"${hash}"`;
219224

220225
return request.headers.get('if-none-match') === etag
@@ -224,6 +229,7 @@ export class AngularServerApp {
224229
'Content-Length': size.toString(),
225230
'ETag': etag,
226231
'Content-Type': 'text/html;charset=UTF-8',
232+
...(locale !== undefined ? { 'Content-Language': locale } : {}),
227233
...headers,
228234
},
229235
});
@@ -254,11 +260,17 @@ export class AngularServerApp {
254260
const url = new URL(request.url);
255261
const platformProviders: StaticProvider[] = [];
256262

263+
const {
264+
manifest: { bootstrap, inlineCriticalCss, locale },
265+
assets,
266+
} = this;
267+
257268
// Initialize the response with status and headers if available.
258269
const responseInit = {
259270
status,
260271
headers: new Headers({
261272
'Content-Type': 'text/html;charset=UTF-8',
273+
...(locale !== undefined ? { 'Content-Language': locale } : {}),
262274
...headers,
263275
}),
264276
};
@@ -281,18 +293,12 @@ export class AngularServerApp {
281293
);
282294
} else if (renderMode === RenderMode.Client) {
283295
// Serve the client-side rendered version if the route is configured for CSR.
284-
let html = await this.assets.getServerAsset('index.csr.html').text();
296+
let html = await assets.getServerAsset('index.csr.html').text();
285297
html = await this.runTransformsOnHtml(html, url);
286298

287299
return new Response(html, responseInit);
288300
}
289301

290-
const {
291-
manifest: { bootstrap, inlineCriticalCss, locale },
292-
hooks,
293-
assets,
294-
} = this;
295-
296302
if (locale !== undefined) {
297303
platformProviders.push({
298304
provide: LOCALE_ID,

Diff for: packages/angular/ssr/test/app-engine_spec.ts

+3
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ function createEntryPoint(locale: string) {
6060
`,
6161
},
6262
},
63+
locale,
6364
);
6465

6566
return {
@@ -110,12 +111,14 @@ describe('AngularAppEngine', () => {
110111
const request = new Request('https://example.com/it/ssr/index.html');
111112
const response = await appEngine.handle(request);
112113
expect(await response?.text()).toContain('SSR works IT');
114+
expect(response?.headers?.get('Content-Language')).toBe('it');
113115
});
114116

115117
it('should return a serve prerendered page with correct locale', async () => {
116118
const request = new Request('https://example.com/it/ssg');
117119
const response = await appEngine.handle(request);
118120
expect(await response?.text()).toContain('SSG works IT');
121+
expect(response?.headers?.get('Content-Language')).toBe('it');
119122
});
120123

121124
it('should correctly serve the prerendered content when the URL ends with "index.html" with correct locale', async () => {

Diff for: packages/angular/ssr/test/testing-utils.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,18 @@ import { ServerRoute, provideServerRoutesConfig } from '../src/routes/route-conf
2121
* Angular components and providers for testing purposes.
2222
*
2323
* @param routes - An array of route definitions to be used by the Angular Router.
24-
* @param serverRoutes - An array of ServerRoute definitions to be used for server-side rendering.
25-
* @param [baseHref='/'] - An optional base href to be used in the HTML template.
24+
* @param serverRoutes - An array of server route definitions for server-side rendering.
25+
* @param [baseHref='/'] - An optional base href for the HTML template (default is `/`).
26+
* @param additionalServerAssets - A record of additional server assets to include,
27+
* where the keys are asset paths and the values are asset details.
28+
* @param locale - An optional locale to configure for the application during testing.
2629
*/
2730
export function setAngularAppTestingManifest(
2831
routes: Routes,
2932
serverRoutes: ServerRoute[],
3033
baseHref = '/',
3134
additionalServerAssets: Record<string, ServerAsset> = {},
35+
locale?: string,
3236
): void {
3337
destroyAngularServerApp();
3438

@@ -43,6 +47,7 @@ export function setAngularAppTestingManifest(
4347
setAngularAppManifest({
4448
inlineCriticalCss: false,
4549
baseHref,
50+
locale,
4651
assets: {
4752
...additionalServerAssets,
4853
'index.server.html': {

0 commit comments

Comments
 (0)