Skip to content

Commit

Permalink
feature!: Add support for third-party templating languages (#62)
Browse files Browse the repository at this point in the history
* feat!: remove hook and refactor to virtual module

BREAKING CHANGE: This deprecates the `useSanityClient` hook and exposes `sanityClient` on the 'sanity:client' module

* chore: put studio on virtual module (`sanity:studio`)

* chore: update examples for new imports

* fix(readme): update documentation for new module names

* chore: tidy up
  • Loading branch information
kmelve authored Sep 29, 2023
1 parent d485cca commit 0e213cb
Show file tree
Hide file tree
Showing 20 changed files with 156 additions and 114 deletions.
7 changes: 3 additions & 4 deletions apps/example-ssr/src/components/InternalLink.astro
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
---
import { useSanityClient } from "@sanity/astro";
import { sanityClient } from "sanity:client";
const { node } = Astro.props;
const { markDef } = node;
const client = useSanityClient();
// Only deal with posts links for this example
const destination = markDef._ref && await client.fetch(
const destination = markDef._ref && await sanityClient.fetch(
`* [_type == "post" && _id == $id] {slug {current}}[0]`,
{ id: markDef._ref }
);
const linkText = node.children.map(c => c.text).join(' ')
const linkText = node.children.map((c: any) => c.text).join(' ')
---

{
Expand Down
29 changes: 29 additions & 0 deletions apps/example-ssr/src/components/NewsBar.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* A dynamic Newsbar that fetches content live from Sanity.
*
*/
import { useState, useEffect, useCallback } from "react";
import { sanityClient } from "sanity:client";

export function NewsBar() {
const [news, setNews] = useState({ message: "Loading news…" });
const client = sanityClient;
const getNews = useCallback(async () => {
const response = await client.fetch(
`*[_type == "sanityIoSettings"][0].banner`
);
setNews(response || { message: "no news" });
}, [client]);

useEffect(() => {
getNews();
}, [getNews]);

return (
<marquee style={{ background: "blue", padding: "1em" }}>
<a style={{ color: "white" }} href={`https://www.sanity.io/${news.link}`}>
{news.message}
</a>
</marquee>
);
}
9 changes: 4 additions & 5 deletions apps/example-ssr/src/components/SanityImage.astro
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
---
import imageUrlBuilder from "@sanity/image-url";
import {useSanityClient} from "@sanity/astro"
import {sanityClient} from "sanity:client"
const client = useSanityClient()
const builder = imageUrlBuilder(client)
const builder = imageUrlBuilder(sanityClient)
const {node} = Astro.props
const { width = 960 } = Astro.props
// See https://www.sanity.io/docs/presenting-images for general documentation on
// presenting images, and https://www.sanity.io/docs/image-url for specifics on
// this builder API
const image = builder
const image = node && builder
.image(node)
.width(width)
.fit('max')
.auto('format')
---

<img src={image.url()} alt={node.alt || ""} title={node.alt} />
{image && <img src={image.url()} alt={node.alt || ""} title={node.alt} />}
5 changes: 4 additions & 1 deletion apps/example-ssr/src/layouts/Layout.astro
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
---
---
import { NewsBar } from '../components/NewsBar';
interface Props {
title: string;
}
Expand All @@ -17,6 +19,7 @@ const { title } = Astro.props;
<title>{title}</title>
</head>
<body>
<NewsBar client:only="react" />
<slot />
</body>
</html>
Expand Down
24 changes: 8 additions & 16 deletions apps/example-ssr/src/pages/index.astro
Original file line number Diff line number Diff line change
@@ -1,27 +1,19 @@
---
import { useSanityClient } from "@sanity/astro";
import { sanityClient } from "sanity:client";
import Layout from "../layouts/Layout.astro";
const client = useSanityClient();
const posts = await client.fetch(`*[_type == "post" && defined(publishedAt)] | order(publishedAt desc)`);
const posts = await sanityClient.fetch(`*[_type == "post" && defined(publishedAt)] | order(publishedAt desc)`);
---

<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
<meta name="viewport" content="width=device-width" />
<meta name="generator" content={Astro.generator} />
<title>Sanity.io blog</title>
</head>
<body>
<Layout title="Sanity.io blog">
<h1>Sanity.io blog</h1>
<ul>
{posts.map((post) => (
<li>,
{posts.map((post: any) => (
<li>
<a href={'/posts/' + post.slug.current} class="post-link">
{post.title}
</a>
</li>
))}
</body>
</html>
</ul>
</Layout>
21 changes: 10 additions & 11 deletions apps/example-ssr/src/pages/posts/[slug].astro
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
---
import Layout from "../../layouts/Layout.astro";
import { useSanityClient } from "@sanity/astro";
import { sanityClient } from "sanity:client";
import PortableText from "../../components/PortableText.astro";
import SanityImage from "../../components/SanityImage.astro";
const client = useSanityClient();
const post = await client.fetch(
`*[_type == "post" && defined(publishedAt) && slug.current == $slug] | order(publishedAt desc) {
...,
authors[]-> {
name
}
}[0]`, {
slug: Astro.params.slug,
const post = await sanityClient.fetch(
`*[_type == "post" && defined(publishedAt) && slug.current == $slug] | order(publishedAt desc) {
...,
authors[]-> {
name
}
);
}[0]`, {
slug: Astro.params.slug,
}
);
const byline = (post.authors || []).map((a:any) => a.name).join(", ");
---
Expand Down
5 changes: 2 additions & 3 deletions apps/example/sanity.config.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { visionTool } from "@sanity/vision";
import { defineConfig } from "sanity";
import { deskTool } from "sanity/desk";
import { sanityClient } from "sanity:client";
import { schemaTypes } from "./schemas";

export const projectId =
import.meta.env.PUBLIC_SANITY_PROJECT_ID! || "3do82whm";
export const dataset = import.meta.env.PUBLIC_SANITY_DATASET! || "next";
const { projectId, dataset } = sanityClient.config();

export default defineConfig({
name: "project-name",
Expand Down
7 changes: 3 additions & 4 deletions apps/example/src/components/InternalLink.astro
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
---
import { useSanityClient } from "@sanity/astro";
import { sanityClient } from "sanity:client";
const { node } = Astro.props;
const { markDef } = node;
const client = useSanityClient();
// Only deal with posts links for this example
const destination = markDef._ref && await client.fetch(
const destination = markDef._ref && await sanityClient.fetch(
`* [_type == "post" && _id == $id] {slug {current}}[0]`,
{ id: markDef._ref }
);
const linkText = node.children.map(c => c.text).join(' ')
const linkText = node.children.map((c: any) => c.text).join(' ')
---

{
Expand Down
29 changes: 29 additions & 0 deletions apps/example/src/components/NewsBar.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* A dynamic Newsbar that fetches content live from Sanity.
*
*/
import { useState, useEffect, useCallback } from "react";
import { sanityClient } from "sanity:client";

export function NewsBar() {
const [news, setNews] = useState({ message: "Loading news…" });
const client = sanityClient;
const getNews = useCallback(async () => {
const response = await client.fetch(
`*[_type == "sanityIoSettings"][0].banner`
);
setNews(response || { message: "no news" });
}, [client]);

useEffect(() => {
getNews();
}, [getNews]);

return (
<marquee style={{ background: "blue", padding: "1em" }}>
<a style={{ color: "white" }} href={`https://www.sanity.io/${news.link}`}>
{news.message}
</a>
</marquee>
);
}
10 changes: 5 additions & 5 deletions apps/example/src/components/SanityImage.astro
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
---
import imageUrlBuilder from "@sanity/image-url";
import {useSanityClient} from "@sanity/astro"
import { sanityClient } from "sanity:client";
const client = useSanityClient()
const builder = imageUrlBuilder(client)
const builder = imageUrlBuilder(sanityClient)
const {node} = Astro.props
const { width = 960 } = Astro.props
// See https://www.sanity.io/docs/presenting-images for general documentation on
// presenting images, and https://www.sanity.io/docs/image-url for specifics on
// this builder API
const image = builder
const image = node && builder
.image(node)
.width(width)
.fit('max')
.auto('format')
---

<img src={image.url()} alt={node.alt || ""} title={node.alt} />
{image && <img src={image.url()} alt={node.alt || ""} title={node.alt} />}
5 changes: 4 additions & 1 deletion apps/example/src/layouts/Layout.astro
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
---
---
import { NewsBar } from '../components/NewsBar';
interface Props {
title: string;
}
Expand All @@ -17,6 +19,7 @@ const { title } = Astro.props;
<title>{title}</title>
</head>
<body>
<NewsBar client:only="react" />
<slot />
</body>
</html>
Expand Down
22 changes: 7 additions & 15 deletions apps/example/src/pages/index.astro
Original file line number Diff line number Diff line change
@@ -1,27 +1,19 @@
---
import { useSanityClient } from "@sanity/astro";
import {sanityClient} from "sanity:client";
import Layout from "../layouts/Layout.astro";
const client = useSanityClient();
const posts = await client.fetch(`*[_type == "post" && defined(publishedAt)] | order(publishedAt desc)`);
const posts = await sanityClient.fetch(`*[_type == "post" && defined(publishedAt)] | order(publishedAt desc)`);
---

<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
<meta name="viewport" content="width=device-width" />
<meta name="generator" content={Astro.generator} />
<title>Sanity.io blog</title>
</head>
<body>
<Layout title="Sanity.io blog">
<h1>Sanity.io blog</h1>
<ul>
{posts.map((post: any) => (
<li>,
<li>
<a href={'/posts/' + post.slug.current} class="post-link">
{post.title}
</a>
</li>
))}
</body>
</html>
</ul>
</Layout>
5 changes: 2 additions & 3 deletions apps/example/src/pages/posts/[slug].astro
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
---
import { sanityClient } from "sanity:client";
import Layout from "../../layouts/Layout.astro";
import { useSanityClient } from "@sanity/astro";
import PortableText from "../../components/PortableText.astro";
import SanityImage from "../../components/SanityImage.astro";
export async function getStaticPaths() {
const client = useSanityClient();
const posts = await client.fetch(
const posts = await sanityClient.fetch(
`*[_type == "post" && defined(publishedAt)] | order(publishedAt desc) {
...,
authors[]-> {
Expand Down
11 changes: 5 additions & 6 deletions packages/sanity-astro/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,14 @@ export default defineConfig({
});
```

This enables the use of `useSanityClient()` in your template files. For example:
This enables the use of `sanityClient` in your template files. For example:

```mdx
---
// /blog/index.astro
import { useSanityClient } from "@sanity/astro";
import { sanityClient } from "sanity:client";

const client = useSanityClient();
const posts = await client.fetch(`*[_type == "post" && defined(slug)] | order(publishedAt desc)`);
const posts = await sanityClient.fetch(`*[_type == "post" && defined(slug)] | order(publishedAt desc)`);
---

<h1>Blog</h1>
Expand Down Expand Up @@ -114,14 +113,14 @@ You can use this configuration file to install plugins, add a schema with docume

```javascript
// astro.config.mjs
import sanityIntegration from "@sanity/astro";
import sanity from "@sanity/astro";
import { defineConfig } from "astro/config";
import react from "@astrojs/react";

export default defineConfig({
output: "hybrid",
integrations: [
sanityIntegration({
sanity({
projectId: "3do82whm",
dataset: "next",
// Set useCdn to false if you're building statically.
Expand Down
Loading

1 comment on commit 0e213cb

@vercel
Copy link

@vercel vercel bot commented on 0e213cb Sep 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

sanity-astro-example-ssr – ./apps/example-ssr

sanity-astro-example-ssr.sanity.build
sanity-astro-example-ssr-git-main.sanity.build

Please sign in to comment.