Skip to content

Commit 74e83a5

Browse files
authored
feat: Sanity Visual Editing — stega, loadQuery, VisualEditing component (#676)
Adds Sanity Visual Editing support for Astro 6:\n\n- loadQuery() helper: draft perspective + stega encoding when enabled, published content when disabled\n- VisualEditing component in BaseLayout (client:only=\"react\", zero server overhead)\n- All 12 content pages updated to use loadQuery for stega support\n- Count queries and RSS/sitemap stay on sanityFetch (stega would corrupt output)\n- Fail-closed: throws if Visual Editing enabled without SANITY_API_READ_TOKEN\n- CF Workers compatible: VisualEditing renders in browser only\n\nTo enable: set PUBLIC_SANITY_VISUAL_EDITING_ENABLED=true + SANITY_API_READ_TOKEN (viewer-level)"
1 parent 89545fc commit 74e83a5

File tree

16 files changed

+108
-30
lines changed

16 files changed

+108
-30
lines changed

apps/web/astro.config.mjs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ export default defineConfig({
2222
dataset: sanityDataset,
2323
useCdn: false,
2424
apiVersion: "2024-01-01",
25+
// Visual Editing: stega encodes edit markers in strings
26+
// Studio is standalone (apps/sanity), not embedded — no studioBasePath
27+
stega: {
28+
studioUrl: "https://codingcat.dev.sanity.studio",
29+
},
2530
}),
2631
react(),
2732
],

apps/web/src/env.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/// <reference path="../.astro/types.d.ts" />
2+
/// <reference types="@sanity/astro/module" />
23

34
declare namespace App {
45
interface Locals {

apps/web/src/layouts/BaseLayout.astro

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
---
22
import "../styles/global.css";
3+
import { VisualEditing } from "@sanity/astro/visual-editing";
34
45
interface Props {
56
title: string;
67
description?: string;
78
}
89
910
const { title, description = "CodingCat.dev — Purrfect Web Tutorials" } = Astro.props;
11+
12+
const visualEditingEnabled =
13+
import.meta.env.PUBLIC_SANITY_VISUAL_EDITING_ENABLED === "true";
1014
---
1115

1216
<!doctype html>
@@ -42,5 +46,7 @@ const { title, description = "CodingCat.dev — Purrfect Web Tutorials" } = Astr
4246
<p>&copy; {new Date().getFullYear()} CodingCat.dev. All rights reserved.</p>
4347
</div>
4448
</footer>
49+
50+
<VisualEditing enabled={visualEditingEnabled} />
4551
</body>
4652
</html>

apps/web/src/pages/[slug].astro

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
import BaseLayout from "@/layouts/BaseLayout.astro";
33
import { PortableText } from "astro-portabletext";
4-
import { sanityFetch, urlForImage } from "@/utils/sanity";
4+
import { loadQuery, urlForImage } from "@/utils/sanity";
55
import { pageQuery } from "@/lib/queries";
66
77
export const prerender = false;
@@ -14,7 +14,7 @@ if (reservedSlugs.includes(slug!)) {
1414
return Astro.redirect(`/${slug}`);
1515
}
1616
17-
const page = await sanityFetch<any>(pageQuery, { slug });
17+
const { data: page } = await loadQuery<any>({ query: pageQuery, params: { slug } });
1818
1919
if (!page) {
2020
return new Response(null, { status: 404 });

apps/web/src/pages/author/[slug].astro

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
---
22
import BaseLayout from "@/layouts/BaseLayout.astro";
33
import PersonDetail from "@/components/PersonDetail.astro";
4-
import { sanityFetch } from "@/utils/sanity";
4+
import { loadQuery } from "@/utils/sanity";
55
import { authorQuery } from "@/lib/queries";
66
77
export const prerender = false;
88
99
const { slug } = Astro.params;
10-
const author = await sanityFetch<any>(authorQuery, { slug });
10+
const { data: author } = await loadQuery<any>({ query: authorQuery, params: { slug } });
1111
1212
if (!author) {
1313
return Astro.redirect("/404");

apps/web/src/pages/authors.astro

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import BaseLayout from "@/layouts/BaseLayout.astro";
33
import ContentCard from "@/components/ContentCard.astro";
44
import Pagination from "@/components/Pagination.astro";
5-
import { sanityFetch } from "@/utils/sanity";
5+
import { loadQuery, sanityFetch } from "@/utils/sanity";
66
import { authorListQuery, authorCountQuery } from "@/lib/queries";
77
88
export const prerender = false;
@@ -12,10 +12,11 @@ const page = Number.isNaN(rawPage) || rawPage < 1 ? 1 : Math.floor(rawPage);
1212
const perPage = 12;
1313
const offset = (page - 1) * perPage;
1414
15-
const [items, totalCount] = await Promise.all([
16-
sanityFetch<any[]>(authorListQuery, { offset, end: offset + perPage }),
15+
const [itemsResult, totalCount] = await Promise.all([
16+
loadQuery<any[]>({ query: authorListQuery, params: { offset, end: offset + perPage } }),
1717
sanityFetch<number>(authorCountQuery),
1818
]);
19+
const items = itemsResult.data;
1920
2021
const totalPages = Math.ceil(totalCount / perPage);
2122

apps/web/src/pages/blog.astro

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import BaseLayout from "@/layouts/BaseLayout.astro";
33
import ContentCard from "@/components/ContentCard.astro";
44
import Pagination from "@/components/Pagination.astro";
5-
import { sanityFetch } from "@/utils/sanity";
5+
import { loadQuery, sanityFetch } from "@/utils/sanity";
66
import { postListQuery, postCountQuery } from "@/lib/queries";
77
88
export const prerender = false;
@@ -13,10 +13,11 @@ const page = Number.isNaN(rawPage) || rawPage < 1 ? 1 : Math.floor(rawPage);
1313
const perPage = 12;
1414
const offset = (page - 1) * perPage;
1515
16-
const [posts, totalCount] = await Promise.all([
17-
sanityFetch<any[]>(postListQuery, { offset, end: offset + perPage }),
16+
const [postsResult, totalCount] = await Promise.all([
17+
loadQuery<any[]>({ query: postListQuery, params: { offset, end: offset + perPage } }),
1818
sanityFetch<number>(postCountQuery),
1919
]);
20+
const posts = postsResult.data;
2021
2122
const totalPages = Math.ceil(totalCount / perPage);
2223

apps/web/src/pages/guest/[slug].astro

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
---
22
import BaseLayout from "@/layouts/BaseLayout.astro";
33
import PersonDetail from "@/components/PersonDetail.astro";
4-
import { sanityFetch } from "@/utils/sanity";
4+
import { loadQuery } from "@/utils/sanity";
55
import { guestQuery } from "@/lib/queries";
66
77
export const prerender = false;
88
99
const { slug } = Astro.params;
10-
const guest = await sanityFetch<any>(guestQuery, { slug });
10+
const { data: guest } = await loadQuery<any>({ query: guestQuery, params: { slug } });
1111
1212
if (!guest) {
1313
return Astro.redirect("/404");

apps/web/src/pages/guests.astro

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import BaseLayout from "@/layouts/BaseLayout.astro";
33
import ContentCard from "@/components/ContentCard.astro";
44
import Pagination from "@/components/Pagination.astro";
5-
import { sanityFetch } from "@/utils/sanity";
5+
import { loadQuery, sanityFetch } from "@/utils/sanity";
66
import { guestListQuery, guestCountQuery } from "@/lib/queries";
77
88
export const prerender = false;
@@ -12,10 +12,11 @@ const page = Number.isNaN(rawPage) || rawPage < 1 ? 1 : Math.floor(rawPage);
1212
const perPage = 12;
1313
const offset = (page - 1) * perPage;
1414
15-
const [items, totalCount] = await Promise.all([
16-
sanityFetch<any[]>(guestListQuery, { offset, end: offset + perPage }),
15+
const [itemsResult, totalCount] = await Promise.all([
16+
loadQuery<any[]>({ query: guestListQuery, params: { offset, end: offset + perPage } }),
1717
sanityFetch<number>(guestCountQuery),
1818
]);
19+
const items = itemsResult.data;
1920
2021
const totalPages = Math.ceil(totalCount / perPage);
2122

apps/web/src/pages/index.astro

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
---
22
import BaseLayout from "@/layouts/BaseLayout.astro";
33
import ContentCard from "@/components/ContentCard.astro";
4-
import { sanityFetch } from "@/utils/sanity";
4+
import { loadQuery } from "@/utils/sanity";
55
import { homePageQuery } from "@/lib/queries";
66
77
export const prerender = false;
88
9-
const homePage = await sanityFetch<any>(homePageQuery);
9+
const { data: homePage } = await loadQuery<any>({ query: homePageQuery });
1010
---
1111

1212
<BaseLayout title="CodingCat.dev — Purrfect Web Tutorials">

0 commit comments

Comments
 (0)