Vidorra

Vidorra

主站:https://blog.vidorra.life

Fix the error "globalThis is not defined" on the browser side.

Read the original article.

Today during development, the production operations and maintenance personnel reported that the page was blank and they couldn't access the system. Upon investigation, I found that it was an error reported by my print-template-designer, which stated that globalThis is not defined.

Actually, I think I have encountered similar errors before, usually due to a low version of the browser.

I checked on Can I Use and found that globalThis is not supported in versions of Chrome prior to 70.

So there are two solutions:

  • Ask the customer to upgrade their browser version.
  • Upgrade the front-end to be compatible with lower versions.

In the end, I advised the customer to upgrade their browser version. For Windows 7, the latest version 108 should be sufficient. (By the way, Chrome stopped supporting Windows 7 this year, so 108 should be the last version.)

In terms of code, how can we solve this issue? Just add the following code to the head section.

<head>
  <script>
    this.globalThis || (this.globalThis = this)
  </script>
</head>

Also, let's understand what globalThis is:

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.