From 7cc8baead4057afe9a481cefe7b705c6c7227b48 Mon Sep 17 00:00:00 2001 From: aldemirLucas Date: Thu, 5 Dec 2024 15:29:21 -0300 Subject: [PATCH] feat: empty content handling --- next/blog/en/dev.md | 17 ---------- next/blog/es/dev.md | 17 ---------- next/content/FAQ/en/test.md | 8 ----- next/content/FAQ/es/test.md | 8 ----- next/content/caseStudies/en/dev.md | 19 ------------ next/content/caseStudies/es/dev.md | 19 ------------ next/pages/api/blog/index.js | 14 ++++++--- next/pages/api/caseStudies/index.js | 48 +++++++++++++++++------------ next/pages/api/faqs/index.js | 31 +++++++++++-------- next/pages/blog/[slug].js | 10 ++++++ next/pages/case-studies/[id].js | 10 ++++++ next/pages/services.js | 1 + 12 files changed, 76 insertions(+), 126 deletions(-) delete mode 100644 next/blog/en/dev.md delete mode 100644 next/blog/es/dev.md delete mode 100644 next/content/FAQ/en/test.md delete mode 100644 next/content/FAQ/es/test.md delete mode 100644 next/content/caseStudies/en/dev.md delete mode 100644 next/content/caseStudies/es/dev.md diff --git a/next/blog/en/dev.md b/next/blog/en/dev.md deleted file mode 100644 index 3743719c..00000000 --- a/next/blog/en/dev.md +++ /dev/null @@ -1,17 +0,0 @@ ---- -title: Dev -description: Dev -date: - created: "2021-11-25T15:00:00" -authors: - - name: Dev - role: Dev - social: Dev -thumbnail: /blog/analisando-a-frota-brasileira-com-a-bd/image_0.webp -categories: ["Dev"] -medium_slug: >- - Dev -published: true ---- - -Dev \ No newline at end of file diff --git a/next/blog/es/dev.md b/next/blog/es/dev.md deleted file mode 100644 index 3743719c..00000000 --- a/next/blog/es/dev.md +++ /dev/null @@ -1,17 +0,0 @@ ---- -title: Dev -description: Dev -date: - created: "2021-11-25T15:00:00" -authors: - - name: Dev - role: Dev - social: Dev -thumbnail: /blog/analisando-a-frota-brasileira-com-a-bd/image_0.webp -categories: ["Dev"] -medium_slug: >- - Dev -published: true ---- - -Dev \ No newline at end of file diff --git a/next/content/FAQ/en/test.md b/next/content/FAQ/en/test.md deleted file mode 100644 index 27ac2e6f..00000000 --- a/next/content/FAQ/en/test.md +++ /dev/null @@ -1,8 +0,0 @@ ---- -question: Como acessar as fontes originais? -categories: [Dados] -keywords: acesso, consulta, fonte original, externo -id: access-external-links ---- - -Você pode acessar as fontes originais dos dados clicando no botão de redirecionamento para o link externo à plataforma. diff --git a/next/content/FAQ/es/test.md b/next/content/FAQ/es/test.md deleted file mode 100644 index 27ac2e6f..00000000 --- a/next/content/FAQ/es/test.md +++ /dev/null @@ -1,8 +0,0 @@ ---- -question: Como acessar as fontes originais? -categories: [Dados] -keywords: acesso, consulta, fonte original, externo -id: access-external-links ---- - -Você pode acessar as fontes originais dos dados clicando no botão de redirecionamento para o link externo à plataforma. diff --git a/next/content/caseStudies/en/dev.md b/next/content/caseStudies/en/dev.md deleted file mode 100644 index a6ba26ae..00000000 --- a/next/content/caseStudies/en/dev.md +++ /dev/null @@ -1,19 +0,0 @@ ---- -id: "dev" -displayTitle: "Dev" -thumbnail: "https://storage.googleapis.com/basedosdados-website/estudos_de_caso/thumbnails/thumbnail_estudo_de_caso_flemann.png" -title: "Dev" -img: "https://storage.googleapis.com/basedosdados-website/estudos_de_caso/imagens/estudo_de_caso_flemann.png" -imgDescription: "Dev" -description: "Dev" -logo: { - img: "https://storage.googleapis.com/basedosdados-website/estudos_de_caso/logos/flemann.png", - width: 245, - height: 85 -} -about: "Dev" -resume: "Dev" -sector: "Dev" ---- - -Você pode acessar as fontes originais dos dados clicando no botão de redirecionamento para o link externo à plataforma. diff --git a/next/content/caseStudies/es/dev.md b/next/content/caseStudies/es/dev.md deleted file mode 100644 index a6ba26ae..00000000 --- a/next/content/caseStudies/es/dev.md +++ /dev/null @@ -1,19 +0,0 @@ ---- -id: "dev" -displayTitle: "Dev" -thumbnail: "https://storage.googleapis.com/basedosdados-website/estudos_de_caso/thumbnails/thumbnail_estudo_de_caso_flemann.png" -title: "Dev" -img: "https://storage.googleapis.com/basedosdados-website/estudos_de_caso/imagens/estudo_de_caso_flemann.png" -imgDescription: "Dev" -description: "Dev" -logo: { - img: "https://storage.googleapis.com/basedosdados-website/estudos_de_caso/logos/flemann.png", - width: 245, - height: 85 -} -about: "Dev" -resume: "Dev" -sector: "Dev" ---- - -Você pode acessar as fontes originais dos dados clicando no botão de redirecionamento para o link externo à plataforma. diff --git a/next/pages/api/blog/index.js b/next/pages/api/blog/index.js index 0661dff0..11e749fa 100644 --- a/next/pages/api/blog/index.js +++ b/next/pages/api/blog/index.js @@ -49,14 +49,18 @@ export async function getAllPosts(locale = 'pt') { return posts; } catch (error) { console.error("Error reading posts:", error); - throw error; + return [] } } -export async function getPostBySlug(slug, locale) { - const blogpostsDir = path.join(root, `blog/${locale}`) - const filepath = path.join(blogpostsDir, `${slug}.md`); - return await fs.readFile(filepath, "utf-8"); +export async function getPostBySlug(slug, locale = 'pt') { + try { + const blogpostsDir = path.join(root, `blog/${locale}`) + const filepath = path.join(blogpostsDir, `${slug}.md`); + return await fs.readFile(filepath, "utf-8"); + } catch (error) { + return null; + } } const remarkPluginCaption = () => (tree) => diff --git a/next/pages/api/caseStudies/index.js b/next/pages/api/caseStudies/index.js index 6063361c..0be38f07 100644 --- a/next/pages/api/caseStudies/index.js +++ b/next/pages/api/caseStudies/index.js @@ -11,29 +11,37 @@ const root = process.cwd(); export async function getAllCaseStudies(locale = 'pt') { const caseStudiesDirRoot = path.join(root, `content/caseStudies/${locale}`); - const caseStudiesDir = await fs.readdir(caseStudiesDirRoot, "utf-8"); - - const caseStudies = await Promise.all( - caseStudiesDir.map(async (file) => { - const fullpath = path.join(caseStudiesDirRoot, file); - const content = await fs.readFile(fullpath, "utf-8"); - - const { data: metadata, content: caseStudiesContent } = matter(content); - - return { - ...metadata, - content: caseStudiesContent, - }; - }), - ); - - return caseStudies; + try { + const caseStudiesDir = await fs.readdir(caseStudiesDirRoot, "utf-8"); + + const caseStudies = await Promise.all( + caseStudiesDir.map(async (file) => { + const fullpath = path.join(caseStudiesDirRoot, file); + const content = await fs.readFile(fullpath, "utf-8"); + + const { data: metadata, content: caseStudiesContent } = matter(content); + + return { + ...metadata, + content: caseStudiesContent, + }; + }), + ); + + return caseStudies; + } catch (error) { + return [] + } } export async function getCaseStudiesById(id, locale = 'pt') { - const caseStudiesDirRoot = path.join(root, `content/caseStudies/${locale}`); - const filepath = path.join(caseStudiesDirRoot, `${id}.md`); - return await fs.readFile(filepath, "utf-8"); + try { + const caseStudiesDirRoot = path.join(root, `content/caseStudies/${locale}`); + const filepath = path.join(caseStudiesDirRoot, `${id}.md`); + return await fs.readFile(filepath, "utf-8"); + } catch (error) { + return null + } } const remarkPluginCaption = () => (tree) => diff --git a/next/pages/api/faqs/index.js b/next/pages/api/faqs/index.js index 3582070a..18ba1bd6 100644 --- a/next/pages/api/faqs/index.js +++ b/next/pages/api/faqs/index.js @@ -3,23 +3,28 @@ import fs from "fs/promises"; import matter from "gray-matter"; const root = process.cwd(); + export async function getAllFAQs(locale = 'pt') { const faqsDirRoot = path.join(root, `content/FAQ/${locale}`); - const faqsDir = await fs.readdir(faqsDirRoot, "utf-8"); + try { + const faqsDir = await fs.readdir(faqsDirRoot, "utf-8"); - const faqs = await Promise.all( - faqsDir.map(async (file) => { - const fullpath = path.join(faqsDirRoot, file); - const content = await fs.readFile(fullpath, "utf-8"); + const faqs = await Promise.all( + faqsDir.map(async (file) => { + const fullpath = path.join(faqsDirRoot, file); + const content = await fs.readFile(fullpath, "utf-8"); - const { data: metadata, content: faqContent } = matter(content); + const { data: metadata, content: faqContent } = matter(content); - return { - ...metadata, - content: faqContent, - }; - }), - ); + return { + ...metadata, + content: faqContent, + }; + }), + ); - return faqs; + return faqs; + } catch (error) { + return [] + } } \ No newline at end of file diff --git a/next/pages/blog/[slug].js b/next/pages/blog/[slug].js index 7df90f8a..44084e7c 100644 --- a/next/pages/blog/[slug].js +++ b/next/pages/blog/[slug].js @@ -40,6 +40,16 @@ export async function getStaticProps({ params, locale }) { const { slug } = params; const content = await getPostBySlug(slug, locale); + + if (!content) { + return { + redirect: { + destination: locale === "pt" ? "/blog" : `/${locale}/blog`, + permanent: false, + }, + }; + } + const serialize = await serializePost(content); return { diff --git a/next/pages/case-studies/[id].js b/next/pages/case-studies/[id].js index a684b14d..542c5c6a 100644 --- a/next/pages/case-studies/[id].js +++ b/next/pages/case-studies/[id].js @@ -33,6 +33,16 @@ export async function getStaticProps({ params, locale }) { const { id } = params; const content = await getCaseStudiesById(id, locale); + + if (!content) { + return { + redirect: { + destination: locale === "pt" ? "/services" : `/${locale}/services`, + permanent: false, + }, + }; + } + const serialize = await serializeCaseStudies(content); return { diff --git a/next/pages/services.js b/next/pages/services.js index 52d9a39b..0db2f4b0 100644 --- a/next/pages/services.js +++ b/next/pages/services.js @@ -224,6 +224,7 @@ function CaseStudies ({ data }) { return (