-
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.
feat(@angular-devkit/build-angular): inject event-dispatch in SSR HTM…
…L page This commit add support in the Angular CLI to inject the event-dispatcher script when using the application builder.
- Loading branch information
1 parent
adf1197
commit d51cb59
Showing
10 changed files
with
178 additions
and
93 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
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
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
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
31 changes: 31 additions & 0 deletions
31
packages/angular_devkit/build_angular/src/utils/index-file/add-event-dispatch-contract.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,31 @@ | ||
/** | ||
* @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.io/license | ||
*/ | ||
|
||
import { readFile } from 'node:fs/promises'; | ||
import { htmlRewritingStream } from './html-rewriting-stream'; | ||
|
||
let jsActionContractScript: string; | ||
|
||
export async function addEventDispatchContract(html: string): Promise<string> { | ||
const { rewriter, transformedContent } = await htmlRewritingStream(html); | ||
|
||
jsActionContractScript ??= | ||
'<script type="text/javascript" id="ng-event-dispatch-contract">' + | ||
(await readFile(require.resolve('@angular/core/event-dispatch-contract.min.js'), 'utf-8')) + | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong. |
||
'</script>'; | ||
|
||
rewriter.on('startTag', (tag) => { | ||
rewriter.emitStartTag(tag); | ||
|
||
if (tag.tagName === 'body') { | ||
rewriter.emitRaw(jsActionContractScript); | ||
} | ||
}); | ||
|
||
return transformedContent(); | ||
} |
26 changes: 26 additions & 0 deletions
26
...ges/angular_devkit/build_angular/src/utils/index-file/add-event-dispatch-contract_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,26 @@ | ||
/** | ||
* @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.io/license | ||
*/ | ||
|
||
import { addEventDispatchContract } from './add-event-dispatch-contract'; | ||
|
||
describe('addEventDispatchContract', () => { | ||
it('should inline event dispatcher script', async () => { | ||
const result = await addEventDispatchContract(` | ||
<html> | ||
<head></head> | ||
<body> | ||
<h1>Hello World!</h1> | ||
</body> | ||
</html> | ||
`); | ||
|
||
expect(result).toMatch( | ||
/<body>\s*<script type="text\/javascript" id="ng-event-dispatch-contract">.+<\/script>\s*<h1>/, | ||
); | ||
}); | ||
}); |
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
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
Oops, something went wrong.
Hi @alan-agius4
Will this
readFile
happen on every page render in SSR?If yes, won't it affect performance negatively? (then perhaps we might want to put the file contents into some in-memory cache to reuse in between renders)