Skip to content

Commit e901fd9

Browse files
committed
middleware
1 parent b78a709 commit e901fd9

File tree

5 files changed

+18
-4
lines changed

5 files changed

+18
-4
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { createMiddleware } from '@tanstack/start'
2+
import { getCookie } from 'vinxi/http'
3+
4+
const previewMiddleware = createMiddleware().server(async ({ next }) => {
5+
const isPreview = getCookie('__sanity_preview') === 'true';
6+
return next({
7+
context: {
8+
previewMode: isPreview,
9+
}
10+
})
11+
})

apps/docs/app/lib/sanity.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export const client = createClient({
55
dataset: 'grunnmuren',
66
apiVersion: '2024-09-18',
77
useCdn: true,
8+
perspective: 'published',
89
});
910

1011
export async function sanityFetch<const QueryString extends string>({

apps/docs/app/routes/api/studio/preview-mode/disable.ts renamed to apps/docs/app/routes/api/preview-mode/disable.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { deleteCookie, sendRedirect } from 'vinxi/http'
33

44
export const APIRoute = createAPIFileRoute('/api/studio/preview-mode/disable')({
55
GET: () => {
6-
deleteCookie('sanity_preview', { path: '/', secure: import.meta.env.PROD, httpOnly: true, sameSite: 'strict' });
6+
deleteCookie('__sanity_preview', { path: '/', secure: import.meta.env.PROD, httpOnly: true, sameSite: 'strict' });
77
sendRedirect('/');
88
},
99
});

apps/docs/app/routes/api/studio/preview-mode/enable.ts renamed to apps/docs/app/routes/api/preview-mode/enable.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import { randomBytes } from 'node:crypto';
12
import { client } from '@/lib/sanity';
23
import { validatePreviewUrl } from '@sanity/preview-url-secret';
34
import { createAPIFileRoute } from '@tanstack/start/api';
5+
import { SanityClient } from 'sanity';
46
import { sendRedirect, setCookie } from 'vinxi/http';
57

68
export const APIRoute = createAPIFileRoute('/api/studio/preview-mode/enable')({
@@ -23,7 +25,7 @@ export const APIRoute = createAPIFileRoute('/api/studio/preview-mode/enable')({
2325
}
2426

2527
// 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' });
28+
setCookie('__sanity_preview', randomBytes(16).toString('hex'), { path: '/', secure: import.meta.env.PROD, httpOnly: true, sameSite: 'strict' });
2729
sendRedirect(redirectTo)
2830
},
2931
});

apps/docs/sanity.config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ export default defineConfig({
2121
presentationTool({
2222
previewUrl: {
2323
previewMode: {
24-
enable: '/api/studio/preview-mode/enable',
25-
disable: '/api/studio/preview-mode/disable',
24+
enable: '/api/preview-mode/enable',
25+
disable: '/api/preview-mode/disable',
2626
},
2727
},
2828
resolve: {

0 commit comments

Comments
 (0)