Skip to content
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

Minify JSON-LD #146

Closed
atjn opened this issue Mar 13, 2023 · 4 comments
Closed

Minify JSON-LD #146

atjn opened this issue Mar 13, 2023 · 4 comments

Comments

@atjn
Copy link
Contributor

atjn commented Mar 13, 2023

Hi there and thanks for this very cool tool :)

It is relatively common for a site to add some form of json-ld metadata for search engines or other web crawlers to use. Here is a basic example for a PWA:

<script type="application/ld+json">
  {
    "@context": "https://schema.org/",
    "@type": "MobileApplication",
    "name": "Fancy App",
    "description": "Does cool stuff",
    "applicationCategory": "Entertainment"
  }
</script>

Currently, html-minifier-terser ignores these scripts, which means whitespace is not removed. I would suggest adding a rule called minifyJSON that takes any script matching type /^application\/(?:\S*\+)?json$/ui, and then removes whitespace with JSON.parse() followed by JSON.stringify().

If you want to be more advanced, there are also json-ld parsers which can theoretically minify the data even more, although I doubt the savings will be very large. For me personally, only removing the whitespace is fine :)

@atjn atjn changed the title Minify JSON+LD Minify JSON-LD Mar 13, 2023
@DanielRuf
Copy link
Contributor

Please see #34 (comment) and the other comments there.

@atjn
Copy link
Contributor Author

atjn commented Mar 14, 2023

I see, thank you!
Although I will note that this is a relatively bad minification method, since it still keeps a lot of the whitespace. It might even add whitespace to some tags. Here is the above script after being minified:

<script type="application/ld+json"> { "@context": "https://schema.org/", "@type": "MobileApplication", "name": "Fancy App", "description": "Does cool stuff", "applicationCategory": "Entertainment" } </script>

@atjn atjn closed this as completed Mar 14, 2023
atjn added a commit to atjn/easy-web-app-builder that referenced this issue Mar 14, 2023
@DanielRuf
Copy link
Contributor

Although I will note that this is a relatively bad minification method, since it still keeps a lot of the whitespace. It might even add whitespace to some tags. Here is the above script after being minified:

It may depend on your minifier settings.

grafik

@atjn
Copy link
Contributor Author

atjn commented Mar 14, 2023

It may depend on your minifier settings.

I gave it a shot, but I can't find any setting that removes the extra whitespace. The example you posted also doesn't remove most of the extra whitespace.

It seems that disabling conservativeCollapse removes the very first and very last whitespace character, as you also show in your example, but that is only a small amount of the whitespace removed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants