Eleventy - Differentiate between dev and production builds with unique favicons

When you are working on a website, you may have the dev version and production version of the website open in the browser. It is easy to mix them up as the appearance of the browser tabs is identical. You end up playing a guessing game when you are switching tabs! π§

Letβs make our life easier and make them look unique by having a different favicon for each βbuild modeβ!

The code
Here is an abbreviated outline of the project:
ββ _data/
β ββ production.js
ββ _includes/
β ββ head.njk
ββ img
β ββ favicon-dev.svg
β ββ favicon.svg
ββ index.md
We will create a different image file for each build mode. SVG is the simplest format to use for a favicon. We will keep it simple and use a square with a different colour for each mode β red for dev, green for production.
<!-- img/favicon-dev.svg -->
<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
<rect width="100" height="100" fill="red"/>
</svg>
I outlined in a previous tutorial how to create a gloabl production flag to determine the build mode. This is found in _data/production.js. This gives us a production
boolean variable.
We have a head.njk template that covers the head
section of the document β all of a pageβs metadata. We will include this template in every page of the website. It is here that we specify a link
element to identify our favicon. We use the production
variable to reference the file that corresponds to the build mode:
- favicon-dev.svg for dev mode,
- favicon.svg for production mode.
<!-- _includes/head.njk -->
<head>
{% if production %}
<link rel="icon" href="/img/favicon.svg" type="image/svg+xml" sizes="any"/>
{% else %}
<link rel="icon" href="/img/favicon-dev.svg" type="image/svg+xml" sizes="any"/>
{% endif %}
<title>Home</title>
</head>
Thatβs the highlights of the code!
For your production website, you should include other image files for the favicon to satisfy the requirements of different browsers. The OG file format is ico. You can read this article to ensure your bases are cover.
Source code
You will find the code in the favicon-dev
subfolder of the https://github.com/robole/eleventy-tutorials repo.
Can you give the repo a star to indicate that this was a worthwhile tutorial please? π
Conclusion
This favicon trick will make it more obvious which version of a website you are looking at, or looking for in the browser. A boon for your dev experience. π€