Unverified Commit 9e75162a authored by Fabian Mastenbroek's avatar Fabian Mastenbroek Committed by GitHub

[examples/with-react-intl] Fix issues with Intl.js polyfill (#11806)

* Add workaround for Intl.js RegEx issue

This change adds a workaround for an issue with the Intl.js polyfill
library where it instantiates an invalid regular expression.

Relevant issues:
https://github.com/andyearnshaw/Intl.js/issues/270
https://github.com/andyearnshaw/Intl.js/issues/256
https://github.com/andyearnshaw/Intl.js/issues/306

Until this issue is fixed upstream, we use a workaround in the example.

Fixes: #11777

* Do not load Intl.js polyfill by default on Node

This change updates the `with-react-intl` example to prevent it from
polyfilling `Intl` in a Node environment when it is not needed.
parent e686545c
......@@ -15,6 +15,7 @@
"full-icu": "^1.3.0",
"glob": "^7.1.4",
"intl": "^1.2.5",
"intl-locales-supported": "1.8.4",
"next": "latest",
"react": "^16.9.0",
"react-dom": "^16.9.0",
......
const { basename } = require('path')
const glob = require('glob')
const areIntlLocalesSupported = require('intl-locales-supported').default
// Get the supported languages by looking for translations in the `lang/` dir.
const supportedLanguages = glob
.sync('./lang/*.json')
.map(f => basename(f, '.json'))
// Polyfill Node with `Intl` that has data for all locales.
// See: https://formatjs.io/guides/runtime-environments/#server
const IntlPolyfill = require('intl')
Intl.NumberFormat = IntlPolyfill.NumberFormat
Intl.DateTimeFormat = IntlPolyfill.DateTimeFormat
if (global.Intl) {
// Determine if the built-in `Intl` has the locale data we need.
if (!areIntlLocalesSupported(supportedLanguages)) {
// `Intl` exists, but it doesn't have the data we need, so load the
// polyfill and patch the constructors we need with the polyfills.
const IntlPolyfill = require('intl')
Intl.NumberFormat = IntlPolyfill.NumberFormat
Intl.DateTimeFormat = IntlPolyfill.DateTimeFormat
Intl.__disableRegExpRestore = IntlPolyfill.__disableRegExpRestore
}
} else {
// No `Intl`, so use and load the polyfill.
global.Intl = require('intl')
}
// Fix: https://github.com/zeit/next.js/issues/11777
// See related issue: https://github.com/andyearnshaw/Intl.js/issues/308
if (Intl.__disableRegExpRestore) {
Intl.__disableRegExpRestore()
}
// Polyfill DOMParser for **pre-v4** react-intl used by formatjs.
// Only needed when using FormattedHTMLMessage. Make sure to install `xmldom`.
......@@ -11,10 +37,8 @@ Intl.DateTimeFormat = IntlPolyfill.DateTimeFormat
// global.DOMParser = DOMParser
const { readFileSync } = require('fs')
const { basename } = require('path')
const { createServer } = require('http')
const accepts = require('accepts')
const glob = require('glob')
const next = require('next')
const port = parseInt(process.env.PORT, 10) || 3000
......@@ -22,11 +46,6 @@ const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev })
const handle = app.getRequestHandler()
// Get the supported languages by looking for translations in the `lang/` dir.
const supportedLanguages = glob
.sync('./lang/*.json')
.map(f => basename(f, '.json'))
// We need to expose React Intl's locale data on the request for the user's
// locale. This function will also cache the scripts by lang in memory.
const localeDataCache = new Map()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment