英文:
Different date output in development and production build on Next.js
问题
有人可以帮助我解决问题吗?
我从本地开发中获取API,输出显示我想要的内容。它显示了我的当前位置、时区等。
当我将我的应用部署到Vercel并访问我的API路由时,显示的输出与我的开发环境不同。
我尝试了手动使用moment-timezone获取当前日期和时区。将其部署到Vercel,它也显示不同的输出。
英文:
Can anyone help me with my problem?
I fetch API from local dev, the output shown as I wanted. It is showing my current location, timezone, etc.
/ 20230713184522
// http://localhost:3000/api/onefootball/mylocation
{
"continent": "Asia",
"country": "Indonesia",
"zipCode": null,
"timezone": "Asia/Jakarta",
"gmt": "(GMT+07:00) Jakarta",
"languages": "id",
"countryTLD": ".id",
"countryISO2": "ID",
"countryISO3": "IDN",
}
When i deploy my app to vercel and access my API route, is show different output with my development.
// 20230713185444
// https://xxx.xxx.dev/api/onefootball/mylocation
{
"continent": "North America",
"country": "United States",
"zipCode": "95141",
"timezone": "America/Los_Angeles",
"gmt": "(GMT-10:00) Hawaii Time",
"languages": "en",
"countryTLD": ".us",
"countryISO2": "US",
"countryISO3": "USA",
}
I have tried manually to get current date, timezone using moment-timezone. Deploy it to vercel, and it also show different output.
答案1
得分: 1
这种差异是由于在使用IP地址时地理定位的工作方式不同。
当您在本地运行应用程序时,它使用您的实际IP地址并提供正确的地理定位详细信息(大陆、国家、时区等)。
然而,当您将应用程序部署到Vercel时,服务器位于不同的位置,因此使用不同的IP地址。在您的情况下,服务器似乎位于美国,因此在获取地理定位详细信息时,它会获取服务器的位置(美国)而不是您的实际位置。
如果您只想确定用户的时区,浏览器的JavaScript环境可以访问用户的系统时间并确定时区。
const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
console.log(timezone);
或者
const now = new Date();
console.log("Time: " + now.getTime());
将此信息与您的请求一起发送,然后从时区/日期中提取您需要的信息。
英文:
This difference is due to how geolocation works when using IP addresses.
When you are running your app locally, it uses your actual IP address and provides your correct geolocation details (continent, country, timezone, etc.).
However, when you deploy your app to Vercel, the server is located in a different place, hence it uses a different IP address. In your case, the server appears to be in the United States, so when it fetches the geolocation details, it gets the details for the location of the server (US) instead of your actual location.
If you just want to determine the user's timezone, the browser's JavaScript environment has access to the user's system time and can determine the timezone.
const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
console.log(timezone);
or
const now = new Date();
console.log("Time: " + now.getTime());
Send this information along with your request and now extract the piece of information you need from timezone / date in your api
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论