Fix calculator loader URL for SSR
This commit is contained in:
@@ -17,11 +17,21 @@ export interface CalculatorDef {
|
|||||||
let cache: CalculatorDef[] | null = null;
|
let cache: CalculatorDef[] | null = null;
|
||||||
let pending: Promise<CalculatorDef[]> | null = null;
|
let pending: Promise<CalculatorDef[]> | null = null;
|
||||||
|
|
||||||
|
const runtimeHost =
|
||||||
|
import.meta.env.PUBLIC_SITE_URL ??
|
||||||
|
(import.meta.env.DEV ? 'http://localhost:5173' : 'https://howdoyouconvert.com');
|
||||||
|
|
||||||
|
const getCalculatorsUrl = (): string =>
|
||||||
|
import.meta.env.SSR
|
||||||
|
? new URL('/data/calculators.json', runtimeHost).toString()
|
||||||
|
: '/data/calculators.json';
|
||||||
|
|
||||||
export async function loadCalculators(): Promise<CalculatorDef[]> {
|
export async function loadCalculators(): Promise<CalculatorDef[]> {
|
||||||
if (cache) return cache;
|
if (cache) return cache;
|
||||||
if (pending) return pending;
|
if (pending) return pending;
|
||||||
|
|
||||||
pending = fetch('/data/calculators.json')
|
const url = getCalculatorsUrl();
|
||||||
|
pending = fetch(url)
|
||||||
.then(r => r.json())
|
.then(r => r.json())
|
||||||
.then((data: CalculatorDef[]) => {
|
.then((data: CalculatorDef[]) => {
|
||||||
cache = data;
|
cache = data;
|
||||||
|
|||||||
Reference in New Issue
Block a user