'Issues with loading and viewing openstreetmap tiles : Failed to load resource: the server responded with a status of 403 () ElectronReactWebpack

As I described in this StackOverflow question : Leaflet : Suddenly I'm getting this error message: Failed to load resource: the server responded with a status of 403 () I'm experiencing issues with loading and viewing openstreetmap tiles :

Failed to load resource: the server responded with a status of 403 ()

Based on the info here : https://operations.osmfoundation.org/policies/tiles/ the following are the requirements for the openstreetmap tiles usage:

Requirements

    Heavy use (e.g. distributing an app that uses tiles from openstreetmap.org) is forbidden without prior permission from the [Operations Working Group](https://wiki.osmfoundation.org/wiki/Operations_Working_Group). See below for alternatives.
    Clearly display [license](https://wiki.openstreetmap.org/wiki/License) attribution.
    Do not actively or passively encourage copyright infringement.
    Calls to /cgi-bin/export may only be triggered by direct end-user action. (For example: “click here to export”.) The export call is an expensive (CPU+RAM) function to run and will frequently reject when server is under high load.
    Recommended: Do not hardcode any URL at tile.openstreetmap.org as doing so will limit your ability to react quickly if the service is disrupted or blocked.
    Recommended: add a link to https://www.openstreetmap.org/fixthemap to allow your users to report and fix problems in our data.

Technical Usage Requirements

    Valid [HTTP User-Agent](http://en.wikipedia.org/wiki/en:User_agent) identifying application. Faking another app’s User-Agent WILL get you blocked.
    If known, a valid [HTTP Referer](http://en.wikipedia.org/wiki/en:HTTP_Referer).
    DO NOT send no-cache headers. (“Cache-Control: no-cache”, “Pragma: no-cache” etc.)
    Cache Tile downloads locally according to HTTP Expiry Header, alternatively a minimum of 7 days.
    Maximum of 2 download threads. (Unmodified web browsers’ download thread limits are acceptable.)

Note: modern web browsers in standard configuration generally pass all the above technical requirements.

As far as I understand, I do respect all of them

html file:

<html>
  <head>
    <meta charset="UTF-8">
    <meta http-equiv="original-trial" content=AhRSNW+2zbXlDahhAcj194oqlgDxj3TqG4sYwhsUdqldtNbZagboEjqIJZuUvBsIjtij4VSW0YvP5tGSpVgu6wgAAABzeyJvcmlnaW4iOiJodHRwczovL2dnYy53b3JsZDo0NDMiLCJmZWF0dXJlIjoiVW5yZXN0cmljdGVkU2hhcmVkQXJyYXlCdWZmZXIiLCJleHBpcnkiOjE2MzM0NzgzO>
    <title>First Window</title>
  </head>
  <body>
    <div id="app"></div>
    <script type="javascript" defer src="./app/index.js"></script>
    <script>
    </script>

    <style>
      .leaflet-container {
        height: 800px;
        width: 100%;
       }
     </style>

  </body>
</html>

App.tsx :

import * as React from 'react';

const Mapping = React.lazy(() => import('./mapVisualize/maps'))

function App() {

  return (
    <div className='container'>
      <React.Suspense fallback={<div>Loading...</div>}>
        <Mapping />
      </React.Suspense>

    </div>
  );
}


export default App;

./mapVisualize/maps.js :

import Leaflet from "leaflet"; import "leaflet/dist/leaflet.css";

import React from "react";
import {
  TileLayer,
  LayersControl,
  Marker,
  Popup,
  MapContainer
} from "react-leaflet";


import iconUrl from "../../../assets/css/svg/push_pin_black_36dp.svg";

export const newicon = new Leaflet.Icon({
  iconUrl,
  iconAnchor: [5, 55],
  popupAnchor: [10, -44],
  iconSize: [25, 55]
});

const center = {
  lat: 51.505,
  lng: -0.09
};


export default function App() {
  return (
    <div className="App">
      <MapContainer center={center} zoom={13} style={{ height: "200px" }}>
        <LayersControl position="topleft">
          <LayersControl.BaseLayer checked name="Basic Map">
            <TileLayer
              attribution='&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
              url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
            />
          </LayersControl.BaseLayer>

          <Marker position={[51.505, -0.09]}>
            <Popup>
              A pretty CSS3 popup. <br /> Easily customizable.
            </Popup>
          </Marker>

          <LayersControl.BaseLayer name="Topo Map">
            <TileLayer
              attribution='Map data: &copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors, <a href="http://viewfinderpanoramas.org">SRTM</a> | Map style: &copy; <a href="https://opentopomap.org">OpenTopoMap</a> (<a href="https://creativecommons.org/licenses/by-sa/3.0/">CC-BY-SA</a>)'
              url="https://{s}.tile.opentopomap.org/{z}/{x}/{y}.png"
            />
          </LayersControl.BaseLayer>
        </LayersControl>

          <Marker position={[51.505, -0.09]}>
            <Popup>
              A pretty CSS3 popup. <br /> Easily customizable.
            </Popup>
          </Marker>

      </MapContainer>
    </div>
  );
}

webpack.config.js :

const path = require('path');
const cwd = process.cwd();
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CopyPlugin = require("copy-webpack-plugin");
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const webpack = require('webpack');


function srcPaths(src) {
  return path.join(__dirname, src);
}

const isEnvProduction = process.env.NODE_ENV === 'production';
const isEnvDevelopment = process.env.NODE_ENV === 'development';

  // main process
var main_config = {
    mode: isEnvProduction ? 'production' : 'development',
    entry: './src/main/main.ts',
    target: 'electron-main',
    resolve: {
      extensions: ['.jsx', '.js', 'ts'],
    },
    externals: [
      {
        'utf-8-validate': 'commonjs utf-8-validate',
        bufferutil: 'commonjs bufferutil',
      },
    ],
    module: {
      rules: [
        {
          test: /\.ts$/,
          include: /src/,
          use: [{ loader: 'ts-loader' }]
        },
        {
          test: /\.(sass|less|css)$/i,
          use: [
            {
              loader: 'style-loader'
            },
            {
              loader: 'css-loader'
            },
            {
              loader: 'less-loader',
            },
          ],
        },
        {
          test: /\.s?css$/,
          use: [
            {
              loader: MiniCssExtractPlugin.loader,
              options: {
                sourceMap: true
              }
            },
            {
              loader: 'css-loader',
              options: {
                sourceMap: true
              }
            },
            {
              loader: 'sass-loader',
              options: {
                sourceMap: true
              }
            }
          ]
        },
        {
          test: /\.(png|jpe?g|svg|gif)$/i,
          use: [
            {
              loader: "file-loader",
              options: {
                name: "[path]/[name].[ext]",
              },
            },
          ],
        },
        {
          test: /\.geojson$/,
           use: [
             {
               loader: "json-loader",
             }
          ],
        }
      ]
    },
    output: {
      path: __dirname + '/dist',
      filename: 'main.js'
    },
    node: {
      __dirname: false,
      __filename: false
    },
};

  // renderer process
var renderer_config =  {
    mode: isEnvProduction ? 'production' : 'development',
    entry: {
      app: ['./src/app/index.tsx', 'react-app-polyfill/stable'],
      style: [
        './src/app/styles/index.css',
        path.resolve(__dirname, 'node_modules/leaflet/dist/leaflet.css')
      ]
    },
    //target: 'electron-renderer',
    //target: 'web',
    target: ['web', 'es5'],
    resolve: {
      extensions: ['.jsx', '.js', '.tsx', '.ts'],
    },
    output: {
      path: __dirname + '/dist/',
        //filename: 'renderer.js'
      filename: '[name].js',
    },
    externals: [
      {
        'utf-8-validate': 'commonjs utf-8-validate',
        bufferutil: 'commonjs bufferutil',
      },
    ],
    module: {
      rules: [
        {
          test: /\.(js|ts)x?$/,
          exclude: /node_modules/,
          use: {
            loader: 'babel-loader',
          },
        },
        {
          // css files
          test: /\.css$/i,
          use: [
            {
              loader: 'style-loader'
            },
            {
              loader: 'css-loader'
            },
          ],
        },
        {
          test: /\.(png|jpe?g|svg|gif)$/i,
          use: [
            {
              loader: "file-loader",
              options: {
                name: "[path]/[name].[ext]",
              },
            },
          ],
        },
        {
          // Font files
          test: /\.(woff|woff2|ttf|otf)$/,
          loader: 'file-loader',
          options: {
            name: '[hash].[ext]',
            outputPath: 'dist/assets/css/'
          }
        },
      ],
    },
    node: {
      __dirname: false,
      __filename: false
    },
    plugins: [
      new HtmlWebpackPlugin({
        filename: 'index.html',
        template: './src/app/index.html',
        inject:'body',
        chunks: ['app'],
      }),
      new MiniCssExtractPlugin({
        filename: '[name].css',
        chunkFilename: '[id].css',
        linkType: 'text/css',
      }),
      new CopyPlugin({
        patterns: [
          {
            from: path.resolve(__dirname, "./src/assets/css"),
            to: path.resolve(__dirname, "./dist/assets/css")
          },
        ],
        options: {
          concurrency: 100,
        },
      }),
    ]
}

module.exports = [
  main_config,
  renderer_config,
];

What perplex me is that this happens only for the Basic Map Layer:

    <LayersControl position="topleft">
      <LayersControl.BaseLayer checked name="Basic Map">
        <TileLayer
          attribution='&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
          url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
        />
      </LayersControl.BaseLayer>

enter image description here

enter image description here

For example, it fails to load this tile:

https://b.tile.openstreetmap.org/13/4094/2723.png

enter image description here

Error message: Access Denied See: https://operations.osmfoundation.org/policies/tiles/

With the Topo Map Layer the visualization seems fine :

      <LayersControl.BaseLayer name="Topo Map">
        <TileLayer
          attribution='Map data: &copy; <a href="http://viewfinderpanoramas.org">SRTM</a> | Map style: &copy; <a href="https://opentopomap.org">OpenTopoMap</a> (<a href="https://creativecommons.org/licenses/by-sa/3.0/">CC-BY-SA</a>)'
          url="https://{s}.tile.opentopomap.org/{z}/{x}/{y}.png"
        />
      </LayersControl.BaseLayer>

enter image description here

How to solve the issue? Looking forward to your kind help



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source