Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/app/city/[slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
findCityPage,
placesByType,
} from "@/lib/city-pages";
import { countryOfCity } from "@/lib/country-pages";
import { getPlaces } from "@/lib/places";
import { placeJsonLdScript } from "@/lib/jsonld";
import { humanizeCity, PLACE_TYPE_LABELS } from "@/lib/types";
Expand Down Expand Up @@ -48,6 +49,7 @@ export default async function CityPage({ params }: CityPageProps) {

const name = humanizeCity(page.city);
const grouped = placesByType(page.places);
const country = countryOfCity(getPlaces(), page.city);

return (
<PageContainer>
Expand All @@ -60,6 +62,20 @@ export default async function CityPage({ params }: CityPageProps) {
dangerouslySetInnerHTML={{ __html: placeJsonLdScript(place) }}
/>
))}
{country && (
<nav aria-label="Breadcrumb" className="mb-2 text-sm text-muted-foreground">
<Link
href={`/country/${country.slug}`}
className="underline-offset-4 hover:text-foreground hover:underline"
>
{country.name}
</Link>
<span aria-hidden className="mx-1.5">
/
</span>
<span aria-current="page">{name}</span>
</nav>
)}
<p className="kicker">Places in</p>
<h1 className="mt-3 font-heading text-3xl font-bold tracking-tight sm:text-4xl">
{name}
Expand Down
150 changes: 150 additions & 0 deletions src/app/country/[code]/opengraph-image.tsx
Original file line number Diff line number Diff line change
@@ -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(
<div
style={{
width: 1200,
height: 630,
background: "#0A0A0A",
color: "#FAFAFA",
display: "flex",
flexDirection: "column",
padding: "64px 76px",
fontFamily,
}}
>
{/* Wordmark: the StudyMap pin mark + name */}
<div style={{ display: "flex", alignItems: "center", gap: 14 }}>
<div
style={{
width: 34,
height: 34,
borderRadius: "50% 50% 50% 0",
transform: "rotate(-45deg)",
background: "#F59E0B",
display: "flex",
alignItems: "center",
justifyContent: "center",
}}
>
<div
style={{ width: 12, height: 12, borderRadius: 999, background: "#0A0A0A" }}
/>
</div>
<span style={{ fontSize: 32, fontWeight: 700, letterSpacing: "-0.02em" }}>
StudyMap
</span>
</div>

{/* Country name */}
<div
style={{
marginTop: 104,
fontSize: 96,
fontWeight: 700,
letterSpacing: "-0.02em",
lineHeight: 1.02,
}}
>
{summary.name}
</div>
<div
style={{
display: "flex",
marginTop: 18,
fontSize: 28,
color: "#A1A1AA",
}}
>
{summary.total} student place{summary.total === 1 ? "" : "s"} in{" "}
{summary.cityCount} {summary.cityCount === 1 ? "city" : "cities"} · crowdsourced
</div>

{/* Category counts */}
{summary.counts.length > 0 && (
<div style={{ display: "flex", gap: 14, marginTop: 52, flexWrap: "wrap" }}>
{summary.counts.map((count) => (
<div
key={count.label}
style={{
display: "flex",
alignItems: "center",
gap: 12,
background: "#18181B",
border: "1px solid #27272A",
borderRadius: 999,
padding: "14px 26px",
}}
>
<span style={{ fontSize: 28, fontWeight: 700, color: "#F59E0B" }}>
{count.count}
</span>
<span style={{ fontSize: 24, color: "#D4D4D8" }}>{count.label}</span>
</div>
))}
{summary.more > 0 && (
<div
style={{
display: "flex",
alignItems: "center",
background: "transparent",
border: "1px solid #27272A",
borderRadius: 999,
padding: "14px 26px",
fontSize: 24,
color: "#71717A",
}}
>
+{summary.more} more
</div>
)}
</div>
)}

<div style={{ marginTop: "auto", fontSize: 20, color: "#71717A" }}>
studyymap.com
</div>
</div>,
{
...size,
fonts,
},
);
}
167 changes: 167 additions & 0 deletions src/app/country/[code]/page.tsx
Original file line number Diff line number Diff line change
@@ -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<Metadata> {
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 (
<PageContainer>
<script
type="application/ld+json"
// A Country with an ItemList of its cities; real names and counts only.
dangerouslySetInnerHTML={{ __html: countryJsonLdScript(page) }}
/>
<p className="kicker">Places in</p>
<h1 className="mt-3 font-heading text-3xl font-bold tracking-tight sm:text-4xl">
{page.name}
</h1>
<p className="mt-2 max-w-2xl text-muted-foreground">
{page.places.length} place{page.places.length === 1 ? "" : "s"} across{" "}
{page.cities.length} {page.cities.length === 1 ? "city" : "cities"} in {page.name}
, from the crowdsourced StudyMap dataset.
</p>

<Card className="mt-6">
<CardHeader>
<CardTitle>By category</CardTitle>
</CardHeader>
<CardContent>
<ul className="flex flex-wrap gap-2">
{grouped.map(([type, places]) => (
<li key={type}>
<Link
href={mapHref(page.code, type)}
className="inline-flex items-center gap-1.5 rounded-full border px-3 py-1.5 text-sm hover:bg-muted"
>
<span className="font-semibold tabular-nums">{places.length}</span>
{PLACE_TYPE_LABELS[type]}
</Link>
</li>
))}
</ul>
</CardContent>
</Card>

<section className="mt-8" aria-labelledby="country-cities">
<h2
id="country-cities"
className="font-heading text-lg font-semibold tracking-tight"
>
Cities
<span className="ml-2 text-sm font-normal text-muted-foreground">
{page.cities.length}
</span>
</h2>
<ul className="mt-3 grid gap-2 sm:grid-cols-2 lg:grid-cols-3">
{page.cities.map((entry) => (
<li key={entry.slug}>
<Link
href={`/city/${entry.slug}`}
className="flex items-center justify-between rounded-md border px-3 py-2 text-sm hover:bg-muted"
>
<span className="font-medium">{humanizeCity(entry.city)}</span>
<span className="tabular-nums text-muted-foreground">{entry.count}</span>
</Link>
</li>
))}
</ul>
</section>

<section className="mt-8" aria-labelledby="country-start">
<h2
id="country-start"
className="font-heading text-lg font-semibold tracking-tight"
>
Start here
</h2>
<ul className="mt-3 grid gap-3 sm:grid-cols-2">
{start.map((place) => (
<li key={place.id}>
<Card size="sm" className="h-full">
<CardHeader>
<CardTitle>{place.name}</CardTitle>
</CardHeader>
<CardContent className="flex flex-wrap items-center gap-2">
<span className="text-muted-foreground">
{PLACE_TYPE_LABELS[place.type]} · {humanizeCity(place.city)}
</span>
<Link
href={`/map?place=${encodeURIComponent(place.id)}`}
className="text-sm font-medium text-primary underline-offset-4 hover:underline"
>
Open on map
</Link>
</CardContent>
</Card>
</li>
))}
</ul>
</section>

<div className="mt-10">
<Button asChild variant="outline">
<Link href={mapHref(page.code)}>Explore {page.name} on the map</Link>
</Button>
</div>
</PageContainer>
);
}
19 changes: 19 additions & 0 deletions src/app/sitemap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { describe, expect, it } from "vitest";

import sitemap from "@/app/sitemap";
import { getCompetitions } from "@/lib/competitions";
import { countryPages } from "@/lib/country-pages";
import { getPlaces } from "@/lib/places";
import { site } from "@/lib/site";

describe("sitemap", () => {
Expand Down Expand Up @@ -58,4 +60,21 @@ describe("sitemap", () => {
expect(entry?.lastModified).toBeDefined();
}
});

it("lists one canonical URL per country page, and none for a country with no places (#358)", () => {
const entries = sitemap();
const pages = countryPages(getPlaces());
const urls = entries
.map((e) => e.url)
.filter((url) => url.startsWith(`${site.url}/country/`));

expect(urls).toHaveLength(pages.length);
expect(new Set(urls).size).toBe(urls.length);
for (const page of pages) {
expect(urls).toContain(`${site.url}/country/${page.slug}`);
}
for (const url of urls) {
expect(url.slice(`${site.url}/country/`.length)).toMatch(/^[a-z]{2}$/);
}
});
});
Loading
Loading