Skip to content
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

Allow changing the URL in RenderAssetTagEvent #105

Open
pableu opened this issue Jan 25, 2021 · 0 comments
Open

Allow changing the URL in RenderAssetTagEvent #105

pableu opened this issue Jan 25, 2021 · 0 comments

Comments

@pableu
Copy link

pableu commented Jan 25, 2021

I have an application where it would be nice to modify the URL before rendering.

In my case, I want to change the URL based on the current request. E.g. if the user visits company.net I want to get the assets from media.company.net, and if the user visits shop.com, I want to get the assets from media.shop.com.

So far added my own twig functions that override the behavior of the default {{ encore_entry_script_tags }} and {{ encore_entry_style_tags }}, but I don't like that a lot.

Version 1.9.0 gave me an idea: What if we allowed to set a new URL to the RenderAssetTagEvent?

E.g. something like this:

<?php
declare(strict_types=1);

namespace App\EventSubscriber;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\WebpackEncoreBundle\Event\RenderAssetTagEvent;

class EncoreTagSubscriber implements EventSubscriberInterface
{
  public static function getSubscribedEvents()
  {
    return [
        RenderAssetTagEvent::class => 'onRenderAssetTag'
    ];
  }

  public function onRenderAssetTag(RenderAssetTagEvent $event)
  {
    // change URL according to our business logic
    $event->setUrl(str_replace('example.TLD', 'example.com', $event->getUrl()));
  }
}

This actually works like this, but it feels wrong and I need to set either src or href depending on type:

$event->setAttribute('src', str_replace('example.TLD', 'example.com', $event->getUrl()));

Would you be interested in a PR that adds the RenderAssetTagEvent::setUrl method? Or is my $event->setAttribute('src', ...); workaround okay and future proof?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant