Skip to content

Commit 39560a4

Browse files
committed
server function
1 parent f4ed680 commit 39560a4

File tree

6 files changed

+57
-20
lines changed

6 files changed

+57
-20
lines changed
Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import { createMiddleware } from '@tanstack/start'
2-
import { getCookie } from 'vinxi/http'
1+
import { createMiddleware } from '@tanstack/start';
2+
import { getCookie } from 'vinxi/http';
33

4-
const previewMiddleware = createMiddleware().server(async ({ next }) => {
4+
export const previewMiddleware = createMiddleware().server(async ({ next }) => {
55
const isPreview = getCookie('__sanity_preview') === 'true';
6+
console.log({ isPreview });
67
return next({
78
context: {
89
previewMode: isPreview,
9-
}
10-
})
11-
})
10+
},
11+
});
12+
});

apps/docs/app/lib/sanity.server.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { createServerFn } from '@tanstack/start';
2+
import { previewMiddleware } from './previewMiddleware';
3+
import { sanityFetch as _sanityFetch, client } from './sanity';
4+
5+
export const sanityFetch = createServerFn({ method: 'GET' })
6+
.middleware([previewMiddleware])
7+
.validator((data: { query: any; params: any }) => data)
8+
.handler(async ({ data, context }) => {
9+
const { query, params } = data;
10+
11+
if (context.previewMode) {
12+
const previewClient = client.withConfig({
13+
perspective: 'previewDrafts',
14+
token: process.env.SANITY_VIEWER_TOKEN, // Needed for accessing previewDrafts perspective
15+
useCdn: false, // the previewDrafts perspective requires this to be `false
16+
});
17+
return _sanityFetch({ query, params, client: previewClient });
18+
}
19+
20+
return _sanityFetch({ query, params });
21+
});

apps/docs/app/lib/sanity.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@ export const client = createClient({
1111
export async function sanityFetch<const QueryString extends string>({
1212
query,
1313
params = {},
14+
client: _client = client,
1415
}: {
1516
query: QueryString;
1617
params?: QueryParams;
18+
client?: typeof client;
1719
}) {
1820
// Not sure what's happening here, but I need to set filterReponse to false to get the data as an array?
19-
const { result } = await client.fetch(query, params, {
21+
const { result } = await _client.fetch(query, params, {
2022
filterResponse: false,
2123
});
2224

apps/docs/app/routes/_docs/komponenter/$slug.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as badgeExamples from '@/examples/badge';
22
import * as buttonExamples from '@/examples/button';
3-
import { sanityFetch } from '@/lib/sanity';
3+
import { sanityFetch } from '@/lib/sanity.server';
4+
import { ComponentPreview } from '@/ui/component-preview';
45
import { Content } from '@/ui/content';
56
import { PropsTable } from '@/ui/props-table';
67
import { createFileRoute, notFound } from '@tanstack/react-router';
@@ -15,8 +16,10 @@ export const Route = createFileRoute('/_docs/komponenter/$slug')({
1516
component: Page,
1617
loader: async ({ params }) => {
1718
const res = await sanityFetch({
18-
query: COMPONENT_QUERY,
19-
params: { slug: params.slug },
19+
data: {
20+
query: COMPONENT_QUERY,
21+
params: { slug: params.slug },
22+
},
2023
});
2124

2225
if (res.data == null) {
Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
import { createAPIFileRoute } from '@tanstack/start/api';
2-
import { deleteCookie, sendRedirect } from 'vinxi/http'
2+
import { deleteCookie, sendRedirect } from 'vinxi/http';
33

4-
export const APIRoute = createAPIFileRoute('/api/studio/preview-mode/disable')({
4+
export const APIRoute = createAPIFileRoute('/api/preview-mode/disable')({
55
GET: () => {
6-
deleteCookie('__sanity_preview', { path: '/', secure: import.meta.env.PROD, httpOnly: true, sameSite: 'strict' });
6+
deleteCookie('__sanity_preview', {
7+
path: '/',
8+
secure: import.meta.env.PROD,
9+
httpOnly: true,
10+
sameSite: 'strict',
11+
});
712
sendRedirect('/');
813
},
914
});

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { createAPIFileRoute } from '@tanstack/start/api';
55
import { SanityClient } from 'sanity';
66
import { sendRedirect, setCookie } from 'vinxi/http';
77

8-
export const APIRoute = createAPIFileRoute('/api/studio/preview-mode/enable')({
8+
export const APIRoute = createAPIFileRoute('/api/preview-mode/enable')({
99
GET: async ({ request }) => {
1010
if (!process.env.SANITY_VIEWER_TOKEN) {
1111
throw new Response('Preview mode missing token', { status: 401 });
@@ -15,17 +15,22 @@ export const APIRoute = createAPIFileRoute('/api/studio/preview-mode/enable')({
1515
token: process.env.SANITY_VIEWER_TOKEN,
1616
});
1717

18-
const {
19-
isValid,
20-
redirectTo = '/',
21-
} = await validatePreviewUrl(clientWithToken, request.url);
18+
const { isValid, redirectTo = '/' } = await validatePreviewUrl(
19+
clientWithToken,
20+
request.url,
21+
);
2222

2323
if (!isValid) {
2424
throw new Response('Invalid secret', { status: 401 });
2525
}
2626

2727
// we can use sameSite: 'strict' because we're running an embedded studio
28-
setCookie('__sanity_preview', randomBytes(16).toString('hex'), { path: '/', secure: import.meta.env.PROD, httpOnly: true, sameSite: 'strict' });
29-
sendRedirect(redirectTo)
28+
setCookie('__sanity_preview', randomBytes(16).toString('hex'), {
29+
path: '/',
30+
secure: import.meta.env.PROD,
31+
httpOnly: true,
32+
sameSite: 'strict',
33+
});
34+
sendRedirect(redirectTo);
3035
},
3136
});

0 commit comments

Comments
 (0)