Skip to content

Commit 400fc82

Browse files
alan-agius4kirjs
authored andcommitted
refactor: replace getDocument() with inject(DOCUMENT)
This replaces `getDocument()` with `inject(DOCUMENT)` across hydration and transfer state logic.
1 parent c994267 commit 400fc82

5 files changed

Lines changed: 36 additions & 26 deletions

File tree

packages/core/src/hydration/api.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import {enableLocateOrCreateElementContainerNodeImpl} from '../render3/instructi
2424
import {enableApplyRootElementTransformImpl} from '../render3/instructions/shared';
2525
import {enableLocateOrCreateContainerAnchorImpl} from '../render3/instructions/template';
2626
import {enableLocateOrCreateTextNodeImpl} from '../render3/instructions/text';
27-
import {getDocument} from '../render3/interfaces/document';
2827
import {TransferState} from '../transfer_state';
2928
import {performanceMarkFeature} from '../util/performance';
3029
import {NgZone} from '../zone';
@@ -56,6 +55,7 @@ import {enableFindMatchingDehydratedViewImpl} from './views';
5655
import {DEHYDRATED_BLOCK_REGISTRY, DehydratedBlockRegistry} from '../defer/registry';
5756
import {gatherDeferBlocksCommentNodes} from './node_lookup_utils';
5857
import {processAndInitTriggers} from '../defer/triggering';
58+
import {DOCUMENT} from '../document';
5959

6060
/**
6161
* Indicates whether the hydration-related code was added,
@@ -186,8 +186,7 @@ export const CLIENT_RENDER_MODE_FLAG = 'ngcm';
186186
/**
187187
* Checks whether the `RenderMode.Client` was defined for the current route.
188188
*/
189-
function isClientRenderModeEnabled(): boolean {
190-
const doc = getDocument();
189+
function isClientRenderModeEnabled(doc: Document): boolean {
191190
return (
192191
(typeof ngServerMode === 'undefined' || !ngServerMode) &&
193192
doc.body.hasAttribute(CLIENT_RENDER_MODE_FLAG)
@@ -237,10 +236,15 @@ export function withDomHydration(): EnvironmentProviders {
237236
return;
238237
}
239238

239+
const doc = inject(DOCUMENT);
240240
if (inject(IS_HYDRATION_DOM_REUSE_ENABLED)) {
241-
verifySsrContentsIntegrity(getDocument());
241+
verifySsrContentsIntegrity(doc);
242242
enableHydrationRuntimeSupport();
243-
} else if (typeof ngDevMode !== 'undefined' && ngDevMode && !isClientRenderModeEnabled()) {
243+
} else if (
244+
typeof ngDevMode !== 'undefined' &&
245+
ngDevMode &&
246+
!isClientRenderModeEnabled(doc)
247+
) {
244248
const console = inject(Console);
245249
const message = formatRuntimeError(
246250
RuntimeErrorCode.MISSING_HYDRATION_ANNOTATIONS,
@@ -369,7 +373,7 @@ export function withIncrementalHydration(): Provider[] {
369373
provide: APP_BOOTSTRAP_LISTENER,
370374
useFactory: () => {
371375
const injector = inject(Injector);
372-
const doc = getDocument();
376+
const doc = inject(DOCUMENT);
373377

374378
return () => {
375379
const deferBlockData = processBlockData(injector);

packages/core/src/render3/util/transfer_state_utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9-
import {getDocument} from '../interfaces/document';
109
import {Injector} from '../../di';
1110
import {isInternalHydrationTransferStateKey} from '../../hydration/utils';
1211
import {APP_ID} from '../../application/application_tokens';
1312
import {retrieveTransferredState} from '../../transfer_state';
13+
import {DOCUMENT} from '../../document';
1414

1515
/**
1616
* Retrieves transfer state data from the DOM using the provided injector to get APP_ID.
@@ -22,7 +22,7 @@ import {retrieveTransferredState} from '../../transfer_state';
2222
* @returns The transfer state data as an object, or empty object if not available
2323
*/
2424
export function getTransferState(injector: Injector): Record<string, unknown> {
25-
const doc = getDocument();
25+
const doc = injector.get(DOCUMENT);
2626
const appId = injector.get(APP_ID);
2727

2828
const transferState = retrieveTransferredState(doc, appId);

packages/core/src/transfer_state.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import {APP_ID} from './application/application_tokens';
1010
import {inject} from './di/injector_compatibility';
1111
import {ɵɵdefineInjectable} from './di/interface/defs';
12-
import {getDocument} from './render3/interfaces/document';
12+
import {DOCUMENT} from './document';
1313

1414
/**
1515
* A type-safe key to use with `TransferState`.
@@ -48,15 +48,6 @@ export function makeStateKey<T = void>(key: string): StateKey<T> {
4848
return key as StateKey<T>;
4949
}
5050

51-
function initTransferState(): TransferState {
52-
const transferState = new TransferState();
53-
if (typeof ngServerMode === 'undefined' || !ngServerMode) {
54-
transferState.store = retrieveTransferredState(getDocument(), inject(APP_ID));
55-
}
56-
57-
return transferState;
58-
}
59-
6051
/**
6152
* A key value store that is transferred from the application on the server side to the application
6253
* on the client side.
@@ -77,7 +68,14 @@ export class TransferState {
7768
static ɵprov = /** @pureOrBreakMyCode */ /* @__PURE__ */ ɵɵdefineInjectable({
7869
token: TransferState,
7970
providedIn: 'root',
80-
factory: initTransferState,
71+
factory: () => {
72+
const transferState = new TransferState();
73+
if (typeof ngServerMode === 'undefined' || !ngServerMode) {
74+
transferState.store = retrieveTransferredState(inject(DOCUMENT), inject(APP_ID));
75+
}
76+
77+
return transferState;
78+
},
8179
});
8280

8381
/** @internal */

packages/core/test/bundling/hydration/bundle.golden_symbols.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,6 @@
537537
"initDomAdapter",
538538
"initFeatures",
539539
"initTNodeFlags",
540-
"initTransferState",
541540
"initializeDirectives",
542541
"initializeInputAndOutputAliases",
543542
"inject2",

packages/platform-server/test/transfer_state_spec.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
ɵgetTransferState as getTransferState,
1616
Injector,
1717
inject,
18-
ɵsetDocument as setDocument,
18+
DOCUMENT,
1919
} from '@angular/core';
2020
import {BrowserModule, withEventReplay, withIncrementalHydration} from '@angular/platform-browser';
2121
import {renderModule, ServerModule} from '../index';
@@ -132,18 +132,27 @@ describe('transfer_state', () => {
132132
}
133133
}
134134

135-
const appId = 'custom-app-id';
136-
const providers = [{provide: APP_ID, useValue: appId}];
137135
const hydrationFeatures = () => [withIncrementalHydration(), withEventReplay()];
138136

139-
const html = await ssr(SimpleComponent, {envProviders: providers, hydrationFeatures});
137+
const appId = 'custom-app-id';
138+
const html = await ssr(SimpleComponent, {
139+
envProviders: [{provide: APP_ID, useValue: appId}],
140+
hydrationFeatures,
141+
});
140142
const transferCacheJson = getHydrationInfoFromTransferState(html)!;
141143

142144
// getTransferState reaches into the DOM to retrieve the transfer state.
143145
// So we need to set the document with the generated HTML.
144146
const {document} = domino.createWindow(html);
145-
setDocument(document);
146-
const transferState = getTransferState(Injector.create({providers}));
147+
148+
const transferState = getTransferState(
149+
Injector.create({
150+
providers: [
151+
{provide: DOCUMENT, useValue: document},
152+
{provide: APP_ID, useValue: appId},
153+
],
154+
}),
155+
);
147156

148157
// The transfer state also contains internal hydration keys,
149158
expect(Object.keys(transferState).length).not.toEqual(JSON.parse(transferCacheJson).length);

0 commit comments

Comments
 (0)