-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add origin to internal tags #49
Add origin to internal tags #49
Conversation
✅ Deploy Preview for cosmic-bubblegum-aa631a canceled.
|
Hi @seggewiss, thanks for your suggestion. Actually, when you define custom attributes, as you could see they are assigned only to the associated entry point. {{ vite_entry_script_tags('app', { attr: { nonce: 'foobar' }}) }} render <script type="module" src="http://127.0.0.1:5174/build/@vite/client" crossorigin></script>
<script crossorigin type="module" src="http://127.0.0.1:5174/build/assets/app.js" nonce="foobar"></script> with your PR, the result would be: <script type="module" src="http://127.0.0.1:5174/build/@vite/client" crossorigin nonce="foobar"></script>
<script crossorigin type="module" src="http://127.0.0.1:5174/build/assets/app.js" nonce="foobar"></script> Several points bother me:
{{ vite_entry_script_tags('app', { attr: { foo: 'bar' }}) }}
{{ vite_entry_script_tags('form', { attr: { hello: 'world' }}) }} render <script type="module" src="http://127.0.0.1:5174/build/@vite/client" foo="bar" crossorigin></script>
<script crossorigin type="module" src="http://127.0.0.1:5174/build/assets/app.js" foo="bar"></script>
<script crossorigin type="module" src="http://127.0.0.1:5174/build/assets/form.js" hello="world"></script> but this {{ vite_entry_script_tags('form', { attr: { hello: 'world' }}) }}
{{ vite_entry_script_tags('app', { attr: { foo: 'bar' }}) }} render <script type="module" src="http://127.0.0.1:5174/build/@vite/client" hello="world" crossorigin></script>
<script crossorigin type="module" src="http://127.0.0.1:5174/build/assets/app.js" foo="bar"></script>
<script crossorigin type="module" src="http://127.0.0.1:5174/build/assets/form.js" hello="world"></script> the behavior is easily predictible but if the twig functions are in different files with inheritance, I'm afraid we'll end up with a behavior that's hard to predict. For all these reasons I'm a little reluctant for your proposal. On the other hand, your issue can be easily solved by adding a listener on // src/EventSubscriber/ScriptNonceSubscriber.php
namespace App\EventSubscriber;
use Pentatrion\ViteBundle\Event\RenderAssetTagEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ScriptNonceSubscriber implements EventSubscriberInterface
{
public function onRenderAssetTag(RenderAssetTagEvent $event)
{
$tag = $event->getTag();
if ($tag->isInternal()) {
$tag->setAttribute('nonce', 'your nonce');
}
}
public static function getSubscribedEvents(): array
{
return [
RenderAssetTagEvent::class => 'onRenderAssetTag',
];
}
} What do you think? Good evening |
Morning @lhapaipai 👋 I understand your concerns. Especially as this could potentially break existing integrations. However our usecase is also not feasible via the RenderAssetTagEvent. I need to be able to add the nonce in one template for a specific Vite entry name. I have no way to check which app is being rendered in the Event. The tag does not carry the information and the Event itself also doesn't. As I see it we have 2 Options
I'm happy to provide both approaches. I would prefer option 1 though 😊 |
Hi @seggewiss, It is possible to retrieve the entry point in the subscriber, class ScriptNonceSubscriber implements EventSubscriberInterface
{
public function onRenderAssetTag(RenderAssetTagEvent $event)
{
$tag = $event->getTag();
if ($tag->getOrigin() === 'app') {
} elseif ($tag->getOrigin() === '_internal') {
}
}
public static function getSubscribedEvents(): array
{
return [
RenderAssetTagEvent::class => 'onRenderAssetTag',
];
}
} implementing a if you invoke the Vite client will have the attributes of the first entry point that will invoke it child-1.html.twig {% extends 'parent.html.twig' %}
{% block javascripts %}
{{ vite_entry_link_tags('app', { attr: { foo: 'bar' }}) }}
{% endblock %} child-2.html.twig {% extends 'parent.html.twig' %}
{% block javascripts %}{% endblock %} parent.html.twig <html>
<head>
{% block javascripts %}{% endblock %}
{{ vite_entry_link_tags('app', { attr: { foo: 'baz' }}) }}
</head>
</html> In your case maybe it won't bother you because you will be aware of it but for a user who has not followed this discussion, the side effects could seem unpredictable. a suggestion :
we could thus recover the entry point that launched the Vite client. |
dd78e8f
to
2582b24
Compare
Hey @lhapaipai 👋 I hope this is to your liking. I checked all use cases of the Tag Class and adjusted accordingly. Hope you have a nice rest of your day 😊 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's perfect thank you very much
hi @seggewiss, |
Hey @lhapaipai 👋 |
I noticed that custom attribtues aren't applied to the Vite client script. This causes problems if you need to pass a nonce for example.