Skip to content

[AssetMapper] and framework assets version #57584

@mathielen

Description

@mathielen

Symfony version(s) affected

6.4+

Description

I am migrating some code from using webpack encore to assetmapper.
I found a very strange behavior and debugged it for a long time. Everything seems to work with assetmapper but the browser still shows a 404 error when importing relative js and css files from app.js like this:

...
//this is for symfony ux
import './bootstrap.js';
...

When looking at the network tab in the browser it would still download the bootstrap.js file (with the correct version hash). And also the importmap looked good:

<script type="importmap">
{
    "imports": {
...
        "/assets/bootstrap.js?v=dev": "/assets/bootstrap-209b7ad405377c6efaf2dffd7b3feba1.js?v=dev",
...

What I did not saw at first (as I'm new to assetmapper) - there is a ?v=dev in the urls. That didnt bother me too much.
Turns out, that was the problem.

Because when you activate asset versioning with the symfony framework like here: https://symfony.com/doc/current/reference/configuration/framework.html#version
it adds the version string to all the generated urls. Which then doesnt match with actual URLs from the in-browser executed app.js anymore.
My framework/assets configuration looked like this:

framework:
  assets:
    version: 'dev'
    version_format: '%%s?v=%%s'

Disabling the whole assets configuration solved my problem.

It might be a good thing to point to this problem in the documentation of assetmapper or somehow solve this so the url-generation in importmap would not use the asset versioning feature even if its turned on.

Cheers
Markus

How to reproduce

  1. Use basic Assetmapper set up
  2. Enable asset versioning:

in config/packages/framework.yml

framework:
  assets:
    version: 'dev'
    version_format: '%%s?v=%%s'
  1. Import a relative file in app.js
...
//this is for symfony ux
import './bootstrap.js';
...
  1. See 404 errors in the browser

Possible Solution

Disable framework asset versioning.

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions