Skip to content

Commit

Permalink
feat: add 404
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentHardouin committed May 9, 2024
1 parent e3b4150 commit 41bb97b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
17 changes: 17 additions & 0 deletions src/pages/NotFound.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<script setup>
</script>

<template>
<div class="container">
<h1>Not Found</h1>
<p>Sorry, the page you are looking for does not exist.</p>
<router-link to="/">
Go to Home
</router-link>
</div>
</template>

<style scoped>
</style>
9 changes: 7 additions & 2 deletions src/pages/Post.vue
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
<script setup>
import { useRoute } from 'vue-router';
import { useRoute, useRouter } from 'vue-router';
import { ref, watchEffect } from 'vue';
import * as posts from '../../docs/diary/*.md';
const route = useRoute();
const router = useRouter();
const slug = route.params.slug;
const markdown = ref(null);
watchEffect(async () => {
markdown.value = posts[slug].default;
const post = posts[slug];
if (!post)
return router.replace('/404');
markdown.value = post.default;
});
</script>

Expand Down
2 changes: 2 additions & 0 deletions src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { createRouter, createWebHistory } from 'vue-router';
import Index from './pages/Index.vue';
import Post from './pages/Post.vue';
import Posts from './pages/Posts.vue';
import PageNotFound from './pages/NotFound.vue';

const router = createRouter({
history: createWebHistory(),
Expand All @@ -10,6 +11,7 @@ const router = createRouter({
{ path: '/practice', name: 'Practice', component: Index },
{ path: '/posts/', name: 'Posts', component: Posts },
{ path: '/posts/:slug', name: 'Post', component: Post },
{ path: '/:pathMatch(.*)*', name: 'NotFound', component: PageNotFound },
],
});

Expand Down

0 comments on commit 41bb97b

Please sign in to comment.