我为这个问题找到的解决方案是删除Next.js默认的polyfill
array.flat
在较旧的浏览器上,因为它与
jQUery.replaceWith()
。
我使用的应用程序
rooks library
和
react-device-detect
。
import { useDidMount } from 'rooks';
import {
browserVersion,
isChrome,
isSafari,
isFirefox,
} from 'react-device-detect';
const MyPage: React.FC<PropTypes> = ({ ...props }) => {
useDidMount(() => {
/*
Remove NextJS default polyfill for array.flat as it will
error using jQuery.replaceWith() which is used in the package
we are using. jQuery will handle support for browsers which
do not support array.flat.
*/
// Stats from https://caniuse.com/?search=array.flat
const minimumChromeArrayFlatVersion = '69';
const minimumSafariArrayFlatVersion = '12';
const minimumFirefoxArrayFlatVersion = '62';
const isChromeCompatible =
isChrome && browserVersion >= minimumChromeArrayFlatVersion;
const isSafariCompatible =
isSafari && browserVersion >= minimumSafariArrayFlatVersion;
const isFirefoxCompatible =
isFirefox && browserVersion >= minimumFirefoxArrayFlatVersion;
const isTargetedBrowser = isChrome || isSafari || isFirefox;
const isBrowserCompatible =
isChromeCompatible || isSafariCompatible || isFirefoxCompatible;
if (isTargetedBrowser && !isBrowserCompatible) {
Array.prototype.flat = undefined;
}
});
return <div>...</div>;
}