Skip to content

Commit

Permalink
add generic 404 page
Browse files Browse the repository at this point in the history
  • Loading branch information
vorant94 committed Apr 10, 2024
1 parent 8d4f53a commit a9a4cf8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 2 additions & 0 deletions packages/blog-ssr/src/posts/handlers/post.handler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export const postHandler: FastifyPluginAsync = async function (app) {
post = await queryPost(request.params.slug);
} catch (e) {
if (isErrnoException(e)) {
// TODO make 404 response handling DRY,
// see https://github.com/fastify/fastify/issues/402#issuecomment-340370348
return reply
.code(statusCode.notFound)
.type('text/html')
Expand Down
16 changes: 15 additions & 1 deletion packages/blog-ssr/src/server.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { envSchema, isCiEnv } from '@/config/index.js';
import { homeModule } from '@/home/index.js';
import { NotFound, statusCode } from '@/network/index.js';
import { postsModule } from '@/posts/index.js';
import { projectsModule } from '@/projects/projects.module.js';
import { uiModule } from '@/ui/index.js';
import { CenteredLayout, render, uiModule } from '@/ui/index.js';
import fastifyStatic from '@fastify/static';
import consola from 'consola';
import fastify from 'fastify';
Expand Down Expand Up @@ -32,6 +33,19 @@ await app.register(homeModule);
await app.register(postsModule);
await app.register(projectsModule);

app.setNotFoundHandler(async (_, reply) => {
return reply
.status(statusCode.notFound)
.type(`text/html`)
.send(
render(
<CenteredLayout title={`404 Not found`}>
<NotFound />
</CenteredLayout>,
),
);
});

app.listen({ port: env.PORT }, (err, address) => {
if (err) {
consola.error(err);
Expand Down

0 comments on commit a9a4cf8

Please sign in to comment.