Alert IE Users: Display Warning Message with Next.js

My Next.js application is experiencing issues on Internet Explorer.

When accessed using IE, the app displays a blank screen and generates syntax errors in the console.

Although it's understandable given that IE is being phased out, I want to prevent the problematic code from executing specifically for this browser.

Referencing this solution, I can identify if the user is using IE:

if (window.document.documentMode) {
  // IE detected

  document.write('IE is not supported. Please switch to a more modern browser!')
}

By implementing the code snippet above, users who use IE will be informed about why the site isn't functioning properly instead of encountering a blank screen.

However, there are two interrogations regarding this approach:

  1. Where should the aforementioned code be placed within Next.js?
  2. Is it feasible to terminate the application after running this code, or is there an alternative approach?

Any assistance on this matter would be highly appreciated.

Answer №1

If you want to display a message warning about using an outdated or unsupported browser in your Next.js app, it's best not to rely on the app itself as it may crash due to lack of polyfills on those browsers.

A better approach is to load a JavaScript script asynchronously that doesn't use ES5+ features, ensuring compatibility with all browsers without slowing down your app or increasing bundle size.

The earliest place to check the browser is in _document.js, which is rendered once on the first page request. You can either include this script from an external CDN or create a file called checkBrowser.js in the public folder.

Here is an example of how you can implement this:

pages/_document.js

export default class MyDocument extends Document {

  ...

  render() {
    return (
      <Html>
        <Head>
          <script async src="/scripts/checkBrowser.js" />
        </Head>

      ...

      </Html>
    )
  }
}

public/scripts/checkBrowser.js

// Check if the browser is Internet Explorer and redirect

if (window.document.documentMode) {

  window.location.replace('/outdated.html')
}

public/outdated.html

<html>
  <head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <title>Unsupported browser</title>
  </head>
  <body>
    <p>Internet Explorer (IE) is not supported. Please use a modern browser.</p>
  </body>
</html>

Credit: Script from CDN, Redirect approach, also @YuZhou for sharing those links

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Loading data from external URLs using AJAX and JSON

I am facing an issue, can someone please help? I have two websites and I want to send data between them. First website: var site_adres = $(location).attr('href'); var myArray = site_adres.split('/'); var site_adi_al = myArray[2]; ...

What is the process for converting a JavaScript Date into a Time Zone string?

Seeking a specific format for dates, I am looking to convert a Date into what I refer to as a 'TZ string': 2016-01-28T20:39:17.512Z I have experimented with different methods: > new Date("2016/02/28 00:19:58").toString() 'Sun Feb 28 20 ...

Utilizing JavaScript to adjust the width of an HTML element

Struggling to manipulate the position and size of a div tag using a JavaScript function, particularly confused about how to retrieve the current width of the div. Here is the function in question function socialExt() { document.getElementById("Left") ...

Using a custom filter in AngularJS allows for seamless data filtering directly from the initial dataset

My goal is to implement a custom filter that will allow me to filter data based on a search word. The scope attribute gets populated in the controller's scope as shown below: naApp.controller('naCareNewTicketCtrl', ['$scope', &apo ...

Designing a versatile pop-up window with jQuery or JavaScript that functions seamlessly across all web browsers

I've encountered an issue with my code where it displays a popup message correctly in Chrome, but when testing it on Firefox and Edge, it creates a strange box at the end of the page instead. Here is the code snippet I'm referring to: var iframe ...

Redirect Conditions in NextJS

One scenario: a blog with an extensive library of content cataloged by Google. Every piece is accessible via www.site.com/post1. When migrating this blog to NextJS, the posts are now located at www.site.com/blog/post1. Redirects using 301 have successful ...

Make sure to invoke the navigate() function within a React.useEffect() hook, rather than calling it during the initial rendering of

I am new to ReactJS. When the page initially loads, I need to check if the state is null, and if it is, redirect to a different login Page Below is the code for the component: export default function Addblousesalwar() { const navigate = useNavigate(); ...

Verify if a window is considered a Popup or not

This inquiry is distinct from the one at if window is popup, but it bears similarities. In my current project, I am working on an extension that injects scripts into all web pages. My challenge lies in determining whether the window is a popup or not. Pl ...

"Exploring the Synchronization Feature in Vue.js 2.3 with Element UI Dialog Components

Recently, I've encountered some changes while using Element UI with the latest release of Vue.js 2.3 In my project, a dialog should only be displayed if certain conditions are met: private.userCanManageUsers && private.pendingUsers.length > ...

Move my site content to the appropriate location

I am working on a single-paged website built with Jquery and HTML. The main content is housed within a div called #site-content, which has a width of 4400px. Each "page" is positioned within this div, with the first page starting at left: 0px, the second a ...

In VueJS and Quasar, what is the best way to retrieve the clicked item in order to pass it to the API?

Utilizing a codepen sample with local data, I am hoping it will work for troubleshooting purposes. However, in reality, I am using vuex and an API endpoint to source the data. Unfortunately, I cannot share the API details. The core functionality involves ...

The conceal feature doesn't appear to be functioning properly on jQuery Mobile

I am facing an issue with a small mobile app built using jQuery Mobile. Within a div, I have three buttons and other content. My goal is to hide these three buttons if the user's device is running on Windows (WP). The buttons are defined as follows: ...

Performing server operations within a client-side component using Next.js

I'm facing the challenge of needing to execute server actions on a client component because the shadcn/ui <Form> component necessitates it. I've been consulting documentation from shadcn/ui and Lucia. The shadcn/ui library utilizes react-ho ...

Tips for transferring element attributes to ng-show in AngularJS

I have a chart in my application that looks like this: <canvas id="line" class="chart chart-line" height="250" width="{{width_chart}}" chart-data="data" chart-labels="labels" chart-series="series" chart-options="options" chart-colors="colors" chart-dat ...

Updating the DOM through Long Polling allows us to maintain existing attributes in jQuery Plugin

UPDATE: The functionality is all set; the pushes are working. The only issue I'm facing is that each push resets the #load_info div to empty. Is there a way to retain the original content inside the div, even though the content will be an updated vers ...

Find the specific element that is visible within a customized viewport, such as a div or ul

Exploring the capabilities of the Viewport Selectors for jQuery plugin, here is a snippet of its source code: (function($) { $.belowthefold = function(element, settings) { var fold = $(window).height() + $(window).scrollTop(); return fold <= $ ...

What could be causing Next.js to re-render the entire page unnecessarily?

As a newcomer to Next.js, I am trying to develop an app where the header/navbar remains fixed at all times. Essentially, when the user navigates to different pages, only the main content should update without refreshing the navbar. Below is the code I have ...

Exploring Vue.js: Leveraging External JavaScript/JQuery Functions

I am currently working on developing a lightbox/modal feature using Vue.js. After making significant progress, I have encountered the need to utilize existing functions from another Javascript/Jquery file. Due to the complexity of these functions, rewritin ...

Shuffle the elements within each container in the DOM

Currently, I am focusing on designing a card-based layout where each card contains details about the product. One important detail is the phone number, which is displayed in the format 555-555-5555. My current goal is to clear the text value of the phone ...

Unexpected Results from WordPress Ajax Request

Currently, I am utilizing the snippets plugin in conjunction with Elementor. To implement an ajax function as a snippet, I have set it up like so: add_action( 'wp_ajax_get_slug_from_id', 'get_slug_from_id' ); add_action( 'wp_ajax_n ...