{"id":665,"date":"2021-06-23T19:05:40","date_gmt":"2021-06-23T17:05:40","guid":{"rendered":"http:\/\/strawberrycode.com\/blog\/?p=665"},"modified":"2021-06-23T19:05:42","modified_gmt":"2021-06-23T17:05:42","slug":"favicon-dev-prod","status":"publish","type":"post","link":"https:\/\/strawberrycode.com\/blog\/favicon-dev-prod\/","title":{"rendered":"Different Favicon for Dev and Prod"},"content":{"rendered":"\n<p>Having different favicons for your development and production environments can be extremely useful: you can see in a split second if you are working on the right tab in your browser.<\/p>\n\n\n\n<p>If you already refreshed, refreshed and refreshed again a page for 5 minutes&#8230; wondering why your changes don&#8217;t show up, and realizing that you were refreshing the live website instead of the local one, raise your hand &#x270b;.<\/p>\n\n\n\n<p>This won&#8217;t happen anymore with this quick JavaScript hack to display a different favicon depending on your environment.<\/p>\n\n\n\n<h3>1. Create two different favicon images<\/h3>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" width=\"312\" height=\"110\" src=\"http:\/\/strawberrycode.com\/blog\/wp-content\/uploads\/2021\/06\/favicons_dev_prod.png\" alt=\"\" class=\"wp-image-666\" srcset=\"https:\/\/strawberrycode.com\/blog\/wp-content\/uploads\/2021\/06\/favicons_dev_prod.png 312w, https:\/\/strawberrycode.com\/blog\/wp-content\/uploads\/2021\/06\/favicons_dev_prod-300x106.png 300w\" sizes=\"(max-width: 312px) 100vw, 312px\" \/><figcaption>favicon.png and favicon_dev.png<\/figcaption><\/figure><\/div>\n\n\n\n<p>&#x1f449; <strong>Note:<\/strong> a favicon image should be square (128x128px is okay), otherwise it will be distorted.<\/p>\n\n\n\n<h3>1. Use the following code<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"html\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ In the Html file header\n&lt;link rel=\"icon\" type=\"image\/png\" href=\"favicon.png\"><\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ in your JavaScript file\nif (location.hostname === \"localhost\" \n || location.hostname === \"127.0.0.1\" \n || location.hostname.search(\"local\") != -1) {\n    $(\"link[rel=icon]\").attr(\"href\", \"favicon_dev.png\");\n} else {\n    $(\"link[rel=icon]\").attr(\"href\", \"favicon.png\");\n}<\/pre>\n\n\n\n<p>&#x1f449; <strong>Note:<\/strong> the following test <code>location.hostname.search(\"local\") != -1<\/code> matches the local aliases like <code>http:\/\/mywebsite.com.local<\/code>. Have a look at <a href=\"http:\/\/strawberrycode.com\/blog\/alias-localhost\/\" target=\"_blank\" rel=\"noreferrer noopener\">this article on how to make an alias for a local website<\/a> for more details.<\/p>\n\n\n\n<p>Of course, using favicons can depend on your working environment (<a href=\"https:\/\/css-tricks.com\/different-favicon-for-development\/\" target=\"_blank\" rel=\"noreferrer noopener\">WordPress<\/a>, Symphony, React, &#8230;) and you can also use environment variables to do a cleaner version of this hack &#x1f609;<\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>If you found it useful, feel free to\u00a0<a href=\"https:\/\/www.buymeacoffee.com\/strawberrycode\" target=\"_blank\" rel=\"noreferrer noopener\">buy me a coffee (or tea)<\/a>\u00a0to support the blog &#x1f642;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Having different favicons for your development and production environments can be extremely useful: you can see in a split second if you are working on the right tab in your browser. If you already refreshed, refreshed and refreshed again a page for 5 minutes&#8230; wondering why your changes don&#8217;t show up, and realizing that you were refreshing the live website instead of the local one, raise your hand &#x270b;. This won&#8217;t happen anymore with this quick JavaScript hack to display a different favicon depending on your environment. 1. Create two different favicon images &#x1f449; Note: a favicon image should be &hellip; <a href=\"https:\/\/strawberrycode.com\/blog\/favicon-dev-prod\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Different Favicon for Dev and Prod<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":669,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[115,86],"tags":[131,125,120],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v16.1.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Different Favicon for Dev and Prod - StrawberryCode<\/title>\n<meta name=\"description\" content=\"Learn how to set different favicons to differentiate your development and production environments in a split second\" \/>\n<link rel=\"canonical\" href=\"https:\/\/strawberrycode.com\/blog\/favicon-dev-prod\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Different Favicon for Dev and Prod - StrawberryCode\" \/>\n<meta property=\"og:description\" content=\"Learn how to set different favicons to differentiate your development and production environments in a split second\" \/>\n<meta property=\"og:url\" content=\"https:\/\/strawberrycode.com\/blog\/favicon-dev-prod\/\" \/>\n<meta property=\"og:site_name\" content=\"StrawberryCode\" \/>\n<meta property=\"article:published_time\" content=\"2021-06-23T17:05:40+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-06-23T17:05:42+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/strawberrycode.com\/blog\/wp-content\/uploads\/2021\/06\/favicons_dev_prod_cover.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1051\" \/>\n\t<meta property=\"og:image:height\" content=\"550\" \/>\n<meta name=\"twitter:label1\" content=\"Estimated reading time\">\n\t<meta name=\"twitter:data1\" content=\"2 minutes\">\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"http:\/\/strawberrycode.com\/blog\/#website\",\"url\":\"http:\/\/strawberrycode.com\/blog\/\",\"name\":\"StrawberryCode\",\"description\":\"Fruit for Thought\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"http:\/\/strawberrycode.com\/blog\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-GB\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/strawberrycode.com\/blog\/favicon-dev-prod\/#primaryimage\",\"inLanguage\":\"en-GB\",\"url\":\"https:\/\/strawberrycode.com\/blog\/wp-content\/uploads\/2021\/06\/favicons_dev_prod_cover.png\",\"contentUrl\":\"https:\/\/strawberrycode.com\/blog\/wp-content\/uploads\/2021\/06\/favicons_dev_prod_cover.png\",\"width\":1051,\"height\":550,\"caption\":\"Favicon dev prod\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/strawberrycode.com\/blog\/favicon-dev-prod\/#webpage\",\"url\":\"https:\/\/strawberrycode.com\/blog\/favicon-dev-prod\/\",\"name\":\"Different Favicon for Dev and Prod - StrawberryCode\",\"isPartOf\":{\"@id\":\"http:\/\/strawberrycode.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/strawberrycode.com\/blog\/favicon-dev-prod\/#primaryimage\"},\"datePublished\":\"2021-06-23T17:05:40+00:00\",\"dateModified\":\"2021-06-23T17:05:42+00:00\",\"author\":{\"@id\":\"http:\/\/strawberrycode.com\/blog\/#\/schema\/person\/c328d959959928f47281d7a0ec779e2a\"},\"description\":\"Learn how to set different favicons to differentiate your development and production environments in a split second\",\"breadcrumb\":{\"@id\":\"https:\/\/strawberrycode.com\/blog\/favicon-dev-prod\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/strawberrycode.com\/blog\/favicon-dev-prod\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/strawberrycode.com\/blog\/favicon-dev-prod\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"item\":{\"@type\":\"WebPage\",\"@id\":\"http:\/\/strawberrycode.com\/blog\/\",\"url\":\"http:\/\/strawberrycode.com\/blog\/\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"position\":2,\"item\":{\"@type\":\"WebPage\",\"@id\":\"https:\/\/strawberrycode.com\/blog\/favicon-dev-prod\/\",\"url\":\"https:\/\/strawberrycode.com\/blog\/favicon-dev-prod\/\",\"name\":\"Different Favicon for Dev and Prod\"}}]},{\"@type\":\"Person\",\"@id\":\"http:\/\/strawberrycode.com\/blog\/#\/schema\/person\/c328d959959928f47281d7a0ec779e2a\",\"name\":\"StrawberryCode\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","_links":{"self":[{"href":"https:\/\/strawberrycode.com\/blog\/wp-json\/wp\/v2\/posts\/665"}],"collection":[{"href":"https:\/\/strawberrycode.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/strawberrycode.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/strawberrycode.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/strawberrycode.com\/blog\/wp-json\/wp\/v2\/comments?post=665"}],"version-history":[{"count":3,"href":"https:\/\/strawberrycode.com\/blog\/wp-json\/wp\/v2\/posts\/665\/revisions"}],"predecessor-version":[{"id":670,"href":"https:\/\/strawberrycode.com\/blog\/wp-json\/wp\/v2\/posts\/665\/revisions\/670"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/strawberrycode.com\/blog\/wp-json\/wp\/v2\/media\/669"}],"wp:attachment":[{"href":"https:\/\/strawberrycode.com\/blog\/wp-json\/wp\/v2\/media?parent=665"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/strawberrycode.com\/blog\/wp-json\/wp\/v2\/categories?post=665"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/strawberrycode.com\/blog\/wp-json\/wp\/v2\/tags?post=665"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}