Tips for dealing with 403 errors when using the Sanity client in a Sveltekit project
18 replies
Last updated: Feb 17, 2023
R
Hi -- any tips on dealing w/ 403 errors using the sanity client in a Sveltekit project?
I have this
page.ts
with this
page.svelte
it does momentarily flash the content, then it throws an error. I can fetch dummy data no problem.
I have this
page.ts
import type { PageLoad } from './$types'; import sanityClient from '../../lib/sanity' // eslint-disable-next-line @typescript-eslint/no-unused-vars export const load = (async ({ params }) => { const data = await sanityClient.fetch(`*[_type=="project"]`); return { name: "Rob", projects: data }; }) satisfies PageLoad;
page.svelte
<script lang="ts"> import type { PageData } from './$types'; export let data: PageData; const { name, projects } = data; </script> <h1>{name}</h1> {#if projects} <pre> {JSON.stringify(projects, null, 2)} </pre> {:else} <p>Your data will show up here when you've configured everything correctly</p> {/if}
Feb 15, 2023, 9:47 PM
Is your dataset private by any chance? If so, you’ll need to perform an authenticated request by providing a token to your Sanity client’s config.
Feb 15, 2023, 9:52 PM
R
public, thx tho
Feb 15, 2023, 9:59 PM
R
right but why only when I'm fetching Sanity data?
Feb 16, 2023, 1:17 AM
Yes? If you haven’t added a CORS origin to Sanity it’ll throw an error when you try to fetch data.
Feb 16, 2023, 6:11 PM
R
Hey
user M
I'm on localhost, per docs I shouldn't be having this issueFeb 16, 2023, 7:42 PM
R
I found it
Feb 16, 2023, 7:48 PM
R
the docs should specify :3333 is the default, that's not clear
Feb 16, 2023, 7:49 PM
Yeah, that’s misleading. You’d have to look at your existing origins to know that.
Feb 16, 2023, 7:50 PM
R
how long should that take to start working bc I'm still getting same error?
Feb 16, 2023, 7:53 PM
R
No, 204 no content? Works in Vision & logs data server side load function, client side is bombing.
Feb 16, 2023, 7:58 PM
R
Any recommendations here to sort this out? I'll be moving onto another CMS if this isn't easily solvable
Feb 16, 2023, 8:23 PM
If it’s fetching on the server side then there’s something wrong with your frontend code. But it’s understandable if you’d rather move on.
Feb 16, 2023, 8:27 PM
R
user M
how do you figure if it went from a 403 to a 204?Feb 16, 2023, 8:48 PM
R
devTools, same way I figured it was 403...
Feb 16, 2023, 10:48 PM
R
user M
bump ^?Feb 17, 2023, 5:42 PM
Sanity– build remarkable experiences at scale
Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.