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

@ -1,7 +1,6 @@
---
import FAQs from '../../components/FAQs/FAQs.astro';
import FrameRenderer from '../../components/FrameRenderer/FrameRenderer.astro';
import MarkdownFile from '../../components/MarkdownFile.astro';
import RelatedRoadmaps from '../../components/RelatedRoadmaps.astro';
import RoadmapHeader from '../../components/RoadmapHeader.astro';
import ShareIcons from '../../components/ShareIcons/ShareIcons.astro';
@ -13,19 +12,31 @@ import {
generateArticleSchema,
generateFAQSchema,
} from '../../lib/jsonld-schema';
import { type RoadmapFrontmatter, getRoadmapIds } from '../../lib/roadmap';
import { type RoadmapFrontmatter } from '../../lib/roadmap';
interface Params extends Record<string, string | undefined> {
roadmapId: string;
}
const { roadmapId } = Astro.params as Params;
const roadmapFile = await import(
let roadmapFile;
let roadmapFAQs = [];
try {
roadmapFile = await import(
`../../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;
let jsonLdSchema = [];

@ -3,6 +3,7 @@ import type { APIRoute } from 'astro';
export const GET: APIRoute = async function ({ params, request, props }) {
const { roadmapId } = params;
try {
const roadmapJson = await import(
`../../data/roadmaps/${roadmapId}/${roadmapId}.json`
);
@ -13,4 +14,20 @@ export const GET: APIRoute = async function ({ params, request, props }) {
'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 {
type BestPracticeFileType,
type BestPracticeFrontmatter,
getAllBestPractices,
} from '../../../lib/best-pratice';
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> {
bestPracticeId: string;
}
@ -34,7 +22,20 @@ interface Props {
}
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;
let jsonLdSchema = [];
@ -49,7 +50,7 @@ if (bestPracticeData.schema) {
datePublished: bestPracticeSchema.datePublished,
dateModified: bestPracticeSchema.dateModified,
imageUrl: bestPracticeSchema.imageUrl,
})
}),
);
}
---

@ -1,33 +1,33 @@
import type { APIRoute } from 'astro';
export async function getStaticPaths() {
const bestPracticeJsons = await import.meta.glob(
'/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>;
export const GET: APIRoute = async function ({ params, request, props }) {
const { bestPracticeId } = params;
return {
params: {
bestPracticeId,
},
props: {
bestPracticeJson: bestPracticeJson?.default,
},
};
});
}
try {
const roadmapJson = await import(
`../../../data/best-practices/${bestPracticeId}/${bestPracticeId}.json`
);
export const GET: APIRoute = async function ({ params, request, props }) {
return new Response(JSON.stringify(props.bestPracticeJson), {
return new Response(JSON.stringify(roadmapJson), {
status: 200,
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",
"jsx": "react-jsx",
"jsxImportSource": "react"
}
},
"exclude": ["node_modules", "dist"]
}
Loading…
Cancel
Save