Skip to content

Instantly share code, notes, and snippets.

@eladroz
Created April 4, 2022 08:18
Show Gist options
  • Save eladroz/0f98a4e110c39ef78ec781e1081fcf14 to your computer and use it in GitHub Desktop.
Save eladroz/0f98a4e110c39ef78ec781e1081fcf14 to your computer and use it in GitHub Desktop.
Rendering a blog post from Contentful
<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