Skip to content

Instantly share code, notes, and snippets.

@rosskevin
Last active July 28, 2022 06:20
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rosskevin/5cfd78c73a10ca1989696350d76ea3c1 to your computer and use it in GitHub Desktop.
Save rosskevin/5cfd78c73a10ca1989696350d76ea3c1 to your computer and use it in GitHub Desktop.
nginx map for outdated or unsupported browser redirect based on browserslist output
# include in the server section of the conf.
# ---------------------------------------------------------------
# If we perform a rewrite on every immediately, images won't be
# displayed, so we have to avoid the rewrite for specific files
# in support of displaying the outdated page.
#
set $browser_unsupported_rewrite do_not_perform;
if ($outdated){
set $browser_unsupported_rewrite perform;
}
# rewrite images used on page to png instead of svg for really outdated browsers
if ($outdated = 2){
rewrite ^(/error/)(.*).svg$ $1$2.png last;
}
if ($uri ~* \.(jpeg|jpg|png|gif|svg|css)$) {
set $browser_unsupported_rewrite do_not_perform;
}
if ($browser_unsupported_rewrite = perform) {
rewrite ^ /error/browser-unsupported.html;
break;
}
# include in the http section of the conf.
# originally from https://serverfault.com/a/617223/327530
# Use browserslist to generate a current list and match the regexes below to match outdated browser ranges.
# @see https://github.com/browserslist/browserslist
# "production": [
# ">0.2%",
# "not dead",
# "not op_mini all",
# "not ie < 100",
# "not edge < 19"
# ],
# yarn browserslist
# blacklist specific browser version ranges that are outdated or simply unsupported.
# @see https://developers.whatismybrowser.com/useragents/explore/ for sample user agents
map $http_user_agent $outdated {
default 0;
# 1 - outdated but supports svg
# 2 - outdated so much that we need to rewrite svg to png
# -------------------------------------------
# Safari
#
# Allow:
# safari 12
# safari 11.1
# Mozilla/5.0 (iPad; CPU OS 12_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/5.1 Mobile/15E148 Safari/604.1
"~AppleWebKit.*Version/[0-9]\..*Safari" 2;
"~AppleWebKit.*Version/10.*Safari" 2;
"~AppleWebKit.*Version/11\.0.*Safari" 1;
# -------------------------------------------
# ie 11
#
# Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; .NET4.0C; .NET4.0E; rv:11.0) like Gecko
# https://github.com/faisalman/ua-parser-js/blob/master/src/ua-parser.js#L264
"~Trident/.*" 2;
# -------------------------------------------
# ie edge EdgeHTML (abandoned for ie edge Chromium)
#
# Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/17.17134
"~Edge/.*" 1;
# -------------------------------------------
# ie edge Chromium (allowed)
# Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.18 Safari/537.36 Edg/75.0.139.4
# -------------------------------------------
# Chrome
#
# Allow:
# chrome 73
# chrome 72
# chrome 71
# chrome 70
# chrome 69
# chrome 63
# chrome 61
# chrome 49
# Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36
"~Chrome/[0-9]\." 2;
"~Chrome/[0-3][0-9]\." 2;
"~Chrome/4[0-8]\." 1;
# -------------------------------------------
# Firefox
#
# Allow:
# firefox 66
# firefox 65
# firefox 52
# Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:67.0) Gecko/20100101 Firefox/67.0
"~Mozilla.*Firefox/[1-9]\." 2;
"~Mozilla.*Firefox/[0-4][0-9]\." 1;
"~Mozilla.*Firefox/5[0-1]\." 1;
# -------------------------------------------
# Opera
#
# Allow:
# opera 58
# Opera/9.80 (X11; Linux x86_64; U; fr) Presto/2.9.168 Version/11.50
"~Opera.*Version/[0-9].*" 2;
# Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36 OPR/55.0.2994.61
"~OPR/[0-1]\." 2;
"~OPR/[0-1][0-4]\." 2;
"~OPR/5[0-7]\." 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment