still writing groq queries.
This commit is contained in:
parent
eead28f05a
commit
c2c2ad29eb
@ -2,30 +2,24 @@
|
|||||||
* Example of how you could re-use GROQ queries across different contexts with Javascript.
|
* Example of how you could re-use GROQ queries across different contexts with Javascript.
|
||||||
* As your schema evolves, this pattern will be useful to keep your data in sync across all surfaces.
|
* As your schema evolves, this pattern will be useful to keep your data in sync across all surfaces.
|
||||||
*/
|
*/
|
||||||
export function getPostsQuery() {
|
export async function getPostsQuery() {
|
||||||
|
const data = await client.fetch(`*[_type == "post"] {
|
||||||
|
|
||||||
|
|
||||||
return `*[_type == "post"] {
|
|
||||||
_id,
|
|
||||||
_type,
|
|
||||||
_createdAt,
|
|
||||||
_updatedAt,
|
|
||||||
title,
|
title,
|
||||||
slug,
|
slug,
|
||||||
coverImage {
|
coverImage {
|
||||||
...,
|
...,
|
||||||
asset->
|
asset->
|
||||||
},
|
},
|
||||||
duration {
|
body,
|
||||||
start,
|
}`);
|
||||||
end
|
|
||||||
},
|
if (data) {
|
||||||
client,
|
return {
|
||||||
site,
|
posts: data
|
||||||
tags[],
|
};
|
||||||
body
|
}
|
||||||
}`
|
return {
|
||||||
|
status: 500,
|
||||||
|
body: new Error("Internal Server Error")
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,24 +1,4 @@
|
|||||||
import { client } from '$lib/sanity/client.js'
|
import { client } from '$lib/sanity/client.js'
|
||||||
// import { getPostsQuery } from '$lib/queries.js'
|
|
||||||
|
|
||||||
// // Fetch all valid posts & authors to display in the homepage
|
|
||||||
// export async function load() {
|
|
||||||
// const data = await client.fetch(/* groq */ `{
|
|
||||||
// "posts": ${getPostsQuery()},
|
|
||||||
// }`)
|
|
||||||
|
|
||||||
// if (data) {
|
|
||||||
// return {
|
|
||||||
// status: 200,
|
|
||||||
// body: data
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// return {
|
|
||||||
// status: 404
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
|
|
||||||
export async function load({ params }) {
|
export async function load({ params }) {
|
||||||
const data = await client.fetch(`*[_type == "post"] {
|
const data = await client.fetch(`*[_type == "post"] {
|
||||||
|
@ -1,14 +1,15 @@
|
|||||||
<script>
|
<script>
|
||||||
|
import { text } from "@sveltejs/kit";
|
||||||
export let data;
|
export let data;
|
||||||
|
function setElement(style, text) {
|
||||||
|
const targetElement =
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
{#each data.posts as post}
|
{#each data.posts as post}
|
||||||
<h2>{post.title}</h2>
|
<h2>{post.title}</h2>
|
||||||
{#each post.body as block}
|
{#each post.body as block, i}
|
||||||
{#if }
|
<p id="post-text-{i}">{setElement(block.style, block.children[0].text)}</p>
|
||||||
|
|
||||||
{/if}
|
|
||||||
<>{block}</p>
|
|
||||||
{/each}
|
{/each}
|
||||||
|
|
||||||
<img src="{post.coverImage.asset.url}" alt="yes">
|
<img src="{post.coverImage.asset.url}" alt="yes">
|
||||||
|
Loading…
Reference in New Issue
Block a user