Skip to content

Commit

Permalink
feat: Add preview params
Browse files Browse the repository at this point in the history
  • Loading branch information
kiosion committed Aug 16, 2023
1 parent 3e7bec1 commit 4402c40
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 10 deletions.
18 changes: 12 additions & 6 deletions web/info/src/lib/sanity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,21 @@ import type { DocumentRegistry } from '$types';
const client = createClient({
projectId: SANITY_PROJECT_ID,
dataset: SANITY_DATASET,
useCdn: true,
useCdn: false,
apiVersion: SANITY_API_VERSION
});

export const getPageDocument = async <T extends string>(id: T) => {
const document = await client
.fetch<T extends keyof DocumentRegistry ? DocumentRegistry[T] : never>(
`*[_type == "page" && id == "${id}" && !(_id in path('drafts.**'))][0]`
)
export const getPageDocument = async <T extends string>(
id: T,
preview = false,
token?: string
) => {
const localClient = token ? createClient({ ...client.config(), token }) : client;
const query = `*[_type == "page" && id == "${id}"${
preview ? '' : ' && !(_id in path("drafts.**"))'
}][0]`;
const document = await localClient
.fetch<T extends keyof DocumentRegistry ? DocumentRegistry[T] : never>(query)
.then((response) => {
const document = response;
return document;
Expand Down
7 changes: 5 additions & 2 deletions web/info/src/routes/+page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ import { getPageDocument } from '$lib/sanity';

import type { PageLoad } from './$types';

export const load = (async ({ parent }) => {
export const load = (async ({ parent, url }) => {
const preview = url.searchParams.get('preview') === 'true',
token = url.searchParams.get('token') ?? undefined;

const { feeds, featuredFeeds } = await parent();

const content = await getPageDocument('home');
const content = await getPageDocument('home', preview, token);

return { content, feeds: { feeds, featured: featuredFeeds } };
}) satisfies PageLoad;
7 changes: 5 additions & 2 deletions web/info/src/routes/[page=valid_doc_route]/+page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import { getPageDocument } from '$lib/sanity';

import type { PageLoad } from './$types';

export const load = (async ({ parent, params }) => {
export const load = (async ({ parent, params, url }) => {
const preview = url.searchParams.get('preview') === 'true',
token = url.searchParams.get('token') ?? undefined;

// @ts-expect-error This is fine in this ctx
const pageName = DOC_ROUTES_NAMES[params.page] as keyof typeof DOC_ROUTES_NAMES;

Expand All @@ -15,7 +18,7 @@ export const load = (async ({ parent, params }) => {

const { feeds, featuredFeeds } = await parent();

const content = getPageDocument(pageName);
const content = getPageDocument(pageName, preview, token);

return { content, feeds: { feeds, featured: featuredFeeds } };
}) satisfies PageLoad;
34 changes: 34 additions & 0 deletions web/sanity/sanity.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,40 @@ export default defineConfig({
defaultApiVersion: 'v2022-11-29'
})
],
document: {
productionUrl: async (prev, context) => {
const { getClient, document } = context;

const client = getClient({
apiVersion: 'v2022-11-29'
});

if (document._type !== 'page') {
return prev;
}

const baseUrl = 'https://furryli.st';

const params = new URLSearchParams();
params.set('preview', 'true');
params.set('token', client.config()?.token || '');

const id = document._id.replace(/^drafts\./, '');

switch (id) {
case 'home':
return `${baseUrl}/?${params.toString()}`;
case 'welcome':
return `${baseUrl}/welcome?${params.toString()}`;
case 'feeds':
return `${baseUrl}/feeds?${params.toString()}`;
case 'communityGuidelines':
return `${baseUrl}/community-guidelines?${params.toString()}`;
default:
return prev;
}
}
},
schema: {
types: schemaTypes
},
Expand Down

0 comments on commit 4402c40

Please sign in to comment.