Keeping scripts out of local Jekyll builds
Some code belongs on a production site but gets in the way during local development. Analytics and comment scripts are common examples. Jekyll exposes the current environment as jekyll.environment, so you can include that code conditionally.
{% if jekyll.environment == "production" %}
<script>
// JavaScript snippet...
</script>
{% endif %}
The block is rendered only when the environment is production. Set the value when you build or serve the site:
JEKYLL_ENV=production jekyll build
For local work, you can use JEKYLL_ENV=development jekyll serve --watch, although development is already Jekyll’s default. I prefer keeping production-only integrations behind this small check rather than loading them locally and adding exceptions elsewhere.
Enjoyed reading this?
If you found this article helpful, consider supporting my writing. Every little bit helps keep this blog going.
Related articles
- The HTTP Status Codes I Actually Check
A practical guide to common HTTP response codes, redirects, client and server errors, and the headers that explain them.
- What Makes a Website a Progressive Web App?
A practical look at service workers, web app manifests, installation, and the cases where a Progressive Web App is worth building.
- What a Static Site Generator Actually Does
A static site generator combines content and templates before deployment, producing HTML files that a web server can deliver directly.