Skip to content

Commit b78a709

Browse files
committed
docs: presentation mode
1 parent f5c89c6 commit b78a709

File tree

9 files changed

+183
-4
lines changed

9 files changed

+183
-4
lines changed

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ dist/
55
storybook-static/
66
next-env.d.ts
77

8+
packages/icons-react/icons.tsx
89

910
# editors
1011
.idea/
@@ -17,5 +18,4 @@ next-env.d.ts
1718

1819
# Secrets
1920
.FIGMA_TOKEN
20-
.vercel
21-
packages/icons-react/icons.tsx
21+
.env.local

apps/docs/.env.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Creata a viewer token at sanity.io/manage
2+
SANITY_VIEWER_TOKEN=

apps/docs/.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
.output
22
.vinxi
33
public/resources/icons/
4-
docgen.ts
4+
docgen.ts
5+
6+
.env
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { createAPIFileRoute } from '@tanstack/start/api';
2+
import { deleteCookie, sendRedirect } from 'vinxi/http'
3+
4+
export const APIRoute = createAPIFileRoute('/api/studio/preview-mode/disable')({
5+
GET: () => {
6+
deleteCookie('sanity_preview', { path: '/', secure: import.meta.env.PROD, httpOnly: true, sameSite: 'strict' });
7+
sendRedirect('/');
8+
},
9+
});
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { client } from '@/lib/sanity';
2+
import { validatePreviewUrl } from '@sanity/preview-url-secret';
3+
import { createAPIFileRoute } from '@tanstack/start/api';
4+
import { sendRedirect, setCookie } from 'vinxi/http';
5+
6+
export const APIRoute = createAPIFileRoute('/api/studio/preview-mode/enable')({
7+
GET: async ({ request }) => {
8+
if (!process.env.SANITY_VIEWER_TOKEN) {
9+
throw new Response('Preview mode missing token', { status: 401 });
10+
}
11+
12+
const clientWithToken = client.withConfig({
13+
token: process.env.SANITY_VIEWER_TOKEN,
14+
});
15+
16+
const {
17+
isValid,
18+
redirectTo = '/',
19+
} = await validatePreviewUrl(clientWithToken, request.url);
20+
21+
if (!isValid) {
22+
throw new Response('Invalid secret', { status: 401 });
23+
}
24+
25+
// we can use sameSite: 'strict' because we're running an embedded studio
26+
setCookie('__sanity_preview', 'test', { path: '/', secure: import.meta.env.PROD, httpOnly: true, sameSite: 'strict' });
27+
sendRedirect(redirectTo)
28+
},
29+
});

apps/docs/dktp/main.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ properties:
1616
containers:
1717
- image: dktprodacr.azurecr.io/grunnmuren/docs:${IMAGE_TAG}
1818
name: docs
19+
env:
20+
- name: SANITY_VIEWER_TOKEN
21+
secretRef: ${todo}
1922
resources:
2023
cpu: 0.25
2124
memory: 0.5Gi

apps/docs/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
"@portabletext/react": "3.2.0",
2424
"@sanity/client": "6.24.1",
2525
"@sanity/code-input": "5.1.2",
26+
"@sanity/presentation": "1.20.0",
27+
"@sanity/preview-url-secret": "2.0.5",
2628
"@sanity/vision": "3.68.3",
2729
"@tanstack/react-router": "1.95.1",
2830
"@tanstack/start": "1.95.1",

apps/docs/sanity.config.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { obosAuthStore } from '@code-obos/sanity-auth';
22
import { codeInput } from '@sanity/code-input';
33
import { visionTool } from '@sanity/vision';
44
import { defineConfig } from 'sanity';
5+
import { defineDocuments, presentationTool } from 'sanity/presentation';
56
import { structureTool } from 'sanity/structure';
67
import { schemaTypes } from './studio/schema-types';
78

@@ -13,7 +14,27 @@ export default defineConfig({
1314
basePath: '/studio',
1415
title: 'Grunnmuren - Sanity Studio',
1516
auth: obosAuthStore({ dataset }),
16-
plugins: [structureTool(), visionTool(), codeInput()],
17+
plugins: [
18+
structureTool(),
19+
visionTool(),
20+
codeInput(),
21+
presentationTool({
22+
previewUrl: {
23+
previewMode: {
24+
enable: '/api/studio/preview-mode/enable',
25+
disable: '/api/studio/preview-mode/disable',
26+
},
27+
},
28+
resolve: {
29+
mainDocuments: defineDocuments([
30+
{
31+
route: '/komponenter/:slug',
32+
filter: `_type == "component" && slug.current == $slug`,
33+
},
34+
]),
35+
},
36+
}),
37+
],
1738
schema: {
1839
types: schemaTypes,
1940
},

pnpm-lock.yaml

Lines changed: 111 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)