diff --git a/src/Symfony/Component/AssetMapper/ImportMap/ImportMapRenderer.php b/src/Symfony/Component/AssetMapper/ImportMap/ImportMapRenderer.php index ef955fca1447c..58bb9c0e347df 100644 --- a/src/Symfony/Component/AssetMapper/ImportMap/ImportMapRenderer.php +++ b/src/Symfony/Component/AssetMapper/ImportMap/ImportMapRenderer.php @@ -62,6 +62,11 @@ public function render(string|array $entryPoint, array $attributes = []): string continue; } + // for subdirectories or CDNs, the import name needs to be the full URL + if (str_starts_with($importName, '/') && $this->assetPackages) { + $importName = $this->assetPackages->getUrl(ltrim($importName, '/')); + } + $preload = $data['preload'] ?? false; if ('css' !== $data['type']) { $importMap[$importName] = $path; diff --git a/src/Symfony/Component/AssetMapper/Tests/ImportMap/ImportMapRendererTest.php b/src/Symfony/Component/AssetMapper/Tests/ImportMap/ImportMapRendererTest.php index a0d90e0cc5c15..6d53e93c8599d 100644 --- a/src/Symfony/Component/AssetMapper/Tests/ImportMap/ImportMapRendererTest.php +++ b/src/Symfony/Component/AssetMapper/Tests/ImportMap/ImportMapRendererTest.php @@ -54,6 +54,10 @@ public function testBasicRender() 'path' => 'https://ga.jspm.io/npm:es-module-shims', 'type' => 'js', ], + '/assets/implicitly-added' => [ + 'path' => '/assets/implicitly-added-d1g35t.js', + 'type' => 'js', + ], ]); $assetPackages = $this->createMock(Packages::class); @@ -92,6 +96,8 @@ public function testBasicRender() $this->assertStringNotContainsString('', $html); // remote js $this->assertStringContainsString('"remote_js": "https://cdn.example.com/assets/remote-d1g35t.js"', $html); + // both the key and value are prefixed with the subdirectory + $this->assertStringContainsString('"/subdirectory/assets/implicitly-added": "/subdirectory/assets/implicitly-added-d1g35t.js"', $html); } public function testNoPolyfill()