Solid with Sanity CMS

Hi everyone Anyone using Solid with Sanity CMS?
25 Replies
b_e_n_t_e_n
b_e_n_t_e_n•3w ago
I'm not but I've used Solid with react extensivly and the principles are mostly the same. Use the sanity client to query your cms, you can do it inside of server actions like any other query. do you have any specific questions or concerns with it?
Daniel Sousa @TutoDS
Is more the PortableText, the package is for React I'm new in Solid, try to learning Already do a query (not sure if I do it the correct way) and it's working
const [movies] = createResource(getMovie)

// ...

<Suspense fallback={<div>Loading....</div>}>
<Show when={movies()}>
<ul class={'grid grid-cols-4 gap-6'}>
<For each={movies()}>
{(movie) => {
const imageUrl = urlFor(movie.poster)

return (
<li
class={
'shadow-md font-sans flex flex-col gap-1 items-start p-4 rounded-sm'
}
>
<Image
loading="lazy"
class="w-full rounded aspect-square object-cover"
src={imageUrl
.width(1000)
.dpr(2)
.fit('max')
.url()}
alt={movie.title}
loadingImage={imageUrl.size(120, 120).quality(70).blur(50).format('webp').url()}
srcset={`
${imageUrl.width(78).url()} 78w,
${imageUrl.width(120).url()} 120w,
${imageUrl.width(1000).fit('max').url()} 1000w,
`}
/>
<h2 class={'font-sans text-xl'}>{movie.title}</h2>
<p class={'font-sans text-sm'}>{movie.slug}</p>
</li>
)
}}
</For>
</ul>
</Show>
</Suspense>
const [movies] = createResource(getMovie)

// ...

<Suspense fallback={<div>Loading....</div>}>
<Show when={movies()}>
<ul class={'grid grid-cols-4 gap-6'}>
<For each={movies()}>
{(movie) => {
const imageUrl = urlFor(movie.poster)

return (
<li
class={
'shadow-md font-sans flex flex-col gap-1 items-start p-4 rounded-sm'
}
>
<Image
loading="lazy"
class="w-full rounded aspect-square object-cover"
src={imageUrl
.width(1000)
.dpr(2)
.fit('max')
.url()}
alt={movie.title}
loadingImage={imageUrl.size(120, 120).quality(70).blur(50).format('webp').url()}
srcset={`
${imageUrl.width(78).url()} 78w,
${imageUrl.width(120).url()} 120w,
${imageUrl.width(1000).fit('max').url()} 1000w,
`}
/>
<h2 class={'font-sans text-xl'}>{movie.title}</h2>
<p class={'font-sans text-sm'}>{movie.slug}</p>
</li>
)
}}
</For>
</ul>
</Show>
</Suspense>
b_e_n_t_e_n
b_e_n_t_e_n•3w ago
oh yeah
b_e_n_t_e_n
b_e_n_t_e_n•3w ago
GitHub
GitHub - portabletext/solid-portabletext: Render Portable Text with...
Render Portable Text with Solid. Contribute to portabletext/solid-portabletext development by creating an account on GitHub.
Daniel Sousa @TutoDS
Oh thanks
Daniel Sousa @TutoDS
One question that maybe is something that I'm doing wrong When I navigate between pages the request to Sanity fails, but if I refresh the page everything works fine
No description
Daniel Sousa @TutoDS
I'm doing the fetching wrong? And sorry for the questions :/
b_e_n_t_e_n
b_e_n_t_e_n•3w ago
why be sorry? this is the place to ask 🙂 can i see the code you use to fetch? not the actual fetcher but the solid code and maybe the fetching code as well
Daniel Sousa @TutoDS
get-movies.ts
import {client} from '~/cms'
import {getMoviesQuery} from '~/cms/queries/movie'
import {cache} from '@solidjs/router'

const getMovies = cache(async () => {
try {
const movies = await client.fetch(getMoviesQuery)

return movies
} catch (error) {
console.error(error)
return []
}
}, 'movies')

export {getMovies}
import {client} from '~/cms'
import {getMoviesQuery} from '~/cms/queries/movie'
import {cache} from '@solidjs/router'

const getMovies = cache(async () => {
try {
const movies = await client.fetch(getMoviesQuery)

return movies
} catch (error) {
console.error(error)
return []
}
}, 'movies')

export {getMovies}
get-movies.query.ts
const getMoviesQuery = `
*[_type == "movie"] {
title,
'slug': slug.current,
overview,
releaseDate,
'poster': {
...poster,
'asset': poster.asset->
}
}
`;
const getMoviesQuery = `
*[_type == "movie"] {
title,
'slug': slug.current,
overview,
releaseDate,
'poster': {
...poster,
'asset': poster.asset->
}
}
`;
routes/index.tsx
export default function Home() {
const movies = createAsync(() => getMovies())

return (
<main class="text-center mx-auto text-gray-700 p-4">
<Suspense fallback={<div>Loading....</div>}>
<Show when={movies()}>
<ul class={'grid grid-cols-4 gap-6'}>
<For each={movies()}>
{(movie) => {
const imageUrl = urlFor(movie.poster)

return (
<li
class={
'shadow-md font-sans flex flex-col gap-1 p-4 rounded-sm text-left'
}
>
<Image
loading="lazy"
class="w-full rounded aspect-square object-cover"
src={imageUrl
.width(1000)
.dpr(2)
.fit('max')
.url()}
alt={movie.title}
loadingImage={imageUrl.size(120, 120).quality(70).blur(50).format('webp').url()}
srcset={`
${imageUrl.width(78).url()} 78w,
${imageUrl.width(120).url()} 120w,
${imageUrl.width(1000).fit('max').url()} 1000w,
`}
/>
<h2 class={'font-sans text-left text-xl font-semibold'}>{movie.title}</h2>
<p class={'font-sans text-sm text-left'}>{movie.slug}</p>

<div class={'line-clamp-3'}>
<PortableText value={movie.overview} components={components} />
</div>
</li>
)
}}
</For>
</ul>
</Show>
</Suspense>
</main>
)
}
export default function Home() {
const movies = createAsync(() => getMovies())

return (
<main class="text-center mx-auto text-gray-700 p-4">
<Suspense fallback={<div>Loading....</div>}>
<Show when={movies()}>
<ul class={'grid grid-cols-4 gap-6'}>
<For each={movies()}>
{(movie) => {
const imageUrl = urlFor(movie.poster)

return (
<li
class={
'shadow-md font-sans flex flex-col gap-1 p-4 rounded-sm text-left'
}
>
<Image
loading="lazy"
class="w-full rounded aspect-square object-cover"
src={imageUrl
.width(1000)
.dpr(2)
.fit('max')
.url()}
alt={movie.title}
loadingImage={imageUrl.size(120, 120).quality(70).blur(50).format('webp').url()}
srcset={`
${imageUrl.width(78).url()} 78w,
${imageUrl.width(120).url()} 120w,
${imageUrl.width(1000).fit('max').url()} 1000w,
`}
/>
<h2 class={'font-sans text-left text-xl font-semibold'}>{movie.title}</h2>
<p class={'font-sans text-sm text-left'}>{movie.slug}</p>

<div class={'line-clamp-3'}>
<PortableText value={movie.overview} components={components} />
</div>
</li>
)
}}
</For>
</ul>
</Show>
</Suspense>
</main>
)
}
https://docs.solidjs.com/solid-start/building-your-application/data-loading I see an example from this docs And on the route/page, I added:
export const route = {
load: () => getMovies()
}
export const route = {
load: () => getMovies()
}
I'm doing something wrong?
b_e_n_t_e_n
b_e_n_t_e_n•3w ago
I'm assuming your fetching code is working on the server but not on the client. so when you navigate client side it the fetch fails try putting "use server" at the top of your getMovies() function inside of cache()
Daniel Sousa @TutoDS
Works Should I do something different? And other thing, is possible fetch the data while I hover the link? I'm probably being a very noob, but it's normal this happen:
/movies/[slug].tsx
export default function MovieDetails() {
const {slug} = useParams<{slug: string}>()

return <main>
<h1>{slug} Movie</h1>
</main>
}
export default function MovieDetails() {
const {slug} = useParams<{slug: string}>()

return <main>
<h1>{slug} Movie</h1>
</main>
}
Only have this and put <a href={/movies/${movie.slug}}>view details</a> And whe I click on the view details the URL is ok but the page appears blank, after a refresh the slug appears Is very strange
Daniel Sousa @TutoDS
Before refresh
No description
Daniel Sousa @TutoDS
After refresh
No description
Daniel Sousa @TutoDS
I did another test: - If I comment the fetch code and the for loop, and add a simpel button to /movies/abc works - If I access the route directly from the URL works Otherwise not
Daniel Sousa @TutoDS
Sometimes appear this error on console
No description
b_e_n_t_e_n
b_e_n_t_e_n•3w ago
this feels to me like a bug within solid start / vinxi well, the sanity client not working on the server your params aren't updating because you're destructuring which loses reactivity
Daniel Sousa @TutoDS
Ok, so I should use const params = useParams
b_e_n_t_e_n
b_e_n_t_e_n•3w ago
yeah, because it's reactive
Daniel Sousa @TutoDS
Ok, thank you and sorry for the noob mistakes 🫣 For the sanity client, should I do something different?
Daniel Sousa @TutoDS
https://github.com/tutods/sanity-solid this is my code if you want to check
GitHub
GitHub - tutods/sanity-solid
Contribute to tutods/sanity-solid development by creating an account on GitHub.
b_e_n_t_e_n
b_e_n_t_e_n•3w ago
Looks fine to me. Anything specific you want me to look at?
Daniel Sousa @TutoDS
For now seems to be working I will try to keep doing more queries with Sanity to see if works Thanks for your help @b_e_n_t_e_n
b_e_n_t_e_n
b_e_n_t_e_n•3w ago
🔥
Daniel Sousa @TutoDS
And sorry for the noob questions
b_e_n_t_e_n
b_e_n_t_e_n•3w ago
no need to apologize
Want results from more Discord servers?
Add your server
More Posts
Optimistic UI with useSubmissions/useSubmission not workingI'm trying to use useSubmission to show optimistic UI but I can't get it to work, the pending state What Replaced createRouteData?In older version of SolidStart you could get all routes in a folder. This was useful for doing thingHow to make Server Sent Event (SSE)I need to make a SSE, so that all client have the data updated when it is modified. Howewer, since IHow do I recursively wrap a `JSX.Element` with an `Array<FlowComponent>````tsx // Let's say we have these values: const children = props.children const layouts = [Layout1, Broken vercel deployment with solidstart 1.0.0 and pnpm monorepo - clientOnly does not loadHi, I'm trying to deploy a solidstart app from a pnpm monorepo to vercel. `import { clientOnly } frTransitionGroup from props derived from store duplicates itemsI'm very new to solidjs (this is my first project) and I'm trying to migrate a simple react shoppingVsCode: Cannot find name reactDoes anyone know how to fix this annoying warning, this is not happening at all on intellij.Solid, CDK, Basic S3+CF DistroHi there, I've been working on a solid SPA that does not use solid-start. Are there any docs or helpcreateStore doesn't signal with a class as the store, but everything else works.If I creatgeStore using a class: letss say a simple class like this one: ` class TryMe { 'tryswi404 in deployment with VercelI am trying to implement in Vercel and I can't, I configured defineConfig but nothing, here I leave