Update @sveltekit to next.405

This commit is contained in:
Thomas 2022-08-21 17:18:06 +01:00
parent 80d062f474
commit e8f10187ee
13 changed files with 116 additions and 132 deletions

View file

@ -1,7 +1,3 @@
<script context="module">
export const prerender = true;
</script>
<script lang="ts"> <script lang="ts">
import { slide } from 'svelte/transition'; import { slide } from 'svelte/transition';
import Navbar from '../components/Navbar.svelte'; import Navbar from '../components/Navbar.svelte';

1
src/routes/+page.ts Normal file
View file

@ -0,0 +1 @@
export const prerender = true;

View file

@ -1,4 +1,5 @@
import allPosts from '../../content/posts.json'; import { json as json$1 } from '@sveltejs/kit';
import allPosts from '../../../content/posts.json';
export const GET = async ({ url }) => { export const GET = async ({ url }) => {
try { try {
@ -16,19 +17,15 @@ export const GET = async ({ url }) => {
return 0; return 0;
}); });
return { return json$1({
status: 200,
body: {
posts: sortedBlogPosts posts: sortedBlogPosts
} });
};
} catch (error) { } catch (error) {
console.error({ error: JSON.stringify(error) }); console.error({ error: JSON.stringify(error) });
return { return json$1({
status: 500,
body: {
error: 'Could not fetch posts. ' + error error: 'Could not fetch posts. ' + error
} }, {
}; status: 500
});
} }
}; };

View file

@ -1,20 +0,0 @@
import { fetchBlogPostBySlug } from '$lib';
import type { LoadInput } from '@sveltejs/kit';
export const GET = async ({ params }: LoadInput) => {
const { slug } = params;
const post = await fetchBlogPostBySlug(slug);
if (!post) {
return {
status: 404,
body: {}
};
}
return {
status: 200,
body: { post }
};
};

View file

@ -0,0 +1,14 @@
import { json, type LoadEvent, error } from '@sveltejs/kit';
import { fetchBlogPostBySlug } from '$lib';
export const GET = async ({ params }: LoadEvent) => {
const { slug } = params;
const post = await fetchBlogPostBySlug(slug);
if (!post) {
throw error(404, `Could not find blog post with slug '${slug}'`);
}
return json({ post });
};

View file

@ -1,75 +1,43 @@
<script lang="ts" context="module"> <script lang="ts">
import type { Load, LoadInput, LoadOutput } from '@sveltejs/kit'; import type { PageData } from './$types';
import { differenceInCalendarDays, intlFormat } from 'date-fns'; import Navbar from '../../components/Navbar.svelte';
export const prerender = true; import { intlFormat } from 'date-fns';
type BlogPost = { export let data: PageData;
filename: string; $: ({
preview: string[];
title: string;
slug: string;
date: Date;
book_review?: boolean;
};
export async function load({ fetch }: LoadInput): Promise<LoadOutput> {
const posts = await fetch('/api/blog.json')
.then((res) => res.json())
.then((res) => res.posts);
const mostRecentPost = posts[0];
const daysSinceLastPublish = differenceInCalendarDays(
new Date(),
new Date(mostRecentPost.date)
);
const numberOfPosts = posts.length;
const firstPost = posts[numberOfPosts - 1]
const daysSinceFirstPost = differenceInCalendarDays(new Date(), new Date(firstPost.date))
const averageDaysBetweenPosts = Number(daysSinceFirstPost / numberOfPosts).toFixed(2)
const res: LoadOutput = {
status: 200,
props: {
posts, posts,
firstPost, firstPost,
averageDaysBetweenPosts,
daysSinceFirstPost,
daysSinceLastPublish,
numberOfPosts, numberOfPosts,
} daysSinceLastPublish,
}; daysSinceFirstPost,
averageDaysBetweenPosts
return res; } = data);
}
</script>
<script lang="ts">
import Navbar from '../components/Navbar.svelte';
export let posts: BlogPost[];
export let firstPost: BlogPost;
export let numberOfPosts: number;
export let daysSinceLastPublish: number;
export let daysSinceFirstPost: number;
export let averageDaysBetweenPosts: number;
</script> </script>
<svelte:head> <svelte:head>
<!-- Primary Meta Tags --> <!-- Primary Meta Tags -->
<title>Blog | thomaswilson.xyz</title> <title>Blog | thomaswilson.xyz</title>
<meta name="title" content="Blog | thomaswilson.xyz"> <meta name="title" content="Blog | thomaswilson.xyz" />
<meta name="description" content="I write about software and how I should have built it, and sometimes other things."> <meta
name="description"
content="I write about software and how I should have built it, and sometimes other things."
/>
<!-- Open Graph / Facebook --> <!-- Open Graph / Facebook -->
<meta property="og:type" content="website"> <meta property="og:type" content="website" />
<meta property="og:url" content="https://www.thomaswilson.xyz/blog"> <meta property="og:url" content="https://www.thomaswilson.xyz/blog" />
<meta property="og:title" content="Blog | thomaswilson.xyz"> <meta property="og:title" content="Blog | thomaswilson.xyz" />
<meta property="og:description" content="I write about software and how I should have built it, and sometimes other things."> <meta
property="og:description"
content="I write about software and how I should have built it, and sometimes other things."
/>
<!-- Twitter --> <!-- Twitter -->
<meta property="twitter:title" content="Blog | thomaswilson.xyz"> <meta property="twitter:title" content="Blog | thomaswilson.xyz" />
<meta property="twitter:description" content="I write about software and how I should have built it, and sometimes other things."> <meta
property="twitter:description"
content="I write about software and how I should have built it, and sometimes other things."
/>
</svelte:head> </svelte:head>
<Navbar /> <Navbar />
@ -81,13 +49,11 @@
things. things.
</p> </p>
<p> <p>
It's been <span It's been <span class="days-since" class:days-since-success={daysSinceLastPublish === 0}>
class="days-since"
class:days-since-success={daysSinceLastPublish === 0}
>
{daysSinceLastPublish} {daysSinceLastPublish}
</span> </span>
{daysSinceLastPublish === 1 ? 'day' : 'days'} since I last published something. On average I publish something every {averageDaysBetweenPosts} days ({numberOfPosts} posts in {daysSinceFirstPost} days). {daysSinceLastPublish === 1 ? 'day' : 'days'} since I last published something. On average I publish
something every {averageDaysBetweenPosts} days ({numberOfPosts} posts in {daysSinceFirstPost} days).
</p> </p>
</section> </section>

36
src/routes/blog/+page.ts Normal file
View file

@ -0,0 +1,36 @@
import type { LoadEvent, Load, Page } from '@sveltejs/kit';
import { differenceInCalendarDays, intlFormat } from 'date-fns';
export const prerender = true;
type BlogPost = {
filename: string;
preview: string[];
title: string;
slug: string;
date: Date;
book_review?: boolean;
};
export async function load({ fetch }: LoadEvent) {
const posts = await fetch('/api/blog.json')
.then((res) => res.json())
.then((res) => res.posts);
const mostRecentPost = posts[0];
const daysSinceLastPublish = differenceInCalendarDays(new Date(), new Date(mostRecentPost.date));
const numberOfPosts = posts.length;
const firstPost = posts[numberOfPosts - 1];
const daysSinceFirstPost = differenceInCalendarDays(new Date(), new Date(firstPost.date));
const averageDaysBetweenPosts = Number(daysSinceFirstPost / numberOfPosts).toFixed(2);
return {
posts,
firstPost,
averageDaysBetweenPosts,
daysSinceFirstPost,
daysSinceLastPublish,
numberOfPosts
};
}

View file

@ -1,34 +1,11 @@
<script lang="ts" context="module">
import type { LoadInput, LoadOutput } from '@sveltejs/kit';
export async function load({ params, fetch }: LoadInput): Promise<LoadOutput> {
const { slug } = params;
const { post } = await fetch(`/api/blog/${slug}.json`)
.then((res) => res.json())
.catch((error) => {
console.error(error);
return { post: null };
});
const res: LoadOutput = {
status: post ? 200 : 404,
props: {
date: new Date(post.date),
post
}
};
return res;
}
</script>
<script lang="ts"> <script lang="ts">
import type { PageData } from './$types';
import type { Post } from '$lib/Post'; import type { Post } from '$lib/Post';
import { intlFormat } from 'date-fns'; import { intlFormat } from 'date-fns';
import Navbar from '../../components/Navbar.svelte'; import Navbar from '../../../components/Navbar.svelte';
export let date: Date; export let data: PageData;
export let post: Post; $: ({ date, post } = data);
</script> </script>
<svelte:head> <svelte:head>

View file

@ -0,0 +1,17 @@
import type { LoadEvent } from '@sveltejs/kit';
import type { Post } from '$lib/Post';
export async function load({ params, fetch }: LoadEvent): Promise<{ post: Post; date: Date }> {
const { slug } = params;
const { post } = await fetch(`/api/blog/${slug}.json`)
.then((res) => res.json())
.catch((error) => {
console.error(error);
return { post: null };
});
return {
post,
date: new Date(post.date)
};
}

View file

@ -1,6 +1,6 @@
<script lang="ts"> <script lang="ts">
import { onDestroy, onMount } from 'svelte'; import { onDestroy, onMount } from 'svelte';
import Employee from '../components/salary-calculator/employee.svelte'; import Employee from '../../components/salary-calculator/employee.svelte';
type Employee = { type Employee = {
id: string; id: string;