From 272f6bf799c377036d6291e107928b486160177f Mon Sep 17 00:00:00 2001 From: Mikhail Chandran Date: Tue, 24 Nov 2026 15:09:52 +0530 Subject: [PATCH 1/2] feat: add a landing page for every country /country/ is generated from the dataset next to the city pages: a count per category (each linking to the map filtered to that country), the country's cities linking to their existing city pages, and a short 'start here' list that puts verified places first. It carries a schema.org Country with an ItemList of its cities, a generated Open Graph image in the same style as the city cards, and a meta description built from real counts. The only URL is the lowercase code, so /country/IN is a 404 and there is one canonical page per country. Counts are checked against the same tally that /stats uses. Part of #358 --- src/app/country/[code]/opengraph-image.tsx | 150 ++++++++++++++ src/app/country/[code]/page.tsx | 167 ++++++++++++++++ src/lib/city-pages.ts | 4 +- src/lib/country-pages.test.ts | 218 +++++++++++++++++++++ src/lib/country-pages.ts | 155 +++++++++++++++ src/lib/jsonld.test.ts | 57 ++++++ src/lib/jsonld.ts | 41 ++++ src/lib/og.test.ts | 53 ++++- src/lib/og.ts | 27 +++ 9 files changed, 869 insertions(+), 3 deletions(-) create mode 100644 src/app/country/[code]/opengraph-image.tsx create mode 100644 src/app/country/[code]/page.tsx create mode 100644 src/lib/country-pages.test.ts create mode 100644 src/lib/country-pages.ts diff --git a/src/app/country/[code]/opengraph-image.tsx b/src/app/country/[code]/opengraph-image.tsx new file mode 100644 index 0000000..fbdd1a7 --- /dev/null +++ b/src/app/country/[code]/opengraph-image.tsx @@ -0,0 +1,150 @@ +import { ImageResponse } from "next/og"; +import { notFound } from "next/navigation"; + +import { countryPageSlugs, findCountryPage } from "@/lib/country-pages"; +import { loadHeadingFonts } from "@/lib/og-fonts"; +import { getPlaces } from "@/lib/places"; +import { ogCountrySummary } from "@/lib/og"; + +// A different image per country, so shared links stop looking identical (#122, #358). +export const alt = "StudyMap — student places and benefits"; +export const size = { width: 1200, height: 630 }; +export const contentType = "image/png"; + +// The country pages are decided at build time from the dataset; so are their +// images. A slug outside the dataset is a 404, never an edge render. +export const dynamicParams = false; + +export function generateStaticParams(): { code: string }[] { + return countryPageSlugs(getPlaces()); +} + +interface CountryOgImageProps { + params: Promise<{ code: string }>; +} + +export default async function CountryOgImage({ params }: CountryOgImageProps) { + const { code } = await params; + const page = findCountryPage(getPlaces(), code); + if (!page) notFound(); + + const summary = ogCountrySummary(page); + const fonts = (await loadHeadingFonts()).map(({ data, weight }) => ({ + data, + name: "Space Grotesk", + weight, + style: "normal" as const, + })); + const fontFamily = '"Space Grotesk", "Inter", system-ui, sans-serif'; + + return new ImageResponse( +
+ {/* Wordmark: the StudyMap pin mark + name */} +
+
+
+
+ + StudyMap + +
+ + {/* Country name */} +
+ {summary.name} +
+
+ {summary.total} student place{summary.total === 1 ? "" : "s"} in{" "} + {summary.cityCount} {summary.cityCount === 1 ? "city" : "cities"} · crowdsourced +
+ + {/* Category counts */} + {summary.counts.length > 0 && ( +
+ {summary.counts.map((count) => ( +
+ + {count.count} + + {count.label} +
+ ))} + {summary.more > 0 && ( +
+ +{summary.more} more +
+ )} +
+ )} + +
+ studyymap.com +
+
, + { + ...size, + fonts, + }, + ); +} diff --git a/src/app/country/[code]/page.tsx b/src/app/country/[code]/page.tsx new file mode 100644 index 0000000..5be7ab3 --- /dev/null +++ b/src/app/country/[code]/page.tsx @@ -0,0 +1,167 @@ +import type { Metadata } from "next"; +import Link from "next/link"; +import { notFound } from "next/navigation"; + +import { placesByType } from "@/lib/city-pages"; +import { + countryMetaDescription, + countryPageSlugs, + findCountryPage, + topPlaces, +} from "@/lib/country-pages"; +import { countryJsonLdScript } from "@/lib/jsonld"; +import { getPlaces } from "@/lib/places"; +import { mapStateToSearch } from "@/lib/share"; +import { humanizeCity, PLACE_TYPE_LABELS } from "@/lib/types"; +import type { PlaceType } from "@/lib/types"; +import { Button } from "@/components/ui/button"; +import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; +import { PageContainer } from "@/components/layout/page-container"; + +// Every country page is decided at build time from the dataset; a code that is +// not in it (or not in its one canonical lowercase form) is a 404, never a +// server-rendered miss. +export const dynamicParams = false; + +export function generateStaticParams(): { code: string }[] { + return countryPageSlugs(getPlaces()); +} + +interface CountryPageProps { + params: Promise<{ code: string }>; +} + +export async function generateMetadata({ params }: CountryPageProps): Promise { + const { code } = await params; + const page = findCountryPage(getPlaces(), code); + if (!page) return {}; + return { + title: `${page.name} — student places`, + description: countryMetaDescription(page), + }; +} + +/** A /map link filtered to this country, and to one category when given. */ +function mapHref(country: string, type?: PlaceType): string { + return `/map${mapStateToSearch({ + types: type ? [type] : [], + city: null, + country, + query: "", + placeId: null, + lat: null, + lng: null, + zoom: null, + })}`; +} + +export default async function CountryPage({ params }: CountryPageProps) { + const { code } = await params; + const page = findCountryPage(getPlaces(), code); + if (!page) notFound(); + + const grouped = placesByType(page.places); + const start = topPlaces(page.places); + + return ( + + ", "n")]); + const script = countryJsonLdScript(hostile); + expect(script).not.toContain(""); + expect(script).not.toContain("<"); + expect(JSON.parse(script)["@graph"][1].itemListElement).toHaveLength(1); + }); +}); diff --git a/src/lib/jsonld.ts b/src/lib/jsonld.ts index 732d016..6380fee 100644 --- a/src/lib/jsonld.ts +++ b/src/lib/jsonld.ts @@ -1,4 +1,6 @@ import { site } from "@/lib/site"; +import type { CountryPage } from "@/lib/country-pages"; +import { humanizeCity } from "@/lib/types"; import type { Competition, CompetitionFormat } from "@/lib/types"; import type { Place } from "@/lib/types"; @@ -133,3 +135,42 @@ export function competitionJsonLd(competition: Competition): Record { + const url = `${site.url}/country/${page.slug}`; + return { + "@context": "https://schema.org", + "@graph": [ + { + "@type": "Country", + "@id": `${url}#country`, + name: page.name, + identifier: page.code, + url, + }, + { + "@type": "ItemList", + "@id": `${url}#cities`, + name: `Cities in ${page.name} on StudyMap`, + numberOfItems: page.cities.length, + itemListElement: page.cities.map((entry, index) => ({ + "@type": "ListItem", + position: index + 1, + name: humanizeCity(entry.city), + url: `${site.url}/city/${entry.slug}`, + })), + }, + ], + }; +} + +/** The JSON payload for a country page's `