deploy: secure weather caching

This commit is contained in:
2026-05-27 13:44:22 +00:00
parent 46cd4f77c9
commit ac6ca6d1b3
2 changed files with 47 additions and 58 deletions
+8 -57
View File
@@ -240,67 +240,19 @@
}
async function initWeather() {
// 1. Get coordinates using IP API fallback or direct Geolocation
try {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(
async (pos) => {
// Successfully got GPS coordinates
await fetchForecast(pos.coords.latitude, pos.coords.longitude, "Local Location");
},
async () => {
// Denied or failed - fall back to IP lookup
await fetchViaIP();
},
{ timeout: 5000 }
);
} else {
await fetchViaIP();
}
} catch (e) {
document.getElementById('loading-text').textContent = "⚠️ Unable to load weather. Check internet connection.";
}
}
async function fetchViaIP() {
try {
document.getElementById('loading-text').textContent = "🌐 Looking up location via IP...";
const resp = await fetch("https://freeipapi.com/api/json");
document.getElementById('loading-text').textContent = "📊 Loading local weather forecasts...";
const resp = await fetch("weather-data.json");
const data = await resp.json();
let locName = "Your Area";
if (data.cityName && data.regionName) {
locName = `${data.cityName}, ${data.regionName}`;
} else if (data.cityName) {
locName = data.cityName;
}
if (data.latitude && data.longitude) {
await fetchForecast(data.latitude, data.longitude, locName);
} else {
throw new Error("Invalid IP response coordinates");
}
} catch (e) {
// Ultimate hard-coded default (e.g. general center of US/state if offline)
document.getElementById('loading-text').textContent = "⚠️ Location lookup failed. Please verify internet.";
}
}
async function fetchForecast(lat, lon, locationLabel) {
try {
document.getElementById('loading-text').textContent = "📊 Loading weather forecasts...";
// Query the completely open, keyless Open-Meteo API
const url = `https://api.open-meteo.com/v1/forecast?latitude=${lat}&longitude=${lon}&current=temperature_2m,relative_humidity_2m,apparent_temperature,weather_code,wind_speed_10m&daily=weather_code,temperature_2m_max,temperature_2m_min&temperature_unit=fahrenheit&wind_speed_unit=mph&precipitation_unit=inch&timezone=auto`;
const resp = await fetch(url);
const data = await resp.json();
const locName = data.location || "Your Area";
const fc = data.forecast;
// Render current stats
const current = data.current;
const current = fc.current;
const wInfo = getWeatherInfo(current.weather_code);
document.getElementById('location-name').textContent = locationLabel;
document.getElementById('location-name').textContent = locName;
document.getElementById('temp-val').textContent = `${Math.round(current.temperature_2m)}°F`;
document.getElementById('main-icon').textContent = wInfo.emoji;
document.getElementById('condition-txt').textContent = wInfo.txt;
@@ -309,11 +261,10 @@
document.getElementById('wind-val').textContent = `${Math.round(current.wind_speed_10m)} mph`;
// Render 3-Day Forecast
const daily = data.daily;
const daily = fc.daily;
const forecastBox = document.getElementById('forecast-box');
forecastBox.innerHTML = ""; // Clear loader
// We render days 1, 2, and 3 (index 1 to 3, as index 0 is today)
for (let i = 1; i <= 3; i++) {
const dateStr = daily.time[i];
const dateObj = new Date(dateStr + "T00:00:00");
@@ -338,7 +289,7 @@
document.getElementById('weather-content').style.display = 'block';
} catch (e) {
document.getElementById('loading-text').textContent = "⚠️ Error pulling forecast data.";
document.getElementById('loading-text').textContent = "⚠️ Preparing weather data... Please wait a moment.";
}
}