Skip to content

AssetMapper's importmap key is missing project prefix when deploying to subdirectory #52520

@hashbanged

Description

@hashbanged

Symfony version(s) affected

6.3, 6.4

Description

Projects using AssetMapper and deployed to a subdirectory (e.g., https://dev.local/asset-mapper-test) are unable to load assets imported in app.js. For example, app.js would include assets with the following:

assets/app.js

import './bootstrap.js';

The importmap is rendered without the project prefix ("asset-mapper-test"), leading to a 404:

index.php

<script type="importmap">
{
    "imports": {
        "app": "/asset-mapper-test/assets/app-8b5b10df553be204955ec428f07f7774.js",
        "/assets/bootstrap.js": "/asset-mapper-test/assets/bootstrap-c423b8bbc1f9cae218c105ca8ca9f767.js",
        "@Symfony.com/stimulus-bundle": "/asset-mapper-test/assets/@symfony/stimulus-bundle/loader-e1ee9ace0562f2e6a52301e4ccc8627d.js",
        "@hotwired/stimulus": "/asset-mapper-test/assets/vendor/@hotwired/stimulus-b5b1d00e42695b8959b4a1e94e3bc92a.js",
        "/assets/@symfony/stimulus-bundle/controllers.js": "/asset-mapper-test/assets/@symfony/stimulus-bundle/controllers-9ecc391c3a41226613a9c690f864b6b8.js",
        "/assets/controllers/hello_controller.js": "/asset-mapper-test/assets/controllers/hello_controller-7e7d7d4d4fc7f0016ac0f083fefacc14.js"
    }
}
</script>

The missing asset prefix (subdirectory) causes the resource to be unversioned (Figure 1).

initiator
Figure 1. Request initiator chain showing an unversioned bootstrap.js request

How to reproduce

  1. Configure a server for subdirectory deployment. I used Apache2 for this:
Alias /asset-mapper-test/ /srv/https/dev.local/asset-mapper-test/public/
<Directory /srv/https/dev.local/asset-mapper-test/public>
    AllowOverride None
    Require all granted

    <FilesMatch \.php$>
       SetHandler "proxy:unix:/run/php/php8.2-fpm.sock|fcgi://localhost"
    </FilesMatch>

    DirectoryIndex index.php
    Options FollowSymlinks

    <IfModule mod_negotiation.c>
        Options -MultiViews +FollowSymlinks
    </IfModule>

    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteBase /asset-mapper-test

        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_URI} !=/favicon.ico
        RewriteRule ^ ./index.php [L]
    </IfModule>

    <IfModule !mod_rewrite.c>
        <IfModule mod_alias.c>
            RedirectMatch 307 ^/$ /index.php/
        </IfModule>
    </IfModule>
</Directory>
  1. Create a new Symfony (* 6.4.x-dev) project with AssetMapper as a dependency. At the time of this submission, I configured composer with:
{
    "minimum-stability": "dev",
    "prefer-stable": false,
    "extra": {
        "symfony": {
            "allow-contrib": false,
            "require": "^6.4-dev"
        }
    },
}
  1. Add a javascript file to the project:

assets/some-dependency.js

console.log("Woot!");
  1. Import the dependency:

assets/app.js

/*
 * Welcome to your app's main JavaScript file!
 *
 * This file will be included onto the page via the importmap() Twig function,
 * which should already be in your base.html.twig.
 */
import './some-dependency.js';

console.log('This log comes from assets/app.js - welcome to AssetMapper! 🎉')
  1. Check the browser console for the log line and network loaded assets. Also review ./bin/console debug:asset-map --full for mappings.

Possible Solution

No response

Additional Context

Also see #51417.

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

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy