Skip to content

Commit

Permalink
#16 added error checks for fetch calls, #30 basic head info
Browse files Browse the repository at this point in the history
  • Loading branch information
alkmst-xyz committed May 6, 2023
1 parent e00499d commit 0d24ae4
Show file tree
Hide file tree
Showing 15 changed files with 110 additions and 37 deletions.
2 changes: 1 addition & 1 deletion src/lib/components/PostsList.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts">
import type { MdMeta } from '$lib/api/content/types';
import type { MdMeta } from '../../routes/api/content/types';
export let posts: MdMeta[];
export let postsPath: string = '/posts';
Expand Down
19 changes: 0 additions & 19 deletions src/routes/+layout.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,9 @@
import type { LayoutLoad } from './$types';

// set all pages to prerendered
export const prerender = true;

// JS is needed on the client for page transitions and link prefetching.
// change to false to ship without any JS
// or to dev for HMR in development
// import { dev } from '$app/environment';
// export const csr = dev;
export const csr = true;

export const load = (({ url }) => {
// import { error } from '@sveltejs/kit';
// try {
// return {
// /**
// * The current path
// */
// path: url.pathname
// };
// } catch (err) {
// throw error(500, err);
// }

return {
/**
* The current path
Expand Down
4 changes: 4 additions & 0 deletions src/routes/api/content/[slug]/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ export const GET: RequestHandler = async ({ params, fetch }) => {
}

const response = await fetch('/api/content');
if (!response.ok) {
throw error(400, 'error loading data from endpoint');
}

const result = (await response.json()) as MdMeta[];

if (id >= result.length) {
Expand Down
8 changes: 6 additions & 2 deletions src/routes/posts/+layout.server.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { error } from '@sveltejs/kit';
import type { LayoutServerLoad } from './$types';
import type { MdMeta } from '../api/content/types';

export const load = (async ({ fetch }) => {
const res = await fetch('/api/content');
const response = await fetch('/api/content');
if (!response.ok) {
throw error(400, 'error loading data from endpoint');
}

return {
posts: (await res.json()) as MdMeta[]
posts: (await response.json()) as MdMeta[]
};
}) satisfies LayoutServerLoad;
13 changes: 11 additions & 2 deletions src/routes/posts/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@
export let data: LayoutServerData;
</script>

<div class="flex flex-col space-y-4">
<PostsList posts={data.posts} />
<svelte:head>
<title>Posts</title>
<meta name="Posts" content="Posts" />
</svelte:head>

<div>
<h1 class="mb-8 px-2">Posts</h1>

<div class="flex flex-col space-y-4">
<PostsList posts={data.posts} />
</div>
</div>
10 changes: 6 additions & 4 deletions src/routes/posts/[slug]/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { error } from '@sveltejs/kit';
import type { PageServerLoad } from './$types';
import type { MdBody } from '../../api/content/types';

export const load = (async ({ parent, params, fetch }) => {
const { posts } = await parent();

// find index of file that matches the slug
const postId = posts.findIndex((x) => x.slug === params.slug);

const res = await fetch(`/api/content/${postId}`);
const response = await fetch(`/api/content/${postId}`);
if (!response.ok) {
throw error(400, 'error loading data from endpoint');
}

return {
postBody: (await res.json()) as MdBody
postBody: (await response.json()) as MdBody
};
}) satisfies PageServerLoad;
39 changes: 36 additions & 3 deletions src/routes/posts/[slug]/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,8 +1,41 @@
<script lang="ts">
import Tag from '$lib/components/Tag.svelte';
import type { PageServerData } from './$types';
export let data: PageServerData;
const { meta, html } = data.postBody;
</script>

<article class="prose">
{@html data.postBody.html}
</article>
<svelte:head>
<title>Posts | {meta.title}</title>
<meta name="Post" content="Post" />
</svelte:head>

<div>
<div class="flex flex-col">
<h1>{meta.title}</h1>

<div class="flex space-x-4">
<p>{meta.date}</p>
<a href="/posts/categories">{meta.category}</a>
</div>

{#if meta.tags.length}
<ul class="flex space-x-2">
{#each meta.tags as tag}
<li>
<Tag tagsPage="/posts/tags" {tag} />
</li>
{/each}
</ul>
{/if}
</div>

<span>
{meta.description}
</span>

<article class="prose">
{@html html}
</article>
</div>
5 changes: 5 additions & 0 deletions src/routes/posts/categories/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { error } from '@sveltejs/kit';
import type { PageServerLoad } from './$types';
import type { MdMeta } from '../../api/content/types';

export const load = (async ({ fetch }) => {
const response = await fetch('/api/content');
if (!response.ok) {
throw error(400, 'error loading data from endpoint');
}

const result = (await response.json()) as MdMeta[];

const allCategories = result.map((x) => x.category);
Expand Down
5 changes: 5 additions & 0 deletions src/routes/posts/categories/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
export let data: PageServerData;
</script>

<svelte:head>
<title>Posts | Categories</title>
<meta name="Posts | Categories" content="Posts | Categories" />
</svelte:head>

<div class="flex flex-wrap gap-2">
{#each data.categories as category}
<a
Expand Down
8 changes: 7 additions & 1 deletion src/routes/posts/categories/[slug]/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import { error } from '@sveltejs/kit';
import type { PageServerLoad } from './$types';
import type { MdMeta } from '../../../api/content/types';

export const load = (async ({ params, fetch }) => {
const response = await fetch('/api/content');
if (!response.ok) {
throw error(400, 'error loading data from endpoint');
}

const result = (await response.json()) as MdMeta[];

return {
posts: result.filter((x) => x.category === params.slug)
posts: result.filter((x) => x.category === params.slug),
category: params.slug
};
}) satisfies PageServerLoad;
5 changes: 5 additions & 0 deletions src/routes/posts/categories/[slug]/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
export let data: PageServerData;
</script>

<svelte:head>
<title>Posts in {data.category}</title>
<meta name="Posts per category" content="Posts per category" />
</svelte:head>

<div class="flex flex-col space-y-4">
<PostsList posts={data.posts} />
</div>
5 changes: 5 additions & 0 deletions src/routes/posts/tags/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { error } from '@sveltejs/kit';
import type { PageServerLoad } from './$types';
import type { MdMeta } from '../../api/content/types';

export const load = (async ({ fetch }) => {
const response = await fetch('/api/content');
if (!response.ok) {
throw error(400, 'error loading data from endpoint');
}

const result = (await response.json()) as MdMeta[];

const allTags = result.map((x) => x.tags).flat();
Expand Down
5 changes: 5 additions & 0 deletions src/routes/posts/tags/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
export let data: PageServerData;
</script>

<svelte:head>
<title>Posts | Tags</title>
<meta name="Posts | Tags" content="Posts | Tags" />
</svelte:head>

<div class="flex flex-wrap gap-2">
{#each data.tags as tag}
<a
Expand Down
12 changes: 8 additions & 4 deletions src/routes/posts/tags/[slug]/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { error } from '@sveltejs/kit';
import type { PageServerLoad } from './$types';
import type { MdMeta } from '../../api/content/types';
import type { MdMeta } from '../../../api/content/types';

export const load = (async ({ params, fetch }) => {
const response = await fetch('/api/content');
const result = (await response.json()) as MdMeta[];
if (!response.ok) {
throw error(400, 'error loading data from endpoint');
}

const allMatchedMetdata = result.filter((x) => x.tags.includes(params.slug));
const result = (await response.json()) as MdMeta[];

return {
allMatchedMetdata
posts: result.filter((x) => x.tags.includes(params.slug)),
tag: params.slug
};
}) satisfies PageServerLoad;
7 changes: 6 additions & 1 deletion src/routes/posts/tags/[slug]/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
export let data: PageServerData;
</script>

<svelte:head>
<title>Posts tagged #{data.tag}</title>
<meta name="Posts per tag" content="Posts per tag" />
</svelte:head>

<div class="flex flex-col space-y-4">
<PostsList posts={data.allMatchedMetdata} />
<PostsList posts={data.posts} />
</div>

0 comments on commit 0d24ae4

Please sign in to comment.