<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>nextjs &amp;mdash; ruudje</title>
    <link>https://ruud.je/tag:nextjs</link>
    <description>Software developer and retro geek based in the Netherlands</description>
    <pubDate>Thu, 23 Apr 2026 14:04:54 +0000</pubDate>
    <item>
      <title>Redirect IE11 users in NextJS</title>
      <link>https://ruud.je/redirect-ie11-users-in-nextjs</link>
      <description>&lt;![CDATA[#javascript #nextjs&#xA;&#xA;A while ago, a client I work for decided after a lot of discussions to finally drop support for IE11. Hurray!&#xA;&#xA;Later, they decided to a show a nice page with an explanation to anyone who might still be using IE11 and encourage them to download a more modern browser.&#xA;&#xA;But because we dropped support a while ago, we didn&#39;t test in IE11 anymore, and so any page we tried to load didn&#39;t work. Trying to make the pages work would be a waste of time, because in time they might stop working again.&#xA;&#xA;So we decided to redirect all IE11 users to a simple static page.&#xA;&#xA;!--more--&#xA;&#xA;Redirects in NextJS&#xA;&#xA;Since version 9.5 it is possible to declare your redirects in next.config.js.&#xA;&#xA;The redirects property accepts an async function, that returns an array of redirects.&#xA;&#xA;The redirect in this array goes from the homepage to /iewarning.html (which is a file served from the public folder), and is set to be non-permanent, meaning that the server will return a 302 HTTP status code. If permanent is set to true, a 301 code is returned instead.&#xA;&#xA;module.exports = {&#xA;  redirects: async () =  {&#xA;    return [&#xA;      {&#xA;        source: &#39;/&#39;,&#xA;        permanent: false,&#xA;        destination: &#39;/iewarning.html&#39;,&#xA;      },&#xA;    ]&#xA;  },&#xA;}&#xA;&#xA;After restarting NextJS, the homepage should redirect you to the static file. But right now this will happen in all browsers, which is not what we want.&#xA;&#xA;Matching the User-Agent header&#xA;&#xA;So what if we only want to redirect users that are using IE11?&#xA;&#xA;That&#39;s easy! Redirects also accept a has property, that contains a list of &#34;things&#34; the request should contain. These can be headers, cookies or query parameters.&#xA;&#xA;We will be checking against a header. the User-Agent. All IE11 user-agents contain the word Trident) in their user-agent, so we can simply use a regex-like string to check if the header contains that word.&#xA;&#xA;module.exports = {&#xA;  redirects: async () =  {&#xA;    return [&#xA;      {&#xA;        source: &#39;/&#39;,&#xA;        has: [&#xA;          {&#xA;            type: &#39;header&#39;,&#xA;            key: &#39;User-Agent&#39;,&#xA;            value: &#39;(.Trident.)&#39;,&#xA;          },&#xA;        ],&#xA;        permanent: false,&#xA;        destination: &#39;/iewarning.html&#39;,&#xA;      },&#xA;    ]&#xA;  },&#xA;}&#xA;&#xA;Restart NextJS again, and go to the homepage in IE11. It will still redirect you, while other browsers will not.&#xA;&#xA;But right now the redirect only triggers on the homepage. You&#39;ll want this to trigger on all pages, which we&#39;ll do next.&#xA;&#xA;Site-wide redirect&#xA;&#xA;In order to trigger a redirect on all pages, we can make use of a regex-like string again.&#xA;&#xA;module.exports = {&#xA;  redirects: async () =  {&#xA;    return [&#xA;      {&#xA;        source: &#39;/:path((?!ie11warning.html$).)&#39;,&#xA;        has: [&#xA;          {&#xA;            type: &#39;header&#39;,&#xA;            key: &#39;user-agent&#39;,&#xA;            value: &#39;(.Trident.*)&#39;,&#xA;          },&#xA;        ],&#xA;        permanent: false,&#xA;        destination: &#39;/ie11warning.html&#39;,&#xA;      },&#xA;    ]&#xA;  },&#xA;}&#xA;&#xA;Restart NextJS one more time and now the redirect will trigger on all pages, except /ie_warning.html. The exception is required to prevent a redirect loop.&#xA;&#xA;Conclusion&#xA;&#xA;As you can see, it is quite easy to setup a redirect for a specific browser. Personally I had some trouble with the regex-like strings at first, but then again, regex is difficult anyway.&#xA;&#xA;Hope this was helpful!&#xA;]]&gt;</description>
      <content:encoded><![CDATA[<p><a href="https://ruud.je/tag:javascript" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">javascript</span></a> <a href="https://ruud.je/tag:nextjs" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">nextjs</span></a></p>

<p>A while ago, a client I work for decided after a lot of discussions to finally drop support for IE11. <em>Hurray!</em></p>

<p>Later, they decided to a show a nice page with an explanation to anyone who might still be using IE11 and encourage them to download a more modern browser.</p>

<p>But because we dropped support a while ago, we didn&#39;t test in IE11 anymore, and so any page we tried to load didn&#39;t work. Trying to make the pages work would be a waste of time, because in time they might stop working again.</p>

<p>So we decided to redirect all IE11 users to a simple static page.</p>



<h2 id="redirects-in-nextjs" id="redirects-in-nextjs">Redirects in NextJS</h2>

<p>Since version 9.5 it is possible to <a href="https://nextjs.org/docs/api-reference/next.config.js/redirects" rel="nofollow">declare your redirects</a> in <code>next.config.js</code>.</p>

<p>The <code>redirects</code> property accepts an async function, that returns an array of redirects.</p>

<p>The redirect in this array goes from the homepage to <code>/ie_warning.html</code> (which is a file served from the <code>public</code> folder), and is set to be non-permanent, meaning that the server will return a <code>302</code> HTTP status code. If <code>permanent</code> is set to <code>true</code>, a <code>301</code> code is returned instead.</p>

<pre><code class="language-typescript">module.exports = {
  redirects: async () =&gt; {
    return [
      {
        source: &#39;/&#39;,
        permanent: false,
        destination: &#39;/ie_warning.html&#39;,
      },
    ]
  },
}
</code></pre>

<p>After restarting NextJS, the homepage should redirect you to the static file. But right now this will happen in all browsers, which is not what we want.</p>

<h2 id="matching-the-user-agent-header" id="matching-the-user-agent-header">Matching the User-Agent header</h2>

<p>So what if we only want to redirect users that are using IE11?</p>

<p>That&#39;s easy! Redirects also accept a <a href="https://nextjs.org/docs/api-reference/next.config.js/redirects#header-cookie-and-query-matching" rel="nofollow">has</a> property, that contains a list of “things” the request should contain. These can be headers, cookies or query parameters.</p>

<p>We will be checking against a header. the <code>User-Agent</code>. All IE11 user-agents contain the word <a href="https://en.wikipedia.org/wiki/Trident_(software)" rel="nofollow">Trident</a> in their user-agent, so we can simply use a regex-like string to check if the header contains that word.</p>

<pre><code class="language-typescript">module.exports = {
  redirects: async () =&gt; {
    return [
      {
        source: &#39;/&#39;,
        has: [
          {
            type: &#39;header&#39;,
            key: &#39;User-Agent&#39;,
            value: &#39;(.*Trident.*)&#39;,
          },
        ],
        permanent: false,
        destination: &#39;/ie_warning.html&#39;,
      },
    ]
  },
}
</code></pre>

<p>Restart NextJS again, and go to the homepage in IE11. It will still redirect you, while other browsers will not.</p>

<p>But right now the redirect only triggers on the homepage. You&#39;ll want this to trigger on all pages, which we&#39;ll do next.</p>

<h2 id="site-wide-redirect" id="site-wide-redirect">Site-wide redirect</h2>

<p>In order to trigger a redirect on all pages, we can make use of a regex-like string again.</p>

<pre><code class="language-typescript">module.exports = {
  redirects: async () =&gt; {
    return [
      {
        source: &#39;/:path((?!ie11_warning.html$).*)&#39;,
        has: [
          {
            type: &#39;header&#39;,
            key: &#39;user-agent&#39;,
            value: &#39;(.*Trident.*)&#39;,
          },
        ],
        permanent: false,
        destination: &#39;/ie11_warning.html&#39;,
      },
    ]
  },
}
</code></pre>

<p>Restart NextJS one more time and now the redirect will trigger on all pages, except <code>/ie_warning.html</code>. The exception is required to prevent a redirect loop.</p>

<h2 id="conclusion" id="conclusion">Conclusion</h2>

<p>As you can see, it is quite easy to setup a redirect for a specific browser. Personally I had some trouble with the regex-like strings at first, but then again, regex is difficult anyway.</p>

<p>Hope this was helpful!</p>
]]></content:encoded>
      <guid>https://ruud.je/redirect-ie11-users-in-nextjs</guid>
      <pubDate>Wed, 16 Jun 2021 22:36:30 +0000</pubDate>
    </item>
  </channel>
</rss>