Created
April 4, 2022 08:18
-
-
Save eladroz/0f98a4e110c39ef78ec781e1081fcf14 to your computer and use it in GitHub Desktop.
Rendering a blog post from Contentful
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script context="module"> | |
import { goto } from '$app/navigation'; | |
export async function load({ fetch, params }) { | |
const url = `/${params.slug}.json`; | |
const res = await fetch(url); | |
if (res.ok) { | |
const { blog } = await res.json(); | |
return { | |
props: { | |
blog, | |
gotoFn: goto, | |
}, | |
}; | |
} | |
return { | |
status: res.status, | |
error: new Error(`Could not load ${url}`), | |
}; | |
} | |
</script> | |
<script> | |
import RichContent from "$lib/components/RichContent.svelte"; | |
export let gotoFn; | |
export let blog; | |
</script> | |
<div class="container" data-sb-object-id={blog?.sys?.id}> | |
<button on:click={gotoFn('/')}>Back</button> | |
<h1 class="title" data-sb-field-path="title">{blog?.fields?.title ?? ""}</h1> | |
{#if blog?.fields?.content} | |
<RichContent fieldPath="content" richContent={blog.fields.content} /> | |
{/if} | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment