Replace Netlify function with public APIs for location search#78
Draft
Copilot wants to merge 3 commits into
Draft
Replace Netlify function with public APIs for location search#78Copilot wants to merge 3 commits into
Copilot wants to merge 3 commits into
Conversation
Co-authored-by: niraj-sachania <39126181+niraj-sachania@users.noreply.github.com>
Co-authored-by: niraj-sachania <39126181+niraj-sachania@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Replace city/postcode resolution with countries API data
Replace Netlify function with public APIs for location search
Nov 21, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The location search currently depends on a Netlify function at
strong-mermaid-23f3ab.netlify.appwhich isn't available for all deployments. This replaces it with public APIs: countriesnow.space for city/country data and Nominatim for geocoding.Changes
Data fetching:
getCoordinates()now fetches fromhttps://countriesnow.space/api/v0.1/countries{ country, city, display }objects instead of coordinatesGeocoding:
geocodeLocation(city, country)helper uses Nominatim API{ lat, lon }or nullFlow:
handleSearch(): Single match → geocode → navigate; multiple matches → show choicesrenderChoices(): Geocodes on selection with "Resolving location..." feedbackAPI considerations
Nominatim: Rate limited to ~1 req/sec, requires User-Agent. Consider server-side geocoding or paid provider for production.
countriesnow.space: Large dataset (~250 countries, thousands of cities) matched client-side. Consider caching to reduce repeated fetches.
Screenshot
UI unchanged, maintains existing search/selection flow:
Original prompt
Problem
The search/autosuggest in assets/js/location-search.js currently calls a Netlify function (strong-mermaid preview URL) to resolve city/postcode to coordinates. That function is not available for all deployments and we want to replace the remote endpoint with the public countries list endpoint at https://countriesnow.space/api/v0.1/countries and adjust the code to work with the returned JSON.
Goal
Replace the existing getCoordinates flow so the client fetches the countries data from https://countriesnow.space/api/v0.1/countries, matches user input against country and city names, displays matches to the user, and resolves a chosen location to lat/lon using a geocoding service. Maintain existing UX: if a single match is found, navigate to the weather page with coordinates; if multiple matches are found, show choices. Keep error handling and messaging consistent with the rest of the file.
Files to change
Detailed changes
Replace the implementation of getCoordinates so it:
dataarray of objects withcountryandcities)Add a helper function geocodeLocation(city, country) which:
Update handleSearch to:
Update renderChoices to render the new match objects and when a user selects one, geocode it then navigate to the weather page. Preserve existing DOM structure/classes used for accessibility and styling.
Add comments noting that countriesnow returns a list without coordinates and that Nominatim has rate limits and usage terms; recommend a server-side or API-key provider for production.
Replacement code (drop-in for the existing getCoordinates / handleSearch / renderChoices and added helper):
// Fetch list of countries and cities and return matches based on the user's query.
// Returns an array of { country, city, display } objects (city may be null)
const getCoordinates = async () => {
const cityOrPostcode = getCityOrPostcode();
if (!cityOrPostcode) return null;
try {
const resp = await fetch("https://countriesnow.space/api/v0.1/countries");
const json = await resp.json();
if (!json || !json.data) return null;
} catch (error) {
console.error("Error fetching countries list:", error);
throw error;
}
};
// Lightweight geocoding using Nominatim (OpenStreetMap). Returns { lat, lon } or null.
// Note: Nominatim has usage terms/rate limits. For production use, consider a server-side
// geocode service or an API key-based provider.
const geocodeLocation = async (city, country) => {
const q = city ?
${city}, ${country}: country;const url =
https://nominatim.openstreetmap.org/search?format=json&limit=1&q=${encodeURIComponent( q )};try {
const res = await fetch(url, {
headers: {
Accept: "application/json",
},
});
const data = await res.json();
if (Array.isArray(data) && data.length > 0) {
return { lat: parseFloat(data[0].lat), lon: parseFloat(data[0].lon) };
}
return null;
} catch (err) {
console.error("Geocode error:", err);
return null;
}
};
const handleSearch = async () => {
btn.disabled = true;
clearResults();
try {
const items = await getCoordinates();
This pull request was created as a result of the following prompt from Copilot chat.
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.