1.1.7 • Published 1 year ago

astro-sanity v1.1.7

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

Astro + Sanity Integration

This is a helper package for integrating Sanity with Astro. It is not officially from Sanity but it is architected in the same way their official packages for frameworks like NextJS are.

Installation

npx astro add astro-sanity

Follow the prompts and once it's finished you should have something like the following in your astro.config.mjs file:

import sanity from 'astro-sanity'

...

export default defineConfig({
  integrations: [sanity()],
});

Update the sanity config to match your Sanity Client Config like so:

export default defineConfig({
  integrations: [
    sanity({
    projectId: 'YOUR_PROJECT_ID',
    dataset: 'production',
    apiVersion: '2021-03-25',
    useCdn: true,
  })],
});

Usage

Please see the /demo for a full example. However, this package was designed to give you the building blocks to integrate with Sanity but the flexibility to implement it how you want with the helper function names and behavior you want.

Using the Client

You can globally use the Sanity Client from the config with the following import

import { useSanityClient } from 'astro-sanity';

Querying Sanity with your Client

Here is an example using the client to query Sanity:

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

export async function getFirstBlogPost() {
  const query = groq`*[_type == "post"]`;
  const firstPost = await useSanityClient().fetch(query);
  return firstPost;
}

Create Your own Image Builder Helper

You can learn more about Sanity's image builder here. Here is an example of how you can create your own helper function to use in your components:

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

export const imageBuilder = createImageBuilder(useSanityClient());

export function urlForImage(source) {
  return imageBuilder.image(source);
}

Create a Custom portableTextToHtml Serializer

You can learn more about the @portabletext/to-html package here

import { portableTextToHtml } from 'astro-sanity';
import { urlForImage } from './urlForImage';

const customComponents = {
  /* your custom components here */
};

export function sanityPortableText(portabletext) {
  return portableTextToHtml(portabletext, customComponents);
}

Create a PortableText Astro Component

---
import { sanityPortableText } from '../sanity/portableText'

const {portableText} = Astro.props;
---

<Fragment set:html={sanityPortableText(portableText)} />

This can then be used in your Astro files and convert portable text to HTML automatically.

<PortableText portableText={myPortableText}/>

Support

Please feel free to reach out to us on our Discord if you have questions or file an issue on the repo.

Join Little Sticks Discord

1.1.7

1 year ago

1.1.6

1 year ago

1.1.5

1 year ago

1.1.4

1 year ago

1.1.3

1 year ago

1.1.1

1 year ago

1.1.0

1 year ago

1.1.2

1 year ago

1.0.0

2 years ago

0.0.30

2 years ago

0.0.27

2 years ago

0.0.28

2 years ago

0.0.29

2 years ago

0.0.26

2 years ago

0.0.25

2 years ago

0.0.24

2 years ago

0.0.23

2 years ago

0.0.22

2 years ago

0.0.21

2 years ago

0.0.20

2 years ago

0.0.17

2 years ago

0.0.16

2 years ago

0.0.15

2 years ago

0.0.14

2 years ago

0.0.13

2 years ago

0.0.12

2 years ago

0.0.11

2 years ago

0.0.10

2 years ago

0.0.9

2 years ago

0.0.8

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago