diff --git a/.prettierignore b/.prettierignore index a809909..5b32ef0 100644 --- a/.prettierignore +++ b/.prettierignore @@ -4,3 +4,5 @@ yarn.lock .tanstack .agents **/src/generated/ +# Generated Lighthouse report kept at the repo root for reference. +lighthouse.json diff --git a/apps/frontend/package.json b/apps/frontend/package.json index 6edb098..1ba874c 100644 --- a/apps/frontend/package.json +++ b/apps/frontend/package.json @@ -28,6 +28,7 @@ "react": "^19.3.0", "react-dom": "^19.3.0", "react-leaflet": "5.0.0-rc.2", + "react-leaflet-cluster": "^4.1.3", "tailwindcss": "^4.3.3" }, "devDependencies": { @@ -39,6 +40,7 @@ "@testing-library/react": "^16.3.3", "@testing-library/user-event": "^14.6.8", "@types/leaflet": "^1.9.22", + "@types/leaflet.markercluster": "^1.5.6", "@types/node": "^22.20.4", "@types/react": "^19.3.0", "@types/react-dom": "^19.3.0", diff --git a/apps/frontend/public/favicon.ico b/apps/frontend/public/favicon.ico index 3cd694c..0ba2658 100644 Binary files a/apps/frontend/public/favicon.ico and b/apps/frontend/public/favicon.ico differ diff --git a/apps/frontend/src/assets/fonts/lora-latin.woff2 b/apps/frontend/src/assets/fonts/lora-latin.woff2 new file mode 100644 index 0000000..536efc5 Binary files /dev/null and b/apps/frontend/src/assets/fonts/lora-latin.woff2 differ diff --git a/apps/frontend/src/assets/fonts/playfair-display-latin.woff2 b/apps/frontend/src/assets/fonts/playfair-display-latin.woff2 new file mode 100644 index 0000000..1636e50 Binary files /dev/null and b/apps/frontend/src/assets/fonts/playfair-display-latin.woff2 differ diff --git a/apps/frontend/public/harp.svg b/apps/frontend/src/assets/harp.svg similarity index 100% rename from apps/frontend/public/harp.svg rename to apps/frontend/src/assets/harp.svg diff --git a/apps/frontend/public/harp_shadow.svg b/apps/frontend/src/assets/harp_shadow.svg similarity index 100% rename from apps/frontend/public/harp_shadow.svg rename to apps/frontend/src/assets/harp_shadow.svg diff --git a/apps/frontend/src/assets/map-placeholder.svg b/apps/frontend/src/assets/map-placeholder.svg new file mode 100644 index 0000000..fbf2096 --- /dev/null +++ b/apps/frontend/src/assets/map-placeholder.svg @@ -0,0 +1,37 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/apps/frontend/src/assets/map-poster.avif b/apps/frontend/src/assets/map-poster.avif new file mode 100644 index 0000000..42f9cb6 Binary files /dev/null and b/apps/frontend/src/assets/map-poster.avif differ diff --git a/apps/frontend/src/components/AddReviewDialog.tsx b/apps/frontend/src/components/AddReviewDialog.tsx index 50b54c5..81710bb 100644 --- a/apps/frontend/src/components/AddReviewDialog.tsx +++ b/apps/frontend/src/components/AddReviewDialog.tsx @@ -4,13 +4,12 @@ import { useMutation, useQueryClient } from '@tanstack/react-query' import { graphqlFetch } from '#/graphql/client' import { CREATE_REVIEW } from '#/graphql/venues' -import { assetUrl } from '#/lib/asset-url' +import HARP_SRC from '#/assets/harp.svg?url' import type { VenueReview } from '#/types/venue' const USERNAME_STORAGE_KEY = 'g-spotter:review-username' const BODY_MAX_LENGTH = 1000 const RATING_VALUES = [1, 2, 3, 4, 5] -const HARP_SRC = assetUrl('harp.svg') interface ReviewInput { venueId: string diff --git a/apps/frontend/src/components/ClientMap.tsx b/apps/frontend/src/components/ClientMap.tsx new file mode 100644 index 0000000..d041a8a --- /dev/null +++ b/apps/frontend/src/components/ClientMap.tsx @@ -0,0 +1,76 @@ +import { lazy, Suspense, useEffect, useState } from 'react' +import type { ComponentProps } from 'react' + +import mapPoster from '#/assets/map-poster.avif' +import type MapComponent from './Map' + +// Leaflet touches `window` while its module is evaluated, so it may only be +// imported in the browser. Kick the import off while the rest of the page is +// still hydrating, instead of waiting for the map to mount. +const mapModulePromise = import.meta.env.SSR ? null : import('./Map') + +// The dynamic import also moves Leaflet into its own chunk, keeping it out of +// the JavaScript needed for the first paint. +const LazyMap = mapModulePromise ? lazy(() => mapModulePromise) : null + +type MapProps = ComponentProps + +/** + * A picture of the map's default view (Trondheim, zoom 15) rendered on the + * server. The map is the largest thing on the first screen, so pulling that + * paint out of the JavaScript bundle lets the browser show it immediately + * instead of waiting for Leaflet and a handful of map tiles. + * + * Regenerate it by screenshotting `#map` at a 1112px viewport width (the widest + * the container gets) with no location available, then `avifenc -q 40 -s 6`. + */ +function MapPoster({ ready }: { ready: boolean }) { + return ( + <> + + {/* The live map brings its own attribution control. */} + + + ) +} + +export default function ClientMap(props: MapProps) { + const [mounted, setMounted] = useState(false) + + useEffect(() => { + setMounted(true) + }, []) + + const ready = mounted && LazyMap != null + + return ( +
+ {/* The map fades in on top of the poster rather than the poster fading + out, so the poster stays painted and remains the LCP element. */} + + {ready && ( +
+ + + +
+ )} +
+ ) +} diff --git a/apps/frontend/src/components/ClientVenueMap.tsx b/apps/frontend/src/components/ClientVenueMap.tsx new file mode 100644 index 0000000..9c3ba4f --- /dev/null +++ b/apps/frontend/src/components/ClientVenueMap.tsx @@ -0,0 +1,52 @@ +import { lazy, Suspense, useEffect, useState } from 'react' +import type { ComponentProps } from 'react' + +import mapPlaceholder from '#/assets/map-placeholder.svg' +import type VenueMapComponent from './VenueMap' + +const venueMapModulePromise = import.meta.env.SSR ? null : import('./VenueMap') +const LazyVenueMap = venueMapModulePromise + ? lazy(() => venueMapModulePromise) + : null + +type VenueMapProps = ComponentProps + +/** + * A neutral map skeleton is rendered on the server so this block is painted + * immediately instead of waiting for Leaflet and a handful of tiles: the tiles + * arrive seconds later and would otherwise win the LCP race on this page. The + * live map fades in on top and the skeleton stays painted underneath, which + * keeps it a valid LCP candidate. + */ +export default function ClientVenueMap(props: VenueMapProps) { + const [mounted, setMounted] = useState(false) + + useEffect(() => { + setMounted(true) + }, []) + + const ready = mounted && LazyVenueMap != null + + return ( +
+ + {ready && ( +
+ + + +
+ )} +
+ ) +} diff --git a/apps/frontend/src/components/Devtools.tsx b/apps/frontend/src/components/Devtools.tsx new file mode 100644 index 0000000..5060b81 --- /dev/null +++ b/apps/frontend/src/components/Devtools.tsx @@ -0,0 +1,26 @@ +import { TanStackRouterDevtoolsPanel } from '@tanstack/react-router-devtools' +import { TanStackDevtools } from '@tanstack/react-devtools' + +import TanStackQueryDevtools from '../integrations/tanstack-query/devtools' + +/** + * Developer tools are loaded from a separate chunk that is only requested in + * development. Keeping them out of the production bundle saves a lot of + * JavaScript that would otherwise never run. + */ +export default function Devtools() { + return ( + , + }, + TanStackQueryDevtools, + ]} + /> + ) +} diff --git a/apps/frontend/src/components/Header.tsx b/apps/frontend/src/components/Header.tsx index 64fa22d..20f12f8 100644 --- a/apps/frontend/src/components/Header.tsx +++ b/apps/frontend/src/components/Header.tsx @@ -3,18 +3,19 @@ import ThemeToggle from './ThemeToggle' export default function Header() { return (
-
) diff --git a/apps/frontend/src/components/Map.tsx b/apps/frontend/src/components/Map.tsx index 16daadf..00f526a 100644 --- a/apps/frontend/src/components/Map.tsx +++ b/apps/frontend/src/components/Map.tsx @@ -9,11 +9,14 @@ import { Tooltip, useMap, } from 'react-leaflet' +import MarkerClusterGroup from 'react-leaflet-cluster' import L from 'leaflet' +import 'leaflet/dist/leaflet.css' import { useNavigate } from '@tanstack/react-router' import { formatPrice } from '../lib/format-price' -import { assetUrl } from '#/lib/asset-url' +import harpIconUrl from '#/assets/harp.svg?url' +import harpShadowUrl from '#/assets/harp_shadow.svg?url' import { MOCK_LOCATION_NAME } from '../lib/location' import type { Coordinates } from '../lib/location' import type { SearchResult } from '../types/search' @@ -21,6 +24,11 @@ import type { SearchResult } from '../types/search' const DEFAULT_CENTER: [number, number] = [63.4305, 10.3951] const DEFAULT_ZOOM = 15 +// Using the single `tile` host (instead of the a/b/c subdomains) means the +// browser only needs one connection, and preconnecting to it in the document +// head pays off for every tile request. +const TILE_URL = 'https://tile.openstreetmap.org/{z}/{x}/{y}.png' + function FlyToResult({ coordinates }: { coordinates: number[] | undefined }) { const map = useMap() @@ -34,8 +42,8 @@ function FlyToResult({ coordinates }: { coordinates: number[] | undefined }) { } const harpIcon = new L.Icon({ - iconUrl: assetUrl('harp.svg'), - shadowUrl: assetUrl('harp_shadow.svg'), + iconUrl: harpIconUrl, + shadowUrl: harpShadowUrl, iconSize: [46, 46], shadowSize: [50, 50], iconAnchor: [23, 23], @@ -43,18 +51,45 @@ const harpIcon = new L.Icon({ popupAnchor: [0, -25], }) +/** + * Overlapping pins are merged into a single, larger target. This keeps the map + * usable on touch screens: without clustering the pins on top of each other + * cannot be tapped individually (WCAG 2.2 target size). + */ +function createClusterIcon(cluster: L.MarkerCluster) { + const count = cluster.getChildCount() + + return L.divIcon({ + html: `${count} ${ + count === 1 ? 'pub' : 'pubs' + }, zoom in to see them`, + className: 'gspotter-cluster', + iconSize: L.point(44, 44, true), + iconAnchor: [22, 22], + }) +} + +export interface MapProps { + nearby?: SearchResult[] + userLocation?: Coordinates | null + flyTo?: number[] + isMockLocation?: boolean +} + export default function Map({ nearby = [], userLocation, flyTo, isMockLocation = false, -}: { - nearby?: SearchResult[] - userLocation?: Coordinates | null - flyTo?: number[] - isMockLocation?: boolean -}) { +}: MapProps) { const navigate = useNavigate() + // Start at the user's position when we already know it. Otherwise Leaflet + // first downloads tiles for the default view and immediately replaces them + // after flying to the user, wasting both time and bandwidth. + const initialCenter: [number, number] = userLocation + ? [userLocation.lat, userLocation.lon] + : DEFAULT_CENTER + const initialZoom = userLocation ? 12 : DEFAULT_ZOOM const markers = nearby.filter( (venue, index, all) => all.findIndex( @@ -66,45 +101,54 @@ export default function Map({ ) return ( -
+
- {markers.map((curr) => ( - { - if (curr.cityId) { - void navigate({ - to: '/$cityId/venues/$venueId', - params: { cityId: curr.cityId, venueId: curr.id }, - }) - } - }, - }} - > - - {curr.name} - {curr.address && {curr.address}} - {curr.latestPriceSighting && ( - - {formatPrice(curr.latestPriceSighting)} - - )} - - - ))} + + {markers.map((curr) => ( + { + if (curr.cityId) { + void navigate({ + to: '/$cityId/venues/$venueId', + params: { cityId: curr.cityId, venueId: curr.id }, + }) + } + }, + }} + > + + {curr.name} + {curr.address && {curr.address}} + {curr.latestPriceSighting && ( + + {formatPrice(curr.latestPriceSighting)} + + )} + + + ))} + {userLocation && ( (see __root.tsx and the .mock-location-notice rule in styles.css). */ export default function MockLocationNotice() { return ( -
@@ -118,7 +121,7 @@ function VenuePage() { function PageShell({ children }: { children: ReactNode }) { return ( -
+
{children}
diff --git a/apps/frontend/src/routes/$cityId/venues/index.tsx b/apps/frontend/src/routes/$cityId/venues/index.tsx index d5ea2b0..0424e92 100644 --- a/apps/frontend/src/routes/$cityId/venues/index.tsx +++ b/apps/frontend/src/routes/$cityId/venues/index.tsx @@ -1,22 +1,22 @@ import type { ReactNode } from 'react' -import type { venue } from '#/types/venue' import { useQuery } from '@tanstack/react-query' import { Link, createFileRoute } from '@tanstack/react-router' import Breadcrumbs from '#/components/Breadcrumbs' import VenueCard from '#/components/VenueCard' -import { graphqlFetch } from '#/graphql/client' -import { GET_VENUE } from '#/graphql/venues' +import { venuesQueryOptions } from '#/graphql/venues' export const Route = createFileRoute('/$cityId/venues/')({ + // Prefetch during SSR so the list is in the first paint. Rendering a loading + // shell instead makes the page grow once the data lands, which shifts the + // footer by a sixth of the viewport. + loader: ({ context }) => + context.queryClient.prefetchQuery(venuesQueryOptions), component: CityVenuesPage, }) function CityVenuesPage() { const { cityId } = Route.useParams() - const { data, isPending, isError } = useQuery({ - queryKey: ['venues'], - queryFn: () => graphqlFetch<{ venue: venue[] }>(GET_VENUE), - }) + const { data, isPending, isError } = useQuery(venuesQueryOptions) if (isPending) { return ( @@ -54,7 +54,7 @@ function CityVenuesPage() { } return ( -
+

{cityName}

@@ -72,7 +72,7 @@ function CityVenuesPage() { function PageShell({ children }: { children: ReactNode }) { return ( -

+
{children}
diff --git a/apps/frontend/src/routes/__root.tsx b/apps/frontend/src/routes/__root.tsx index 9eada18..1ac9610 100644 --- a/apps/frontend/src/routes/__root.tsx +++ b/apps/frontend/src/routes/__root.tsx @@ -1,16 +1,15 @@ +import { lazy, Suspense } from 'react' import { HeadContent, Scripts, createRootRouteWithContext, } from '@tanstack/react-router' -import { TanStackRouterDevtoolsPanel } from '@tanstack/react-router-devtools' -import { TanStackDevtools } from '@tanstack/react-devtools' import Footer from '../components/Footer' import Header from '../components/Header' -import TanStackQueryDevtools from '../integrations/tanstack-query/devtools' - import appCss from '../styles.css?url' +import loraFont from '../assets/fonts/lora-latin.woff2?url' +import playfairFont from '../assets/fonts/playfair-display-latin.woff2?url' import type { QueryClient } from '@tanstack/react-query' @@ -20,12 +19,23 @@ interface MyRouterContext { const THEME_INIT_SCRIPT = `(function(){try{var stored=window.localStorage.getItem('theme');var mode=(stored==='light'||stored==='dark')?stored:'dark';var root=document.documentElement;root.classList.remove('light','dark');root.classList.add(mode);root.setAttribute('data-theme',mode);root.style.colorScheme=mode;}catch(e){}})();` +// The mock-location notice depends on `window.isSecureContext`, which is only +// known in the browser. Deciding it after hydration would push the whole page +// down and register as a large layout shift, so the flag is set before the +// first paint and the notice is toggled with plain CSS instead. +const MOCK_LOCATION_SCRIPT = `(function(){try{if(!window.isSecureContext){document.documentElement.setAttribute('data-mock-location','');}}catch(e){}})();` + // Vite hands us `/project2` (no trailing slash) here, so normalize before // appending asset names. const BASE_URL = import.meta.env.BASE_URL.endsWith('/') ? import.meta.env.BASE_URL : `${import.meta.env.BASE_URL}/` +// Loaded on demand so React Router/Query devtools never ship in production. +const Devtools = import.meta.env.DEV + ? lazy(() => import('../components/Devtools')) + : null + export const Route = createRootRouteWithContext()({ head: () => ({ meta: [ @@ -36,9 +46,31 @@ export const Route = createRootRouteWithContext()({ name: 'viewport', content: 'width=device-width, initial-scale=1', }, + { + name: 'description', + content: + 'Find pubs near you, compare Guinness prices, check opening hours and read reviews with G-Spotter.', + }, + { + name: 'theme-color', + content: '#100b0b', + }, { title: 'G-Spotter', }, + { + property: 'og:title', + content: 'G-Spotter', + }, + { + property: 'og:description', + content: + 'Find pubs near you, compare Guinness prices, check opening hours and read reviews with G-Spotter.', + }, + { + property: 'og:type', + content: 'website', + }, ], links: [ { @@ -60,31 +92,40 @@ function RootDocument({ children }: { children: React.ReactNode }) {