Skip to content

Commit 3cf7a52

Browse files
committed
fix(@angular/ssr): initialize the DI tokens with null to avoid requiring them to be set to optional
This commit initializes the `REQUEST`, `RESPONSE_INIT`, and `REQUEST_CONTEXT` DI tokens with `null` values by default. Previously, these tokens lacked default values, which meant that developers had to explicitly mark them as optional when injecting them in constructors. This was necessary to avoid errors when these tokens were not provided, particularly in scenarios like client-side rendering or during development. By initializing these tokens with `null`, we eliminate the need for developers to mark them as optional, simplifying the code and improving the developer experience. (cherry picked from commit 479bfd4)
1 parent 2ec877d commit 3cf7a52

File tree

2 files changed

+50
-7
lines changed

2 files changed

+50
-7
lines changed

goldens/public-api/angular/ssr/tokens/index.api.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
import { InjectionToken } from '@angular/core';
88

99
// @public
10-
export const REQUEST: InjectionToken<Request>;
10+
export const REQUEST: InjectionToken<Request | null>;
1111

1212
// @public
1313
export const REQUEST_CONTEXT: InjectionToken<unknown>;
1414

1515
// @public
16-
export const RESPONSE_INIT: InjectionToken<ResponseInit>;
16+
export const RESPONSE_INIT: InjectionToken<ResponseInit | null>;
1717

1818
// (No @packageDocumentation comment for this package)
1919

packages/angular/ssr/tokens/src/tokens.ts

+48-5
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,62 @@
99
import { InjectionToken } from '@angular/core';
1010

1111
/**
12-
* Injection token for the current request.
12+
* Injection token representing the current HTTP request object.
13+
*
14+
* Use this token to access the current request when handling server-side
15+
* rendering (SSR).
16+
*
17+
* @remarks
18+
* This token may be `null` in the following scenarios:
19+
*
20+
* * During the build processes.
21+
* * When the application is rendered in the browser (client-side rendering).
22+
* * When performing static site generation (SSG).
23+
* * During route extraction in development (at the time of the request).
24+
*
25+
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Request | `Request` on MDN}
26+
*
1327
* @developerPreview
1428
*/
15-
export const REQUEST = new InjectionToken<Request>('REQUEST');
29+
export const REQUEST = new InjectionToken<Request | null>('REQUEST', {
30+
providedIn: 'platform',
31+
factory: () => null,
32+
});
1633

1734
/**
18-
* Injection token for the response initialization options.
35+
* Injection token for response initialization options.
36+
*
37+
* Use this token to provide response options for configuring or initializing
38+
* HTTP responses in server-side rendering or API endpoints.
39+
*
40+
* @remarks
41+
* This token may be `null` in the following scenarios:
42+
*
43+
* * During the build processes.
44+
* * When the application is rendered in the browser (client-side rendering).
45+
* * When performing static site generation (SSG).
46+
* * During route extraction in development (at the time of the request).
47+
*
48+
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Response/Response | `ResponseInit` on MDN}
49+
*
1950
* @developerPreview
2051
*/
21-
export const RESPONSE_INIT = new InjectionToken<ResponseInit>('RESPONSE_INIT');
52+
export const RESPONSE_INIT = new InjectionToken<ResponseInit | null>('RESPONSE_INIT', {
53+
providedIn: 'platform',
54+
factory: () => null,
55+
});
2256

2357
/**
2458
* Injection token for additional request context.
59+
*
60+
* Use this token to pass custom metadata or context related to the current request in server-side rendering.
61+
*
62+
* @remarks
63+
* This token is only available during server-side rendering and will be `null` in other contexts.
64+
*
2565
* @developerPreview
2666
*/
27-
export const REQUEST_CONTEXT = new InjectionToken<unknown>('REQUEST_CONTEXT');
67+
export const REQUEST_CONTEXT = new InjectionToken<unknown>('REQUEST_CONTEXT', {
68+
providedIn: 'platform',
69+
factory: () => null,
70+
});

0 commit comments

Comments
 (0)