Skip to content

Commit 23184e4

Browse files
committed
render not found page in case of 404 response
1 parent 0e3e559 commit 23184e4

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

web/cloudflare/functions/projects/[slug].ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
declare const htmlTemplate: string;
2-
// @ts-expect-error cloudflare converts this to a string using esbuild
1+
declare const htmlTemplate: string; // @ts-expect-error cloudflare converts this to a string using esbuild
32
import htmlTemplate from "../../public/template.html";
3+
declare const notFoundEn: string; // @ts-expect-error cloudflare converts this to a string using esbuild
4+
import notFoundEn from "../../public/404.html";
45

56
import { fsConfig } from "@dzcode.io/utils/dist/config";
67
import { Environment, environments } from "@dzcode.io/utils/dist/config/environment";
@@ -24,11 +25,14 @@ export const onRequest: PagesFunction<Env> = async (context) => {
2425

2526
const pathName = new URL(context.request.url).pathname;
2627

27-
const slug = pathName.split("/").pop();
28-
const projectId = slug?.split("-").pop();
28+
const projectIdRegex = /projects\/(.*)-(.\d)+/;
29+
const projectId = pathName?.match(projectIdRegex)?.[2];
2930

30-
// @TODO-ZM: render 404 page
31-
if (!projectId) return new Response("Not found", { status: 404 });
31+
if (!projectId)
32+
return new Response(notFoundEn, {
33+
headers: { "content-type": "text/html; charset=utf-8" },
34+
status: 404,
35+
});
3236

3337
// @TODO-ZM: get language from request url
3438
const language = "en";
@@ -45,9 +49,5 @@ export const onRequest: PagesFunction<Env> = async (context) => {
4549
.replace(/{{template-title}}/g, pageTitle)
4650
.replace(/{{template-description}}/g, localize("projects-description"));
4751

48-
return new Response(newData, {
49-
headers: {
50-
"content-type": "text/html; charset=utf-8",
51-
},
52-
});
52+
return new Response(newData, { headers: { "content-type": "text/html; charset=utf-8" } });
5353
};

0 commit comments

Comments
 (0)