From 6a4481e9b9243f492a3f7799b1cd40b7a39a300e Mon Sep 17 00:00:00 2001 From: markmybytes Date: Mon, 30 Nov 2026 23:08:01 +0800 Subject: [PATCH] fix(wmo): serve fresh present weather from per-city feed Drop the unstable_cache wrappers whose stale-while-revalidate served stale weather on first load. Fetch present observations from the per-city endpoint instead of the ~71 KB aggregate feed, keep presentAll for that feed, and derive icons from wxImageCode. --- src/app/[locale]/weather/[id]/page.tsx | 28 +++++----- src/lib/wmo-cached.ts | 8 +-- src/lib/wmo.ts | 75 +++++++++++++------------- 3 files changed, 52 insertions(+), 59 deletions(-) diff --git a/src/app/[locale]/weather/[id]/page.tsx b/src/app/[locale]/weather/[id]/page.tsx index 0954da5..8d079c5 100644 --- a/src/app/[locale]/weather/[id]/page.tsx +++ b/src/app/[locale]/weather/[id]/page.tsx @@ -1,6 +1,6 @@ -import { TempUnit, WindUnit } from "@/lib/wmo"; +import { forecasts, present, TempUnit, WindUnit } from "@/lib/wmo"; import type { AppLocale } from "@/i18n/routing"; -import { getCachedCity, getCachedForecasts, getCachedPresent } from "@/lib/wmo-cached"; +import { getCachedCity } from "@/lib/wmo-cached"; import { Metadata } from "next"; import WeatherClassic from "@/components/weather/WeatherClassic"; import WeatherMasthead from "@/components/weather/WeatherMasthead"; @@ -114,37 +114,37 @@ export default async function Page(props: { style === "classic" ? ( ) : style === "masthead" ? ( ) : style === "range-bars" ? ( ) : style === "capsule" ? ( ) : style === "beacon" ? ( ) : ( ) @@ -154,7 +154,7 @@ export default async function Page(props: { style === "classic" ? ( ; }; -/** - * Represents the response structure for a present weather from `/{local}/json/present.xml`. - */ -export type WmoPresentWxResponse = { - present: Array<{ - cityId: number; - daynightcode: "" | "a" | "b"; - iconNum: string; - issue: string; - moonrise: string; - moonset: string; - rh: "" | number; - stnId: string; - stnName: string; - sundate: string; - sunrise: string; - sunset: string; - temp: "" | number; - wd: string; - ws: string; - wxImageCode: string; - wxdesc: string; - }>; +/** A per-city present-weather observation from WMO. */ +export type WmoPresentEntry = { + cityId: number; + daynightcode: "" | "a" | "b"; + issue: string; + moonrise: string; + moonset: string; + rh: "" | number; + stnId: string; + stnName: string; + sundate: string; + sunrise: string; + sunset: string; + temp: "" | number; + wd: string; + ws: string; + wxImageCode: string; + wxdesc: string; +}; + +/** Response from the aggregate present-weather feed. */ +export type WmoPresentAllResponse = { + present: Record; }; const wmoUrl = "https://worldweather.wmo.int"; @@ -217,14 +217,14 @@ const wmoCode: Record = { }; /** - * Generates the URL for a WMO weather icon based on the icon ID. + * Generates the URL for a WMO weather icon based on its two-digit code. * - * @param id - The four-digit icon ID. + * @param code - The two-digit icon code. * @param daynightCode - Code that indicate day or night version (if available). * @returns The URL of the weather icon. */ -function wxIconUrl(id: string, daynightCode: string) { - return `${wmoUrl}/images/i${parseInt(id.slice(0, id.length - 2))}${daynightCode}.png`; +function wxIconUrl(code: string, daynightCode: string) { + return `${wmoUrl}/images/i${parseInt(code)}${daynightCode}.png`; } /** Fetches a URL and parses its JSON body, mapping parse failures to a descriptive error. */ @@ -327,15 +327,14 @@ export async function forecasts( min: { unit, val: parseTemp(day.minTemp, day.minTempF, unit) }, max: { unit, val: parseTemp(day.maxTemp, day.maxTempF, unit) }, }, - icon: day.weatherIcon != 0 ? wxIconUrl(day.weatherIcon.toString(), "") : null, + icon: day.weatherIcon != 0 ? wxIconUrl(day.weatherIcon.toString().slice(0, -2), "") : null, })) .slice(0, Math.max(Math.abs(days), 1)), }; } /** - * Fetches the present weather data for a specified city. Throws a RangeError - * when the feed carries no entry for the city. + * Fetches the present weather data for a specified city. * * @param cityId - The ID of the city. * @param locale - The locale code. @@ -349,13 +348,8 @@ export async function present( unit: TempUnit, windUnit: WindUnit, ): Promise { - const json = await getJson(`${wmoUrl}/${wmoCode[locale]}/json/present.xml`); - - const wx = Object.values(json.present).find((v) => v.cityId == cityId); - - if (!wx) { - throw new RangeError(`No data for the city (id: ${cityId})`); - } + const apiLocale = wmoCode[locale]; + const wx = await getJson(`${wmoUrl}/${apiLocale}/json/present_${cityId}.json`); return { issueAt: parseIssueTime(wx.issue), @@ -365,7 +359,7 @@ export async function present( }, rh: wx.rh || null, weather: wx.wxdesc, - icon: wx.iconNum !== "" ? wxIconUrl(wx.iconNum, wx.daynightcode) : null, + icon: wx.wxImageCode ? wxIconUrl(wx.wxImageCode, wx.daynightcode) : null, wind: wx.wd !== "" && wx.ws !== "" ? { @@ -381,6 +375,11 @@ export async function present( }; } +/** Fetches every present-weather observation from the aggregate feed. */ +export async function presentAll(locale: AppLocale): Promise { + return getJson(`${wmoUrl}/${wmoCode[locale]}/json/present.xml`); +} + /** * Fetches the data of list of countries. *