Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/add support for third party templating #64

Merged
merged 6 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
13 changes: 6 additions & 7 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 All @@ -73,7 +72,7 @@ const posts = await client.fetch(`*[_type == "post" && defined(slug)] | order(pu
</ul>
```

[Check out this guide][guide] for a more elaborate introduction to how to integrate content from Sanity into Astro.
[Check out this guide][guide] for a more elaborate introduction to how to integrate content from Sanity into Astro. You can also look in the `examples` folder in this repository for complete implementation examples.

### Embedding Sanity Studio on a route

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