✨Discover storytelling in the AI age with Pixar's Matthew Luhn at Sanity Connect, May 8th—register now

Tips for dealing with 403 errors when using the Sanity client in a Sveltekit project

18 replies
Last updated: Feb 17, 2023
Hi -- any tips on dealing w/ 403 errors using the sanity client in a Sveltekit project?
I have this

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;
with this

<script lang="ts">
	import type { PageData } from './$types';

	export let data: PageData;
	const { name, projects } = data;

{#if projects}
		{JSON.stringify(projects, null, 2)}
	<p>Your data will show up here when you've configured everything correctly</p>
it does momentarily flash the content, then it throws an error. I can fetch dummy data no problem.
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
public, thx tho
Feb 15, 2023, 9:59 PM
You’ve got a CORS error in your screen shot. That’s likely the issue.
Feb 15, 2023, 10:27 PM
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
user M
I'm on localhost, per docs I shouldn't be having this issue
Feb 16, 2023, 7:42 PM
I found it
Feb 16, 2023, 7:48 PM
It’s configured to respond to localhost:3333.
Feb 16, 2023, 7:48 PM
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
how long should that take to start working bc I'm still getting same error?
Feb 16, 2023, 7:53 PM
I think pretty immediately. It’s still the same CORS error?
Feb 16, 2023, 7:55 PM
No, 204 no content? Works in Vision &amp; logs data server side load function, client side is bombing.
Feb 16, 2023, 7:58 PM
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
user M
how do you figure if it went from a 403 to a 204?
Feb 16, 2023, 8:48 PM
devTools, same way I figured it was 403...
Feb 16, 2023, 10:48 PM
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.

Was this answer helpful?