#Get dynamic content from sanity in a dynamic route

5 messages · Page 1 of 1 (latest)

half onyx
#

Have a [slug].astro file that is currently a 404:

---
import { useSanityClient } from 'astro-sanity';

export async function getStaticPaths() {
  const pages = await useSanityClient().fetch(`*[_type == "pages"]`);

  return pages.map(({ slug, title }) => {
    return {
      params: { slug: slug.current },
      props: { title },
    };
  });
}

const { slug  } = Astro.params;
const { title } = Astro.props;
---
<div>
  this is a slug page
  {title}
  {slug}
</div>

sanity schema is Pages, with a title and slug field.

/about returns a 404 even though pages has a slug of "about" with a title of "about".

I'm able to pull the Pages data on my home page fine using the same groq query.

worthy dune
#

you could debug a little bit, by adding console log and see which step does not yield the expected result. e.g. console.log(pages) before return.

half onyx
#

it's a 404 though so nothing returns in the log

half onyx
#

how would i go about pulling the slug dynamically here, i want to compare the url to a sanity pages field and render data that way ```---
import { useSanityClient, groq } from 'astro-sanity';

export function getStaticPaths() {

return [
{ params: { slug: 'about'} }
];

// const query = groq*[_type == "page"]{slug};
// const content = useSanityClient().fetch(query);
// return content;
}

const { slug } = Astro.params;

<h1>{slug}</h1>```

worthy dune