1.0.3 • Published 5 months ago

georender-extention v1.0.3

Weekly downloads
-
License
ISC
Repository
github
Last release
5 months ago

Geo Router Extension

Description

geo-router-extension is a React Router extension that enables geo-based routing. It allows you to define routes with a geoLocation key that specifies location-based access. If the user's location does not match, the component will navigate to a default fallback route.

Features

  • 🌍 Geo-based Routing: Define routes that only render for specific geographic locations.
  • 🔄 Fallback Support: If the user's location does not match, the component automatically falls back to a default route.
  • 📦 Local Storage Caching: Caches user location data to prevent unnecessary API calls.
  • 🛠 Custom Geo Data Fetching: Pass a custom function to fetch user location data instead of the default API.

Installation

npm install geo-router-extension

or using yarn:

yarn add geo-router-extension

Usage

Basic Example

import React from "react";
import { BrowserRouter } from "react-router-dom";
import GeoRender from "geo-router-extension";
import HomePage from "./HomePage";
import USPage from "./USPage";
import CanadaPage from "./CanadaPage";

const routes = [
  { path: "/", element: <HomePage /> },
  { path: "/", element: <USPage />, geoLocation: { country: "US" } },
  { path: "/", element: <CanadaPage />, geoLocation: { country: "CA" } },
];

const App = () => (
  <BrowserRouter>
    <GeoRender routes={routes} />
  </BrowserRouter>
);

export default App;

Using a Custom Geo Fetcher

const customGeoFetcher = async () => {
  return { country: "US", city: "New York" }; // Example custom geo data
};

<GeoRender routes={routes} getGeoData={customGeoFetcher} />;

Using a Custom Loading Page/Element

<GeoRender routes={routes} loadingElement={<LoadingPage />} />;

API

<GeoRender />

PropTypeDescription
routesArrayList of route objects with path, element, and optional geoLocation.
getGeoDataFunction(Optional) Custom function to fetch user geo data. Defaults to ipapi.co.
loadingElementJSX.Element(Optional) Custom loading element to display while fetching geo data.

Route Object Structure

{
  path: string;
  element: JSX.Element;
  geoLocation?: {
    country?: string;
    city?: string;
    zip?: string;
  };
  ...any other path props as defined in react-router-dom RouteProps
}

Error Handling

  • You can define a matching route for any route that has a geoLocation key. If the user's location does not match or no location data is available, the component will automatically navigate to the default route. Otherwise, the component will redirect to '/'.

License

MIT

1.0.3

5 months ago

1.0.2

5 months ago

1.0.1

5 months ago

1.0.0

5 months ago