webpack - Intl Polyfill + Next.js SSR 应用程序
问题描述
如何在 Next.js 应用程序上填充 intl?有的中文浏览器还没有,我们用react-intl,需要Intl。
解决方案
Next.js 中的 polyfills(一般)
Next.js 通常有一些 polyfill 示例:
https ://github.com/vercel/next.js/tree/canary/examples/with-polyfills
Next.js 建议在<App>
组件内部或使用 polyfill 的单个组件中导入所需的 polyfill。
例如:
// /pages/_app.js
import 'polyfill_some_feature';
function MyApp({ Component, pageProps }){
return <Component { ...pageProps } />
}
export default MyApp
用于Intl
API的 polyfill
formatjs / react-intl为API提供了一些polyfillsIntl
。
您需要的功能,如Intl.getCanonicalLocales
, Intl.Locale
, Intl.PluralRules
, ... 必须单独导入。
请注意,polyfill 相互依赖,因此调用它们的顺序很重要。
例如 polyfill 用于Intl.getCanonicalLocales
检查 if typeof Intl === 'undefined'
,但 polyfillIntl.Locale
依赖于Intl
已经存在。
这是他们的一个例子(这个例子是为了实现getCanonicalLocales
):
https ://formatjs.io/docs/polyfills/intl-getcanonicallocales/#dynamic-import--capability-detection
npm i @formatjs/intl-getcanonicallocales
import { shouldPolyfill } from '@formatjs/intl-getcanonicallocales/should-polyfill';
async function polyfill() {
if( shouldPolyfill() ){
await import('@formatjs/intl-getcanonicallocales/polyfill');
}
}
推荐阅读
- ios - Swift 故事板 - 使用 calloutaccessorycontroltapped 披露按钮在 MapView 中创建转场
- python - Plotly Legent Aa 标签如何删除它们
- python - 测试最佳神经元数量 - Keras/Tensorflow
- ios - 在 UserDefaults 中保存项目数组 - iOS 12
- reactjs - React Semantic UI - 将图标放置在菜单项中的文本左侧
- ajax - Keycloak:ajax调用过期后刷新访问令牌
- android - Android - ViewPager 动画(纸叠)
- bash - 如何在 TCP 会话中运行脚本
- javascript - Puppeteer JS 从选择器生成 PDF
- excel - DynamicJasper:需要列仅跨越 Excel 中的一个单元格