Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix SSG not generating the actual page content on initial load #1599

Merged
merged 4 commits into from
Aug 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 24 additions & 9 deletions components/Loader/Preloader.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,38 @@
import useLoader from 'hooks/useLoader'
import { useEffect, useState } from 'react'
import { Spinner } from './Spinner'

const LOADER_TIMEOUT = 2000

export const Preloader = ({
backgroundColor,
children,
...rest
...spinnerProps
}: {
children: JSX.Element
backgroundColor: string
color: string
size: number
}): JSX.Element => {
const { loader } = useLoader()
if (!loader) return children
const [showLoader, setShowLoader] = useState(true)

useEffect(() => {
const timerId = setTimeout(() => {
setShowLoader(false)
}, LOADER_TIMEOUT)

return () => {clearTimeout(timerId)}
}, [])

return (
<div
className={`loader ${backgroundColor} fixed top-0 left-0 w-full h-screen flex justify-center items-center`}
>
<Spinner {...rest} />
</div>
<>
{children}
{showLoader && (
<div
className={`loader ${backgroundColor} fixed z-30 top-0 left-0 w-full h-screen flex justify-center items-center`}
>
<Spinner {...spinnerProps} />
</div>
)}
</>
)
}
2 changes: 2 additions & 0 deletions database/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,3 +269,5 @@ export const sidebarData: ISidebar[] = [
],
},
]

export const subCategories = sidebarData.flatMap(({ subcategory }) => subcategory.map(({ url }) => url.replace('/', '')))
18 changes: 11 additions & 7 deletions hooks/useFilterDB.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { database } from 'database/data'
import { useRouter } from 'next/router'
import { IData, IUseFilterDBResponse } from 'types'
import { IData } from 'types'
import { useResults } from './ResultsContext'

const useFilterDB = (): IUseFilterDBResponse => {
const useFilterDB = (subcategory: string) => {
const router = useRouter()
const { subcategory } = router.query
if (typeof subcategory !== 'string') {
throw new Error(
'UseFilterDB Error: Response of Router Query is not a string'
)
const { results } = useResults()

let pageCategory = subcategory
if (typeof window !== 'undefined') {
pageCategory = router.asPath.slice(1)
}

// This filters trough the DB with the subcategory which results in an array of arrays
const filterSubCat = database?.map((item) =>
item?.filter((cat: IData) => cat.subcategory === subcategory)
Expand All @@ -23,6 +25,8 @@ const useFilterDB = (): IUseFilterDBResponse => {
return {
filterSubCat,
filterDB,
results,
pageCategory
}
}

Expand Down
15 changes: 0 additions & 15 deletions hooks/useLoader.ts

This file was deleted.

37 changes: 28 additions & 9 deletions pages/[subcategory]/index.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
import React from 'react'
import { TopBar } from 'components/TopBar/TopBar'
import { useRouter } from 'next/router'
import Head from 'next/head'
import useFilterDB from 'hooks/useFilterDB'
import CardsList from 'components/Cards/CardsList'
import ComingSoon from 'components/NewIssue/NewIssue'
import { useResults } from 'hooks/ResultsContext'

const SubCategory = () => {
const router = useRouter()
const { results } = useResults()
const title = `LinksHub - ${router.asPath
.charAt(1)
.toUpperCase()}${router.asPath.slice(2)}`
const { filterDB } = useFilterDB()
import { subCategories } from 'database/data'
import { GetStaticProps, NextPage } from 'next'
import { ParsedUrlQuery } from 'querystring'

interface PageProps {
subcategory: string
}

interface Params extends ParsedUrlQuery, PageProps{}

const SubCategory: NextPage<PageProps> = ({subcategory}) => {
const {filterDB, results, pageCategory} = useFilterDB(subcategory)
const title = `LinksHub - ${pageCategory[0].toUpperCase() + pageCategory.slice(1)}`
let content: JSX.Element[] | JSX.Element

if (filterDB.length > 0) {
Expand Down Expand Up @@ -95,4 +98,20 @@ const SubCategory = () => {
)
}

export const getStaticPaths = async () => {
const paths = subCategories.map(subcategory => ({
params: {subcategory}
}))

return {paths, fallback: false}
}

export const getStaticProps: GetStaticProps = async (context) => {
const {subcategory} = context.params as Params

return {props: {
subcategory
}}
}

export default SubCategory
1 change: 1 addition & 0 deletions pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Head from 'next/head'
import Logo from 'components/logo/logo'
import TypewriterComponent from 'typewriter-effect'
import { sidebarData } from '../database/data'

export default function Home() {
//storing sub categories names for using in typewriter effect
const subCategoriesNames: string[] = []
Expand Down
5 changes: 0 additions & 5 deletions types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,6 @@ export interface IContext {
toggleNav?: () => void
}

export interface IUseFilterDBResponse {
filterSubCat: IData[][]
filterDB: IData[][]
}

export const subcategoryArray = [
// devops
'cicd',
Expand Down
Loading