fix: redirect to the error page

feat/roadmap-slug
Arik Chakma 11 months ago
parent 7c1c295c63
commit 4fcf1f6d50
  1. 11
      src/pages/[roadmapId]/[...topicId].astro
  2. 25
      src/pages/[roadmapId]/index.astro
  3. 17
      src/pages/[roadmapId]/index.json.ts
  4. 29
      src/pages/best-practices/[bestPracticeId]/index.astro
  5. 48
      src/pages/best-practices/[bestPracticeId]/index.json.ts
  6. 3
      tsconfig.json

@ -1,10 +1,5 @@
--- ---
import RoadmapBanner from '../../components/RoadmapBanner.astro'; import { getRoadmapTopicFiles } from '../../lib/roadmap-topic';
import BaseLayout from '../../layouts/BaseLayout.astro';
import {
getRoadmapTopicFiles,
type RoadmapTopicFileType,
} from '../../lib/roadmap-topic';
export const partial = true; export const partial = true;
@ -16,11 +11,9 @@ if (!topicId) {
const topicSlug = `/${roadmapId}/${topicId}`; const topicSlug = `/${roadmapId}/${topicId}`;
const topicPathMapping = await getRoadmapTopicFiles(); const topicPathMapping = await getRoadmapTopicFiles();
console.log(Astro.params);
const topicDetails = topicPathMapping[topicSlug]; const topicDetails = topicPathMapping[topicSlug];
const { file, url, roadmap, heading } = topicDetails; const { file } = topicDetails;
const fileWithoutBasePath = file.file?.replace(/.+?\/src\/data/, '/src/data'); const fileWithoutBasePath = file.file?.replace(/.+?\/src\/data/, '/src/data');
const gitHubUrl = `https://github.com/kamranahmedse/developer-roadmap/tree/master${fileWithoutBasePath}`; const gitHubUrl = `https://github.com/kamranahmedse/developer-roadmap/tree/master${fileWithoutBasePath}`;

@ -1,7 +1,6 @@
--- ---
import FAQs from '../../components/FAQs/FAQs.astro'; import FAQs from '../../components/FAQs/FAQs.astro';
import FrameRenderer from '../../components/FrameRenderer/FrameRenderer.astro'; import FrameRenderer from '../../components/FrameRenderer/FrameRenderer.astro';
import MarkdownFile from '../../components/MarkdownFile.astro';
import RelatedRoadmaps from '../../components/RelatedRoadmaps.astro'; import RelatedRoadmaps from '../../components/RelatedRoadmaps.astro';
import RoadmapHeader from '../../components/RoadmapHeader.astro'; import RoadmapHeader from '../../components/RoadmapHeader.astro';
import ShareIcons from '../../components/ShareIcons/ShareIcons.astro'; import ShareIcons from '../../components/ShareIcons/ShareIcons.astro';
@ -13,19 +12,31 @@ import {
generateArticleSchema, generateArticleSchema,
generateFAQSchema, generateFAQSchema,
} from '../../lib/jsonld-schema'; } from '../../lib/jsonld-schema';
import { type RoadmapFrontmatter, getRoadmapIds } from '../../lib/roadmap'; import { type RoadmapFrontmatter } from '../../lib/roadmap';
interface Params extends Record<string, string | undefined> { interface Params extends Record<string, string | undefined> {
roadmapId: string; roadmapId: string;
} }
const { roadmapId } = Astro.params as Params; const { roadmapId } = Astro.params as Params;
const roadmapFile = await import( let roadmapFile;
let roadmapFAQs = [];
try {
roadmapFile = await import(
`../../data/roadmaps/${roadmapId}/${roadmapId}.md` `../../data/roadmaps/${roadmapId}/${roadmapId}.md`
); );
const { faqs: roadmapFAQs = [] } = await import(
`../../data/roadmaps/${roadmapId}/faqs.astro` if (!roadmapFile) {
); return Astro.redirect('/404');
}
const { faqs } = await import(`../../data/roadmaps/${roadmapId}/faqs.astro`);
roadmapFAQs = faqs || [];
} catch (error) {
return Astro.redirect('/404');
}
const roadmapData = roadmapFile.frontmatter as RoadmapFrontmatter; const roadmapData = roadmapFile.frontmatter as RoadmapFrontmatter;
let jsonLdSchema = []; let jsonLdSchema = [];

@ -3,6 +3,7 @@ import type { APIRoute } from 'astro';
export const GET: APIRoute = async function ({ params, request, props }) { export const GET: APIRoute = async function ({ params, request, props }) {
const { roadmapId } = params; const { roadmapId } = params;
try {
const roadmapJson = await import( const roadmapJson = await import(
`../../data/roadmaps/${roadmapId}/${roadmapId}.json` `../../data/roadmaps/${roadmapId}/${roadmapId}.json`
); );
@ -13,4 +14,20 @@ export const GET: APIRoute = async function ({ params, request, props }) {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
}, },
}); });
} catch (error) {
return new Response(
JSON.stringify({
data: null,
error: {
message: 'Roadmap not found',
},
}),
{
status: 500,
headers: {
'Content-Type': 'application/json',
},
},
);
}
}; };

@ -10,21 +10,9 @@ import { UserProgressModal } from '../../../components/UserProgress/UserProgress
import { import {
type BestPracticeFileType, type BestPracticeFileType,
type BestPracticeFrontmatter, type BestPracticeFrontmatter,
getAllBestPractices,
} from '../../../lib/best-pratice'; } from '../../../lib/best-pratice';
import { generateArticleSchema } from '../../../lib/jsonld-schema'; import { generateArticleSchema } from '../../../lib/jsonld-schema';
export async function getStaticPaths() {
const bestPractices = await getAllBestPractices();
return bestPractices.map((bestPractice: BestPracticeFileType) => ({
params: { bestPracticeId: bestPractice.id },
props: {
bestPractice: bestPractice,
},
}));
}
interface Params extends Record<string, string | undefined> { interface Params extends Record<string, string | undefined> {
bestPracticeId: string; bestPracticeId: string;
} }
@ -34,7 +22,20 @@ interface Props {
} }
const { bestPracticeId } = Astro.params as Params; const { bestPracticeId } = Astro.params as Params;
const { bestPractice } = Astro.props as Props;
let bestPractice;
try {
bestPractice = await import(
`../../../data/best-practices/${bestPracticeId}/${bestPracticeId}.md`
);
if (!bestPractice) {
return Astro.redirect('/404');
}
} catch (error) {
return Astro.redirect('/404');
}
const bestPracticeData = bestPractice.frontmatter as BestPracticeFrontmatter; const bestPracticeData = bestPractice.frontmatter as BestPracticeFrontmatter;
let jsonLdSchema = []; let jsonLdSchema = [];
@ -49,7 +50,7 @@ if (bestPracticeData.schema) {
datePublished: bestPracticeSchema.datePublished, datePublished: bestPracticeSchema.datePublished,
dateModified: bestPracticeSchema.dateModified, dateModified: bestPracticeSchema.dateModified,
imageUrl: bestPracticeSchema.imageUrl, imageUrl: bestPracticeSchema.imageUrl,
}) }),
); );
} }
--- ---

@ -1,33 +1,33 @@
import type { APIRoute } from 'astro'; import type { APIRoute } from 'astro';
export async function getStaticPaths() { export const GET: APIRoute = async function ({ params, request, props }) {
const bestPracticeJsons = await import.meta.glob( const { bestPracticeId } = params;
'/src/data/best-practices/**/*.json',
{
eager: true,
}
);
return Object.keys(bestPracticeJsons).map((filePath) => {
const bestPracticeId = filePath.split('/').pop()?.replace('.json', '');
const bestPracticeJson = bestPracticeJsons[filePath] as Record<string, any>;
return { try {
params: { const roadmapJson = await import(
bestPracticeId, `../../../data/best-practices/${bestPracticeId}/${bestPracticeId}.json`
}, );
props: {
bestPracticeJson: bestPracticeJson?.default,
},
};
});
}
export const GET: APIRoute = async function ({ params, request, props }) { return new Response(JSON.stringify(roadmapJson), {
return new Response(JSON.stringify(props.bestPracticeJson), {
status: 200, status: 200,
headers: { headers: {
'content-type': 'application/json', 'Content-Type': 'application/json',
}, },
}); });
} catch (error) {
return new Response(
JSON.stringify({
data: null,
error: {
message: 'Best Practices not found',
},
}),
{
status: 500,
headers: {
'Content-Type': 'application/json',
},
},
);
}
}; };

@ -4,5 +4,6 @@
"moduleResolution": "node", "moduleResolution": "node",
"jsx": "react-jsx", "jsx": "react-jsx",
"jsxImportSource": "react" "jsxImportSource": "react"
} },
"exclude": ["node_modules", "dist"]
} }
Loading…
Cancel
Save