Skip to content

Commit 796c520

Browse files
committed
feature #77 [TagRenderer] Dispatch a RenderAssetTagEvent before rendering each tag (Kocal)
This PR was merged into the main branch. Discussion ---------- [TagRenderer] Dispatch a `RenderAssetTagEvent` before rendering each tag | Q | A | -------------- | --- | Bug fix? | no | New feature? | yes <!-- please update CHANGELOG.md file --> | Deprecations? | no <!-- if yes, also update UPGRADE-*.md and CHANGELOG.md files --> | Documentation? | yes <!-- required for new features, or documentation updates --> | Issues | Fix #71 <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead --> | License | MIT <!-- Replace this notice by a description of your feature/bugfix. This will help reviewers and should be a good start for the documentation. Additionally (see https://symfony.com/releases): - Always add tests and ensure they pass. - For new features, provide some code snippets to help understand usage. - Features and deprecations must be submitted against branch main. - Update/add documentation as required (we can help!) - Changelog entry should follow https://symfony.com/doc/current/contributing/code/conventions.html#writing-a-changelog-entry - Never break backward compatibility (see https://symfony.com/bc). --> Commits ------- b768736 [TagRenderer] Dispatch a `RenderAssetTagEvent` before rendering each tag
2 parents d18b362 + b768736 commit 796c520

14 files changed

Lines changed: 1092 additions & 18 deletions

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# CHANGELOG
22

3+
## 0.7.0
4+
5+
- Add the `RenderAssetTagEvent`, dispatched before each rendered `<script>`/`<link>` (including the injected dev client and React preamble), so listeners can add, change or remove attributes such as a CSP nonce
6+
37
## 0.6.0
48

59
- Add a per-call `attributes` argument to the `reprise_entry_script_tags()` and `reprise_entry_link_tags()` Twig functions

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Symfony Reprise covers only the Symfony-side glue the bundlers leave out:
3535
- 🧩 **Symfony UX / Stimulus**: registers `controllers.json` and local controllers, eager or lazy
3636
- 🌐 **CDN support**: serve built assets from an absolute `publicPath`
3737
- 🛡️ **Subresource Integrity**: SRI hashes in `entrypoints.json`
38+
- 🔐 **Customizable tag attributes**: `RenderAssetTagEvent` lets listeners add, change or remove attributes on every rendered tag (e.g. a CSP nonce)
3839

3940
Vite and Rsbuild already handle **Sass/Less/PostCSS**, **TypeScript**, **JSX/Vue/Svelte**, **code splitting**, **content hashing**, **source maps**, **minification** and **HMR** on their own, so Symfony Reprise does not reimplement any of that.
4041

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"require": {
1818
"php": ">=8.4",
1919
"symfony/asset": "^7.4|^8.0",
20+
"symfony/event-dispatcher-contracts": "^3",
2021
"twig/twig": "^3.0"
2122
},
2223
"require-dev": {

doc/index.rst

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ reimplement any of that. It covers only the Symfony-side glue the bundlers leave
3030
- **Symfony UX / Stimulus**: registers ``controllers.json`` and local controllers, eager or lazy
3131
- **CDN support**: serve built assets from an absolute ``publicPath``
3232
- **Subresource Integrity**: SRI hashes in ``entrypoints.json``
33+
- **Customizable tag attributes**: ``RenderAssetTagEvent`` lets listeners add, change or remove attributes on every rendered tag
3334

3435
It generates the Encore-compatible ``entrypoints.json`` and ``manifest.json`` that ``RepriseBundle`` reads to render
3536
the ``<script>`` and ``<link>`` tags, wires up the native dev server, and turns your Stimulus controllers into a
@@ -171,6 +172,33 @@ same whether Vite or Rsbuild produced ``entrypoints.json``, and there's nothing
171172
``reprise_entry_script_tags`` injects the Vite HMR client automatically, while under Rsbuild the client is compiled
172173
into the bundle.
173174

175+
Customizing rendered tags
176+
-------------------------
177+
178+
Before Reprise writes any ``<script>`` or ``<link>`` tag (entry files, CSS, and the dev-server tags it injects itself,
179+
like the Vite HMR client and the React Fast Refresh preamble), it dispatches a ``RenderAssetTagEvent``.
180+
A listener can read and mutate ``$event->attributes`` to add, change, or remove attributes on that tag.
181+
182+
As one example, stamping a Content-Security-Policy nonce on every tag::
183+
184+
namespace App\EventListener;
185+
186+
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
187+
use Symfony\Reprise\Event\RenderAssetTagEvent;
188+
189+
#[AsEventListener]
190+
final class CspNonceListener
191+
{
192+
public function __construct(private NonceGenerator $nonceGenerator)
193+
{
194+
}
195+
196+
public function __invoke(RenderAssetTagEvent $event): void
197+
{
198+
$event->attributes['nonce'] = $this->nonceGenerator->getNonce();
199+
}
200+
}
201+
174202
Features
175203
--------
176204

0 commit comments

Comments
 (0)