Skip to content

Commit

Permalink
feat: empty content handling
Browse files Browse the repository at this point in the history
  • Loading branch information
AldemirLucas committed Dec 5, 2024
1 parent 19e126b commit 7cc8bae
Show file tree
Hide file tree
Showing 12 changed files with 76 additions and 126 deletions.
17 changes: 0 additions & 17 deletions next/blog/en/dev.md

This file was deleted.

17 changes: 0 additions & 17 deletions next/blog/es/dev.md

This file was deleted.

8 changes: 0 additions & 8 deletions next/content/FAQ/en/test.md

This file was deleted.

8 changes: 0 additions & 8 deletions next/content/FAQ/es/test.md

This file was deleted.

19 changes: 0 additions & 19 deletions next/content/caseStudies/en/dev.md

This file was deleted.

19 changes: 0 additions & 19 deletions next/content/caseStudies/es/dev.md

This file was deleted.

14 changes: 9 additions & 5 deletions next/pages/api/blog/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) =>
Expand Down
48 changes: 28 additions & 20 deletions next/pages/api/caseStudies/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) =>
Expand Down
31 changes: 18 additions & 13 deletions next/pages/api/faqs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 []
}
}
10 changes: 10 additions & 0 deletions next/pages/blog/[slug].js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 10 additions & 0 deletions next/pages/case-studies/[id].js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions next/pages/services.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ function CaseStudies ({ data }) {

return (
<Stack
display={CaseStudiesPages.length === 0 ? "none" : "flex"}
id="case-studies"
width="100%"
maxWidth="1264px"
Expand Down

0 comments on commit 7cc8bae

Please sign in to comment.