Skip to content

[AssetMapper] If assets are served from a subdirectory or CDN, also adjust importmap keys #52519

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

Merged
merged 1 commit into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -92,6 +96,8 @@ public function testBasicRender()
$this->assertStringNotContainsString('<link rel="stylesheet" href="/subdirectory/assets/styles/app-nopreload-d1g35t.css">', $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()
Expand Down