-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adds suspense to prevent csr of feed
- Loading branch information
1 parent
095f183
commit 69d2aa8
Showing
2 changed files
with
51 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,68 @@ | ||
import Sidebar from "@/components/Sidebar"; | ||
import Widgets from "@/components/Widgets"; | ||
import RefreshFeed from "@/components/RefreshFeed"; | ||
import CommentModal from "@/components/CommentModal"; | ||
import { Suspense } from "react"; | ||
import Feed from "@/components/Feed"; | ||
|
||
export default async function Home() { | ||
const { trendingPosts, randomUsersResults } = await getWidgetsData(); | ||
|
||
return ( | ||
<div> | ||
<main className="flex dark:bg-darkBg dark:text-darkText overflow-clip min-h-screen mx-auto"> | ||
|
||
<main | ||
role="main" | ||
className="flex dark:bg-darkBg dark:text-darkText overflow-clip min-h-screen mx-auto" | ||
> | ||
{/* Sidebar */} | ||
<Sidebar /> | ||
|
||
<div className="flex flex-grow w-full"> | ||
{/* Feed */} | ||
<RefreshFeed /> | ||
<Suspense fallback={<Feed type={undefined} />}> | ||
<RefreshFeed /> | ||
</Suspense> | ||
|
||
{/* Widgets */} | ||
|
||
<Widgets | ||
trendingPosts={trendingPosts || []} | ||
randomUsersResults={randomUsersResults?.results || []} | ||
trendingPosts={trendingPosts} | ||
randomUsersResults={randomUsersResults} | ||
/> | ||
</div> | ||
</main> | ||
</div> | ||
); | ||
} | ||
|
||
async function getWidgetsData() { | ||
|
||
if (!process.env.NEXT_PUBLIC_BACKEND_URL) { | ||
return { | ||
trendingPosts: [], | ||
randomUsersResults: [], | ||
}; | ||
} | ||
|
||
// Trending posts section | ||
// TODO: add backend route for trending posts | ||
const trendingPosts = | ||
await fetch( | ||
`${process.env.NEXT_PUBLIC_BACKEND_URL}/widgets/trending/posts` | ||
).then((res) => res.json()); | ||
async function getWidgetsData() { | ||
const backendUrl = process.env.NEXT_PUBLIC_BACKEND_URL; | ||
|
||
// Who to follow section | ||
if (!backendUrl || backendUrl === "undefined") { | ||
return { | ||
trendingPosts: [], | ||
randomUsersResults: [], | ||
}; | ||
} | ||
|
||
let randomUsersResults: any = []; | ||
try { | ||
const trendingPostsRes = await fetch( | ||
`${backendUrl}/widgets/trending/posts` | ||
); | ||
const randomUsersRes = await fetch( | ||
"https://randomuser.me/api/?results=10&inc=name,login,picture" | ||
); | ||
|
||
try { | ||
const res = await fetch( | ||
"https://randomuser.me/api/?results=10&inc=name,login,picture" | ||
); | ||
const trendingPosts = await trendingPostsRes.json(); | ||
const randomUsersResults = await randomUsersRes.json(); | ||
|
||
randomUsersResults = await res.json(); | ||
} catch (e) { | ||
randomUsersResults = []; | ||
return { | ||
trendingPosts: trendingPosts.trendingPosts, | ||
randomUsersResults: randomUsersResults.results, | ||
}; | ||
} catch (error) { | ||
return { | ||
trendingPosts: [], | ||
randomUsersResults: [], | ||
}; | ||
} | ||
} | ||
|
||
return { | ||
trendingPosts: trendingPosts?.trendingPosts, | ||
randomUsersResults, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters