Fix: HTML minifier corrupts pages with speculationrules or JSON-LD scripts #354
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hi Cache Enabler Team,
First off, thank you for creating and maintaining such a fantastic, fast, and easy-to-use caching plugin.
I've identified and fixed a critical bug in the HTML minification logic that can cause page corruption.
The Bug
When the setting
Minify HTML in cached pages including inline CSS and JavaScript.is enabled, the page content can become corrupted if the source HTML contains a<script>tag with JSON content, such astype="speculationrules"ortype="application/ld+json".The minifier mistakenly identifies character sequences within JSON string values (e.g.,
\/*) as the beginning of a comment. It then removes everything until it finds the next closing comment tag (*/) in the document, which is often in a completely different<style>block, leading to a broken page.Example Source HTML:
Broken Output (Before Fix):
The minifier removes the content between
\/*in the script and*/in the style tag.The Root Cause
The core issue lies in applying a single, global regular expression to the entire HTML document to strip JS/CSS comments. This regex lacks context and cannot distinguish between a legitimate comment and a similar string pattern inside a JSON literal.
Proposed Solution
This pull request refactors the inline CSS/JS minification logic to be context-aware and safer.
Instead of running a single
preg_replaceon the entire HTML body, it now usespreg_replace_callbackto find each<script>and<style>block individually.Initial regex for stripping comments left unchanged.
This approach is more robust because:
speculationrules,application/ld+json). Since JSON does not officially support comments, attempting to strip them is both unnecessary and dangerous.How to Test
Minify HTML in cached pages including inline CSS and JavaScript.\/*and*/.speculationrulesscript will be intact, and the page will render correctly.