查找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
.