判断当前环境是否为浏览器

代码编程
2024/03/19(更新于2025/10/24
预计阅读时长 1 分钟
判断当前环境是否为浏览器,通常在 SSR 场景使用
export const isBrowser = () => {
  // 代码来自:https://ahooks.js.org/zh-CN/guide/blog/ssr
  /* eslint-disable @typescript-eslint/prefer-optional-chain */
  return !!(
    typeof window !== 'undefined' &&
    window.document &&
    window.document.createElement
  );
};

参考链接