查找IP地址后,我正在try 根据用户的国家代码将流量转发到特定的slug.

它在本地工作得很好,但当我部署到Netliify时就不工作了.

export default defineNuxtRouteMiddleware(async (to, from) => {
  const targetPathDe = '/de-de'
  const targetPathEn = '/de-en'

  // Prevent middleware from redirecting if already on the target path
  if (to.path === targetPathDe || to.path === targetPathEn) {
    return
  }

  const ipApiUrl = 'http://ip-api.com/json/?fields=countryCode'

  try {
    // Use native fetch to get IP data
    const response = await fetch(ipApiUrl)
    const ipData = await response.json()

    // Check the country code from IP data
    if (ipData.countryCode === 'DE') {
      return navigateTo(targetPathDe)
    } else {
      return navigateTo(targetPathEn)
    }
  } catch (error) {
    console.error('Error fetching IP data:', error)
    return navigateTo(targetPathEn)
  }
})

在本地,我的运行是npm run dev,Netliify的运行是npm run build.

推荐答案

OP considered using the geolocation edge function for now.
It is easier to implement and probably more precise too!

Vue.js相关问答推荐

Vue3多姆参考已安装的添加EventButtButtButtButtButtButtButtButtButtButtButtButtButtButtButtButtButtButtButtButtButtButtButtButtButtButtButtButtButtButtButtButtButtButtButtButtButtButtButtButtButtButtButtButtButtButtButtButtButtButtButtButtButtButtButtButtButtButtButt

如何使用composition api v-model Select 框获取其他JSON 值?

Nuxt3 / Vue3 - 当属性更改时如何动态更改渲染组件?

Nuxt3: 安装vue3-spinner (error: require is not defined in ES module scope)

Vue:在表格中显示嵌套数据

如何将 vuejs 项目部署到 heroku

为什么在 Vue.js 中使用 `var` 关键字?

在 Vuejs 中Watch观察 window.scrollY 的变化

vuejs中通过变量动态调用方法

错误 [ERR_UNSUPPORTED_ESM_URL_SCHEME]:默认 ESM 加载器仅支持文件和数据 URL - Vue 3

VueJS 通过渲染传递数据

如何在生产模式下为 chrome 扩展启用 Vue devtools?

VueJS 2 在多个组件上debounce

克隆元素并附加到 DOM 的正确方法

从子组件 Vue 更新父模型

Vue中的嵌套循环

在 vue js 中的何处以及如何存储 API 端点?

使用 CloudFront 部署在 S3 上的 VueJS 应用程序的指定的密钥不存在

vuejs 中缺少 webpack 配置

如何删除 vue cli 2?