diff --git a/apps/hub/src/app/layout.tsx b/apps/hub/src/app/layout.tsx index 221ec11dc..d649663f7 100755 --- a/apps/hub/src/app/layout.tsx +++ b/apps/hub/src/app/layout.tsx @@ -1,5 +1,5 @@ import "../styles/globals.css"; -import { readFileSync } from "fs"; +import { existsSync, readFileSync } from "fs"; import path from "path"; import { Metadata } from "next"; import dynamic from "next/dynamic"; @@ -38,14 +38,38 @@ const PostHogPageView = dynamic(() => import("./PostHogPageView"), { }); export default async function RootLayout(props: { children: React.ReactNode }) { - const fetchedTokenList = await (tokenListUrl.startsWith("http") - ? fetch(tokenListUrl).then((res) => res.json()) - : JSON.parse( - readFileSync( - path.join(process.cwd(), `public/${tokenListUrl}`), - "utf8", - ), - )); + let fetchedTokenList = null; + + try { + if (tokenListUrl.startsWith("http")) { + const response = await fetch(tokenListUrl); + if (!response.ok) { + throw new Error(`Failed to fetch token list: ${response.statusText}`); + } + fetchedTokenList = await response.json(); + } else { + const publicPath = path.join(process.cwd(), "publica"); + const tokenListPath = path.join(publicPath, tokenListUrl); + + // Check if public directory and file exist + if (existsSync(tokenListPath)) { + try { + const fileContent = readFileSync(tokenListPath, "utf8"); + fetchedTokenList = JSON.parse(fileContent); + } catch (error: unknown) { + const errorMessage = + error instanceof Error ? error.message : "Unknown error"; + console.error(`Error parsing token list file: ${errorMessage}`); + } + } else { + console.error(`Token list file not found at: ${tokenListPath}`); + } + } + } catch (error: unknown) { + const errorMessage = + error instanceof Error ? error.message : "Unknown error"; + console.error(`Error loading token list: ${errorMessage}`); + } return (