Skip to content

Commit

Permalink
feat: Implement dynamic parameter generation for category and slug pa…
Browse files Browse the repository at this point in the history
…ges; refactor file reading logic
  • Loading branch information
SivaramPg committed Dec 21, 2024
1 parent 7f8d0d2 commit b336925
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 38 deletions.
48 changes: 24 additions & 24 deletions src/app/[category]/[slug]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { readFile } from "node:fs/promises"
import { readFile, readdir } from "node:fs/promises"

import path from "node:path"
import ApiEndpointElement from "@/components/ApiEndpointElement"
Expand All @@ -7,33 +7,33 @@ import RequestDisplayElement from "@/components/RequestDisplayElement"
import ResponseDisplayElement from "@/components/ResponseDisplayElement"
import { notFound } from "next/navigation"

export const dynamic = "force-static"
export const dynamicParams = false

// export async function generateStaticParams() {
// const slugs: { category: string; slug: string }[] = []
export async function generateStaticParams() {
const slugs: { category: string; slug: string }[] = []

// const rawCategories = await fsPromises.readdir("public/api/", {
// withFileTypes: true,
// recursive: true,
// })
// for (const category of rawCategories) {
// if (
// category.isFile() &&
// !["all.json", ".DS_Store", "root.json", "schema.json"].includes(
// category.name,
// )
// ) {
// const categoryName = category.path.split("/").at(-1)
const rawCategories = await readdir("public/api/", {
withFileTypes: true,
recursive: true,
})
for (const category of rawCategories) {
if (
category.isFile() &&
!["all.json", ".DS_Store", "root.json", "schema.json"].includes(
category.name,
)
) {
const categoryName = category.path.split("/").at(-1)

// slugs.push({
// category: categoryName ?? "",
// slug: category.name.split(".")[0],
// })
// }
// }
slugs.push({
category: categoryName ?? "",
slug: category.name.split(".")[0],
})
}
}

// return slugs
// }
return slugs
}

async function getCategorySlugJson(category: string, slug: string) {
try {
Expand Down
28 changes: 14 additions & 14 deletions src/app/[category]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { readFile } from "node:fs/promises"
import { readFile, readdir } from "node:fs/promises"

import { cn } from "@/utils/cn"

Expand All @@ -12,23 +12,23 @@ import RequestDisplayElement from "@/components/RequestDisplayElement"
import { LinkPill } from "@/components/link-pill"
import { notFound } from "next/navigation"

export const dynamic = "force-static"
export const dynamicParams = false

// export async function generateStaticParams() {
// const categories: { category: string }[] = []
export async function generateStaticParams() {
const categories: { category: string }[] = []

// const rawCategories = await fsPromises.readdir("public/api", {
// withFileTypes: true,
// })
const rawCategories = await readdir("public/api", {
withFileTypes: true,
})

// for (const category of rawCategories) {
// if (category.isDirectory()) {
// categories.push({ category: category.name })
// }
// }
for (const category of rawCategories) {
if (category.isDirectory()) {
categories.push({ category: category.name })
}
}

// return categories
// }
return categories
}

async function getCategoryAllJson(category: string) {
try {
Expand Down

0 comments on commit b336925

Please sign in to comment.