Skip to content

Commit 49ee28f

Browse files
committed
fix(gtmetrix): escape special regex characters in unused JavaScript removal
1 parent ba3f4bb commit 49ee28f

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

frontend/scripts/gtmetrix-optimizer.cjs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,9 @@ function removeUnusedJavaScript() {
316316
if (namedImports) {
317317
const importNames = namedImports[1].split(',').map(name => name.trim());
318318
const usedImports = importNames.filter(name => {
319-
const regex = new RegExp(`\\b${name}\\b`, 'g');
319+
// Escape special regex characters to prevent invalid regex patterns
320+
const escapedName = name.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
321+
const regex = new RegExp(`\\b${escapedName}\\b`, 'g');
320322
const matches = js.match(regex) || [];
321323
return matches.length > 1; // More than just the import statement
322324
});

0 commit comments

Comments
 (0)