@gluhar/sanity
格卢哈尔卫生套餐. 在引擎盖下配置@nuxtjs/ saniity. 提供可堆肥等.
取决于 @gluhar/i18n 的字段本地化,所以您需要安装和配置 @gluhar/i18n 以便使用理智模块.
安装
用一个命令将模块安装到您的 Nuxt 应用程序 :
npx nuxi module add @gluhar/sanity
圣洁配置
在您的 / 配置文件夹中创建一个 sanity.config.ts 用于配置 gluhar 模块和 @nuxtjs/sanity 使用幕后画面.
更新 ID 指向您的工程和 apiVersion 到今天的日期 .
export default {
projectId: "8m33196t",
apiVersion: "2024-04-01",
dataset: "production",
};
软件包会自动加载此文件, 并进行其余的工作 。 如果您需要更多自定义配置, 您可以使用模块密钥 gluharSanity。 。 。 .
使用量
您可以从 @gluhar/sanity/types 例如 SanityDocument, (中文(简体) ), SanityImage, (中文(简体) ), SanityPage。 。 。 .
最常用的查询来自 @gluhar/sanity/query 喜欢 imageWithAsset
ISR、预览和视觉编辑isr(中文(简体) )
在Vercel上点取带有 " Sanity " 预览的ISR,从光学-com(生产-检查)和Cosopethrace上取出。 打开内容注册和两个查询:
// nuxt.config.ts
gluhar: {
sanity: {
isr: { noindex: ["/internal/**"] }, // or `isr: true`
},
},
// config/content.config.ts — shared with the Studio, plain data only
import { defineContentRegistry } from "@gluhar/routes";
export default defineContentRegistry({
types: [
{ type: "page", homeSlug: "/" },
{ type: "article", prefix: "news", prefixUid: "news" },
],
aggregators: ["/", "/news"],
globalTypes: ["header", "footer", "settings", "defaultSeo"],
appPaths: ["/login"],
});
// groq/index.ts
export const queryForType = (type: ContentDocumentType) => QUERY_BY_TYPE[type]; // $slug param
export const globalsQuery = groq`{ "header": *[_id == "header"][0]{ ... }, ... }`;
<!-- app.vue -->
<script setup lang="ts">
const { data: globals } = await useGlobals<Globals>();
</script>
<!-- pages/[...slug].vue -->
<script setup lang="ts">
const { page, status, upstreamFailed } = await useSinglePage<Page | Article>();
</script>
<template>
<NotFound v-if="!page && status !== 'pending'" :code="upstreamFailed ? 503 : 404" />
<Sections v-else-if="page?._type === 'page'" :sections="page.sections" />
<ArticleDetail v-else-if="page?._type === 'article'" :article="page" />
</template>
它的设置:
| 路由规则( 仅构建) | /** 简要记录 allowQuery: []· ; /api/**, (中文(简体) ), /preview/** 未涂上; /sitemap.xml 简要记录 没有先交的 |
GET /api/page?type&slug | 路由文件; null ^ 404, 读取失败 ^ 503 |
GET /api/globals | globalsQuery + 键 pageSlugsByUid一个读 |
POST /api/revalidate | 圣洁的网点( 下方) |
/api/__sitemap__/urls | 添加到 sitemap.sources 何时 @nuxtjs/sitemap 已安装 : 注册类型 × 有 hreflang 的地块 |
| 预览 | __prerender_bypass 打开 /preview/enable 之后 秘密的证据; /preview/disable?redirect= 限于此站点; 草稿仅与已验证的 sanity-preview-id 饼干 财务报告和审定财务报表 ?preview=true |
@nuxtjs/sanity | perspective: "published" (强制:"raw"+服务器令牌服务草稿),,, useCdn: false页:1 drafts 客户端有 stega 和一个字段过滤器, visualEditing.stega: false (强制),在 Studio 编辑时进行就地刷新 |
| Vercel 边缘 | bypassToken· ; noindex 在不可索引的建筑物上, isr.noindex 路径和 _payload.json (加上功能上,由@nuxtjs/robots取而代之);308用于后行鞭打 |
| 可编曲 | useSinglePage<T>(), (中文(简体) ), useGlobals<T>(), (中文(简体) ), contentRoutes (也在服务器上) |
| 服务器已使用 | getPage, (中文(简体) ), getGlobals, (中文(简体) ), fetchPublished, (中文(简体) ), fetchDrafts, (中文(简体) ), fetchContentPaths, (中文(简体) ), isPreviewRequest, (中文(简体) ), hasPreviewSession |
| 建立警卫 | VERCEL_BYPASS_TOKEN 现在和URL安全, NUXT_SANITY_STUDIO_URL 设定而非 *.sanity.studio 主机( Vercel 上的错误, 警告) ; 空的预发器, payloadExtraction 非虚假( 到处都是错误) |
信封 : NUXT_SANITY_TOKEN (读出信条;草稿), NUXT_SANITY_STUDIO_URL (工作室解决的URL, https://www.sanity.io/@<org>/studio/<appId>, (中文(简体) ), VERCEL_BYPASS_TOKEN (单位:千美元)openssl rand -hex 16时需要 构建, (中文(简体) ), REVALIDATE_SECRET。 。 。 .
选项 : registry (默认) config/content.config, (中文(简体) ), queries (单位:千美元)groq, (中文(简体) ), expiration, (中文(简体) ), noindex, (中文(简体) ), indexable (默认) site.indexable,则 VERCEL_ENV === "production", (中文(简体) ), stripTrailingSlash, (中文(简体) ), concurrency, (中文(简体) ), stega.logicFields 编号 : stega.nonTextFields (加进 uid key slug current oldSlug 编号 : alt orderRank; 任意字段 options.list 是一个逻辑领域),并且 handlers.{page,globals,revalidate,sitemap,preview}: false 保持一个项目自己的.
网瘾 (Sanity ~ API ~ Webhooks),创建/更新/删除,草稿删除,页眉 x-revalidate-secret: <REVALIDATE_SECRET>数字 :
filter: _type in [<registry types>, <globalTypes>]
projection: { _type, "slug": slug.current, "oldSlug": before().slug.current }
更改会清除文档的 URL( 以及它空出的一个), 集合器和站点映像, 在每个地方, 每个页面的 _payload.json。一个全局类型或弹道变化可以清除每条路线。 项目规则用 Nitro 插件 :
// server/plugins/revalidate.ts
export default defineNitroPlugin((nitroApp) => {
nitroApp.hooks.hook("gluhar:revalidate", (ctx) => {
if (ctx.doc._type === "promotion" && ctx.doc.accent) ctx.global = true; // add "accent" to the projection
if (ctx.doc._type === "tvChannel") ctx.paths.add("/tv");
});
});
这些都没有在 Dev 服务器( Plain SSR) 上活动, 也没有一个可以在那里验证的: 请检查 Vercel 部署(x-vercel-cache,来自演示文稿的预览,一个webhook快递).
useSanityQueryWithPreview (和 @gluhar/app是因为 useSanityPage)被贬值:它们在浏览器中获取并处理 ?preview 作为预览.
丰富文本
<RichText> 使 richtext 编号 : paragraphs 类型来源 @gluhar/studio。按原样通过本地化的字段(它称为 l()),或平面阵列:
<RichText
:value="article.body"
:classes="{
normal: 'mt-5 fl-text-lg leading-relaxed',
h2: 'mt-10 mb-4 fl-text-2xl font-extrabold',
highlight: 'bg-amber-100',
link: 'text-blue-700 underline',
}"
image-sizes="(min-width: 75rem) 50vw, 100vw"
/>
classes: 每个元素的功用类别。 键替换默认值,并且""将取去. 密钥 :normallargeh2h3h4blockquoteulollistrongemunderlinestrike-throughhighlightlinkfigureimgfigcaptionvideoembedtablethtd。 。 。 .components数字 :@portabletext/vue组件在默认情况下合并,按密钥计算.image-sizes页:1sizes图像属性( 默认)100vw) (中文(简体) ).
GROQ 需要没有 asset->: 图像和文件从 asset._ref。 。 。 .
捐款
地方发展
# Install dependencies
npm install
# Generate type stubs
npm run dev:prepare
# Develop with the playground
npm run dev
# Build the playground
npm run dev:build
# Run ESLint
npm run lint
# Run Vitest
npm run test
npm run test:watch
# Release new version
npm run release