-
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.
refactor(@angular/build): avoid hydration warnings when `RenderMode.C…
…lient` is set With the introduction of the `RenderMode` configuration for routes, some routes may be set to `RenderMode.Client` while still including the `provideClientHydration()` function in the provider list during bootstrap. This led to a false-positive warning in the console, incorrectly suggesting a hydration misconfiguration. This commit introduces a DOM marker that allows the framework to bypass these unnecessary checks, preventing the misleading warnings. See: angular/angular#58004
- Loading branch information
1 parent
c33e862
commit 80686be
Showing
3 changed files
with
81 additions
and
0 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
42 changes: 42 additions & 0 deletions
42
packages/angular/build/src/utils/index-file/ngcm-attribute.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,42 @@ | ||
/** | ||
* @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 { htmlRewritingStream } from './html-rewriting-stream'; | ||
|
||
/** | ||
* Defines a name of an attribute that is added to the `<body>` tag | ||
* in the `index.html` file in case a given route was configured | ||
* with `RenderMode.Client`. 'cm' is an abbreviation for "Client Mode". | ||
* | ||
* @see https://github.com/angular/angular/pull/58004 | ||
*/ | ||
const CLIENT_RENDER_MODE_FLAG = 'ngcm'; | ||
|
||
/** | ||
* Transforms the provided HTML by adding the `ngcm` attribute to the `<body>` tag. | ||
* This is used in the client-side rendered (CSR) version of `index.html` to prevent hydration warnings. | ||
* | ||
* @param html The HTML markup to be transformed. | ||
* @returns A promise that resolves to the transformed HTML string with the necessary modifications. | ||
*/ | ||
export async function addNgcmAttribute(html: string): Promise<string> { | ||
const { rewriter, transformedContent } = await htmlRewritingStream(html); | ||
|
||
rewriter.on('startTag', (tag) => { | ||
if ( | ||
tag.tagName === 'body' && | ||
!tag.attrs.some((attr) => attr.name === CLIENT_RENDER_MODE_FLAG) | ||
) { | ||
tag.attrs.push({ name: CLIENT_RENDER_MODE_FLAG, value: '' }); | ||
} | ||
|
||
rewriter.emitStartTag(tag); | ||
}); | ||
|
||
return transformedContent(); | ||
} |
33 changes: 33 additions & 0 deletions
33
packages/angular/build/src/utils/index-file/ngcm-attribute_spec.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,33 @@ | ||
/** | ||
* @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 { addNgcmAttribute } from './ngcm-attribute'; | ||
|
||
describe('addNgcmAttribute', () => { | ||
it('should add the ngcm attribute to the <body> tag', async () => { | ||
const result = await addNgcmAttribute(` | ||
<html> | ||
<head></head> | ||
<body><p>hello world!</p></body> | ||
</html> | ||
`); | ||
|
||
expect(result).toContain('<body ngcm=""><p>hello world!</p></body>'); | ||
}); | ||
|
||
it('should not override an existing ngcm attribute', async () => { | ||
const result = await addNgcmAttribute(` | ||
<html> | ||
<head></head> | ||
<body ngcm="foo"><p>hello world!</p></body> | ||
</html> | ||
`); | ||
|
||
expect(result).toContain('<body ngcm="foo"><p>hello world!</p></body>'); | ||
}); | ||
}); |