reactjstypescriptnext.jsleafletreact-leaflet

LeafLet implementation on Next JS 13


I trying to implement a LeafLet map component in my Next JS 13.0.1 project, but I'm having a problem with the render of the map component.

In the first load of the map component, this error appears:

ReferenceError: window is not defined
    at eval (webpack-internal:///(sc_client)/./node_modules/leaflet/dist/leaflet-src.js:229:19)
    at eval (webpack-internal:///(sc_client)/./node_modules/leaflet/dist/leaflet-src.js:7:11)
    at eval (webpack-internal:///(sc_client)/./node_modules/leaflet/dist/leaflet-src.js:9:3)
    at Object.(sc_client)/./node_modules/leaflet/dist/leaflet-src.js (C:\desenvolvimento\estacionai-front\.next\server\app\page.js:482:1)
    at __webpack_require__ (C:\desenvolvimento\estacionai-front\.next\server\webpack-runtime.js:33:43)
    at eval (webpack-internal:///(sc_client)/./node_modules/leaflet-defaulticon-compatibility/dist/leaflet-defaulticon-compatibility.js:2:18)
    at eval (webpack-internal:///(sc_client)/./node_modules/leaflet-defaulticon-compatibility/dist/leaflet-defaulticon-compatibility.js:4:2)
    at Object.(sc_client)/./node_modules/leaflet-defaulticon-compatibility/dist/leaflet-defaulticon-compatibility.js (C:\desenvolvimento\estacionai-front\.next\server\app\page.js:472:1)  
    at __webpack_require__ (C:\desenvolvimento\estacionai-front\.next\server\webpack-runtime.js:33:43)
    at eval (webpack-internal:///(sc_client)/./components/Mapa.tsx:11:91)

anyways, the map loads, but things like markes do not appear

enter image description here

the real problem is when the page is reloaded, being a hard reload or a Link reference. this error appears:

TypeError: Cannot read properties of undefined (reading 'default')
    at resolveModuleMetaData (webpack-internal:///(sc_server)/./node_modules/next/dist/compiled/react-server-dom-webpack/server.browser.js:195:82)
    at serializeModuleReference (webpack-internal:///(sc_server)/./node_modules/next/dist/compiled/react-server-dom-webpack/server.browser.js:1298:50)
    at resolveModelToJSON (webpack-internal:///(sc_server)/./node_modules/next/dist/compiled/react-server-dom-webpack/server.browser.js:1660:40)
    at Array.toJSON (webpack-internal:///(sc_server)/./node_modules/next/dist/compiled/react-server-dom-webpack/server.browser.js:1081:40)
    at stringify (<anonymous>)
    at processModelChunk (webpack-internal:///(sc_server)/./node_modules/next/dist/compiled/react-server-dom-webpack/server.browser.js:163:36)
    at retryTask (webpack-internal:///(sc_server)/./node_modules/next/dist/compiled/react-server-dom-webpack/server.browser.js:1823:50)
    at performWork (webpack-internal:///(sc_server)/./node_modules/next/dist/compiled/react-server-dom-webpack/server.browser.js:1856:33)
    at AsyncLocalStorage.run (node:async_hooks:330:14)
    at eval (webpack-internal:///(sc_server)/./node_modules/next/dist/compiled/react-server-dom-webpack/server.browser.js:1934:55) {
  digest: '699076802'
}

After this error, the page do not load

blank page enter image description here

  • Project Structure:

project structure

  • Mapa.tsx(map component)
'use-client';

import { useState } from 'react';
import 'leaflet/dist/leaflet.css';
import 'leaflet-defaulticon-compatibility/dist/leaflet-defaulticon-compatibility.css'
import "leaflet-defaulticon-compatibility";
import { MapContainer, TileLayer, Marker, useMap } from 'react-leaflet';


export default function Map() {
  const [geoData, setGeoData] = useState({ lat: 64.536634, lng: 16.779852 });

  return (
    <MapContainer center={[geoData.lat, geoData.lng]} zoom={12} style={{ height: '90vh' }}>
      <TileLayer
         attribution='&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
         url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
       />
      {geoData.lat && geoData.lng && (
        <Marker position={[geoData.lat, geoData.lng]} />
      )}
    </MapContainer>
  );
}
  • Home component (app/page.tsx)
'use client';

import Link from "next/link";
import { useEffect, useState } from "react";
import Mapa from "../components/Mapa";

export default function Home(){
  
  return (
    <div>
      <Link href='/pontos'>Pontos</Link>
      <Mapa />
    </div>
  )
}

  • package.json
{
  "name": "estacionai-front",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "lint": "next lint"
  },
  "dependencies": {
    "@types/node": "18.11.9",
    "@types/react": "18.0.24",
    "@types/react-dom": "18.0.8",
    "leaflet": "^1.9.2",
    "leaflet-defaulticon-compatibility": "^0.1.1",
    "leaflet-geosearch": "^3.7.0",
    "next": "^13.0.1",
    "react": "18.2.0",
    "react-dom": "18.2.0",
    "react-leaflet": "^4.1.0",
    "typescript": "4.8.4"
  },
  "devDependencies": {
    "@types/leaflet": "^1.9.0",
    "autoprefixer": "^10.4.13",
    "postcss": "^8.4.18",
    "tailwindcss": "^3.2.1"
  }
}

I tried to use the dynamic() and import() function from Next but without successm, i think the only way to load the component is using the 'use client' directive in both the map component and page component


Solution

  • I have the exact same issue and I was able to get it working doing something like this:

    // this is your current Map file
    export default function ClientMap() {
      const [geoData, setGeoData] = useState({ lat: 64.536634, lng: 16.779852 });
    
      return (
        <MapContainer center={[geoData.lat, geoData.lng]} zoom={12} style={{ height: '90vh' }}>
          <TileLayer
             attribution='&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
             url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
           />
          {geoData.lat && geoData.lng && (
            <Marker position={[geoData.lat, geoData.lng]} />
          )}
        </MapContainer>
      );
    }
    
    // this is a "barrel file" that prevents the ClientMap from ever getting loaded in the server.
    export const Map: FunctionComponent = () => {
        const [Client, setClient] = useState<FunctionComponent>();
    
        useEffect(() => {
            (async () => {
                if (typeof global.window !== "undefined") {
                    const newClient = (await import('./MapClient')).default
                    setClient(() => newClient);
                }
            })();
        }, [])
    
        if (typeof global.window === "undefined" || !Client) {
            return null;
        }
        
        return Client ? <Client {...props} /> : null;
    }
    

    The goal is to guarantee that leaflet and related code never ever gets executed on the server. I opened a thread on Next 13 app dir discussion to see if there is a more standard way to write this code here