I have the following template code on my homepage example.com
{% block content %}
<img id='imgID' src={{ asset(/asset/img.png) }}>
{% endblock %}
This loads correctly
Now I navigate to example.com/foo
by clicking on a link. This page contains an Ajax
request that returns a new src
for my <img>
in the promise:
$('#imgID').attr('src', '/asset/${newimg}')
The image updates correctly BUT now if I refresh the browser page by clicking on the browser refresh button I get 404
on my image
Error Failed to load resource: the server responded with a status of 404 (Not Found) http://example.com/foo/asset/newimg.png'
it should be http://example.com/asset/newimg.png'
same error if I load the ajax call from anything other than example.com
I do not understand why, I am using an absolute path in my Ajax callback. The twig {{ asset() }}
function is not loading correctly?
What is the correct way of updating a twig asset path with an ajax request from any url without having to use the asset manager server side? Can I somehow specify a true absolute path ?
Do I have to use composer require symfony/asset
like below and therefore make an Ajax call every time I want to update an asset with Ajax ? Or can I keep it client side ? the entire thing seems over complicated and a complete mess
server code:
$package = new Package(new EmptyVersionStrategy());
$img = $package->getUrl('/asset/newimg.png');