Skip to content

Commit 324f6a3

Browse files
committed
fix: Correctly add module content type to script tags when scripts with versions are used
Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
1 parent e9174a4 commit 324f6a3

2 files changed

Lines changed: 14 additions & 1 deletion

File tree

lib/private/legacy/template/functions.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ function emit_script_tag(string $src, string $script_content = '', string $conte
101101
*/
102102
function emit_script_loading_tags($obj) {
103103
foreach ($obj['jsfiles'] as $jsfile) {
104-
$type = str_ends_with($jsfile, '.mjs') ? 'module' : '';
104+
$fileName = explode('?', $jsfile, 2)[0];
105+
$type = str_ends_with($fileName, '.mjs') ? 'module' : '';
105106
emit_script_tag($jsfile, '', $type);
106107
}
107108
if (!empty($obj['inline_ocjs'])) {

tests/lib/TemplateFunctionsTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,18 @@ public function testEmitScriptLoadingTags() {
8686
]);
8787
}
8888

89+
public function testEmitScriptLoadingTagsWithVersion() {
90+
// Test mjs js and inline content
91+
$pattern = '/src="some\.mjs\?v=ab123cd"[^>]+type="module"[^>]*>.+\n'; // some.mjs with type = module
92+
$pattern .= '<script[^>]+src="other\.js\?v=12abc34"[^>]*>.+\n'; // other.js as plain javascript
93+
$pattern .= '/'; // no flags
94+
95+
$this->expectOutputRegex($pattern);
96+
emit_script_loading_tags([
97+
'jsfiles' => ['some.mjs?v=ab123cd', 'other.js?v=12abc34'],
98+
]);
99+
}
100+
89101
// ---------------------------------------------------------------------------
90102
// Test relative_modified_date with dates only
91103
// ---------------------------------------------------------------------------

0 commit comments

Comments
 (0)