From c2b685c0dce421a6c6e4b33e1020ca84321b0dd8 Mon Sep 17 00:00:00 2001 From: Kirill Date: Sun, 24 May 2026 19:38:40 +0500 Subject: [PATCH] perf: code-split maplibre-gl in AboutPage --- client/src/pages/about/ui/AboutMap.tsx | 107 ++++++++++++++++++++++++ client/src/pages/about/ui/AboutPage.tsx | 54 +----------- 2 files changed, 109 insertions(+), 52 deletions(-) create mode 100644 client/src/pages/about/ui/AboutMap.tsx diff --git a/client/src/pages/about/ui/AboutMap.tsx b/client/src/pages/about/ui/AboutMap.tsx new file mode 100644 index 0000000..16693f9 --- /dev/null +++ b/client/src/pages/about/ui/AboutMap.tsx @@ -0,0 +1,107 @@ +import { useEffect, useState } from 'react' +import Box from '@mui/material/Box' +import CircularProgress from '@mui/material/CircularProgress' +import Map, { Marker } from 'react-map-gl/maplibre' +import type * as maplibregl from 'maplibre-gl' + +let maplibreglPromise: Promise | null = null + +function loadMaplibre() { + if (!maplibreglPromise) { + maplibreglPromise = Promise.all([ + import('maplibre-gl'), + import('maplibre-gl/dist/maplibre-gl.css'), + ]).then(([mod]) => mod) + } + return maplibreglPromise +} + +const rasterStyle = { + version: 8 as const, + sources: { + osm: { + type: 'raster' as const, + tiles: ['https://tile.openstreetmap.org/{z}/{x}/{y}.png'], + tileSize: 256, + attribution: '© OpenStreetMap contributors', + }, + }, + layers: [{ id: 'osm', type: 'raster' as const, source: 'osm' }], +} + +type AboutMapProps = { + lat: number + lng: number +} + +export function AboutMap({ lat, lng }: AboutMapProps) { + const [maplibre, setMaplibre] = useState(null) + + useEffect(() => { + let cancelled = false + loadMaplibre().then((mod) => { + if (!cancelled) setMaplibre(mod) + }) + return () => { + cancelled = true + } + }, []) + + if (!maplibre) { + return ( + + + + ) + } + + return ( + + + + + + + + ) +} diff --git a/client/src/pages/about/ui/AboutPage.tsx b/client/src/pages/about/ui/AboutPage.tsx index 1e2e25b..7a31de1 100644 --- a/client/src/pages/about/ui/AboutPage.tsx +++ b/client/src/pages/about/ui/AboutPage.tsx @@ -3,24 +3,10 @@ import Link from '@mui/material/Link' import Paper from '@mui/material/Paper' import Stack from '@mui/material/Stack' import Typography from '@mui/material/Typography' -import * as maplibregl from 'maplibre-gl' -import Map, { Marker } from 'react-map-gl/maplibre' import { STORE_EMAIL, STORE_PHONE, VK_URL } from '@/shared/config' import { PICKUP_ADDRESS_FULL, PICKUP_COORDINATES } from '@/shared/constants/pickup-point' import { usePageTitle } from '@/shared/lib/use-page-title' - -const rasterStyle = { - version: 8 as const, - sources: { - osm: { - type: 'raster' as const, - tiles: ['https://tile.openstreetmap.org/{z}/{x}/{y}.png'], - tileSize: 256, - attribution: '© OpenStreetMap contributors', - }, - }, - layers: [{ id: 'osm', type: 'raster' as const, source: 'osm' }], -} +import { AboutMap } from './AboutMap' export function AboutPage() { usePageTitle('О нас') @@ -63,43 +49,7 @@ export function AboutPage() { - - - - - - - + )