Having trouble getting ghost cms api working in sveltekit with typescript eneabled #9538
-
Hi there. So I have a sveltekit app running my site. I have a ghost cms installed on a separate server and would like to get the content into my sveltekit. I am having trouble as the code I am using this code to test: <script context='module' lang="ts">
import { GhostContentAPI } from '@tryghost/content-api';
const GHOST_URL = 'http://blog.nafuna.tv';
const GHOST_KEY = '87f6096411ae42f96df2615620';
const GHOST_VERSION = 'v5';
export async function load() {
const api = GhostContentAPI({
url: GHOST_URL,
key: GHOST_KEY,
version: GHOST_VERSION})
const postsJson = await api.posts.browse({limit: 5, include: 'tags,authors'});
return {
props: {
postsJson: postsJson
}
}
}
</script>
<script lang="ts">
import type { PageData } from './$types';
export let postsJson;
console.log(postsJson.title);
export let data: PageData;
</script>
<div class="hero bg-base-200 p-5">
<div class="hero-content text-center">
<div class="max-w-md">
<h1 class="text-5xl font-bold">Coming Soon</h1>
<p class="py-6">We're working on this section and it will be live in the next few weeks!</p>
<a href="/agency" class="btn btn-primary">Back to Agency</a>
</div>
</div>
</div>
{#each postsJson as post}
<h1> {post.title} </h1>
<h3> {post.excerpt} </h3>
{/each} not sure what I am missing. A lot of the code I got from the ghost docs |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 13 replies
-
Hi Nafuna, The code example from the ghost docs is using an outdated convention of loading data from Svelte's beta days. You'll have to utilise a The solution should look something like the two file examples below: // src/routes/+page.server.ts
import { GhostContentAPI } from '@tryghost/content-api';
const GHOST_URL = 'http://blog.nafuna.tv';
const GHOST_KEY = '87f6096411ae42f96df2615620';
const GHOST_VERSION = 'v5';
export async function load() {
const api = GhostContentAPI({
url: GHOST_URL,
key: GHOST_KEY,
version: GHOST_VERSION})
const postsJson = await api.posts.browse({limit: 5, include: 'tags,authors'});
return {
postsJson
}
} <script lang="ts">
// src/routes/+page.svelte
import type { PageData } from './$types';
export let data: PageData;
</script>
{#each data.postsJson as post}
<h1> {post.title} </h1>
<h3> {post.excerpt} </h3>
{/each} Let me know if this helps! |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
Hi Nafuna,
The code example from the ghost docs is using an outdated convention of loading data from Svelte's beta days.
Declaring the load function in the svelte file is no longer supported.
You'll have to utilise a
+page.server.ts
file to load your data into a+page.svelte
page file as explained in the link below.https://kit.svelte.dev/docs/load
The solution should look something like the two file examples below: