Skip to content

Commit

Permalink
Add util to check whether we're in Next.js environment, use the util
Browse files Browse the repository at this point in the history
  • Loading branch information
dzole0311 committed Dec 10, 2024
1 parent 52e4b15 commit f170ca1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Feature, Polygon } from 'geojson';
import { useCallback, useEffect, useState } from 'react';
import axios from 'axios';
import { getAoiAppropriateFeatures } from './use-custom-aoi';
import { isNextJs } from '$utils/utils';

function usePresetAOI(selectedState) {
const [error, setError] = useState<string | null>('');
Expand All @@ -18,10 +19,9 @@ function usePresetAOI(selectedState) {
// We're dynamically adjusting the base path based on the environment:
// 1. If running in Next.js, omit "/public" from the path
// 2. For legacy instances, we include "/public" as part of the path
const basePath =
typeof window !== 'undefined'
? `/geo-data/states/`
: `${process.env.PUBLIC_URL ?? ''}/public/geo-data/states/`;
const basePath = isNextJs()
? `/geo-data/states/`
: `${process.env.PUBLIC_URL ?? ''}/public/geo-data/states/`;

const res = await axios.get(`${basePath}${selectedState}.geojson`, {
signal: abortController.signal
Expand Down
8 changes: 8 additions & 0 deletions app/scripts/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,11 @@ export function composeVisuallyDisabled(
export function checkEnvFlag(value?: string): boolean {
return (value ?? '').toLowerCase() === 'true';
}

/**
* Helper to detect if code is running in a Next.js environment
* by checking for Next.js-specific 'next' property on window.
* Will return false on server-side and in non-Next.js environments
* @returns boolean indicating if running in Next.js
*/
export const isNextJs = () => 'next' in window;

0 comments on commit f170ca1

Please sign in to comment.