Skip to content

Commit

Permalink
Do not load Intl.js polyfill by default on Node
Browse files Browse the repository at this point in the history
This change updates the `with-react-intl` example to prevent it from
polyfilling `Intl` in a Node environment when it is not needed.
  • Loading branch information
fabianishere committed Apr 10, 2020
1 parent 5042fbd commit 8cb6a86
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
1 change: 1 addition & 0 deletions examples/with-react-intl/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
37 changes: 25 additions & 12 deletions examples/with-react-intl/server.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,33 @@
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 (IntlPolyfill.__disableRegExpRestore) {
IntlPolyfill.__disableRegExpRestore()
if (Intl.__disableRegExpRestore) {
Intl.__disableRegExpRestore()
}

// Polyfill DOMParser for **pre-v4** react-intl used by formatjs.
Expand All @@ -17,22 +37,15 @@ if (IntlPolyfill.__disableRegExpRestore) {
// 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
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()
Expand Down

0 comments on commit 8cb6a86

Please sign in to comment.