mirror of
https://github.com/vercel/commerce.git
synced 2025-07-23 04:36:49 +00:00
Added preview functionality for home page
This commit is contained in:
@@ -41,8 +41,10 @@ export const SANITY_API_VERSION = '2022-10-25'
|
||||
// Set this to enable helper links in document status banners and shortcut links on products and collections.
|
||||
export const STORM_STORE_ID = ''
|
||||
|
||||
export const SANITY_STUDIO_API_READ_TOKEN = process.env.SANITY_STUDIO_API_READ_TOKEN;
|
||||
|
||||
// Project preview URLs
|
||||
export const localStorefrontUrl = 'http://localhost:3000';
|
||||
export const localStorefrontPreviewUrl = 'http://localhost:3000/api/draft';
|
||||
export const localStorefrontPreviewUrl = 'http://localhost:3000/api/preview';
|
||||
export const publicStorefrontUrl = 'https://km-storefront.vercel.app';
|
||||
export const publicStorefrontPreviewUrl = 'https://km-storefront.vercel.app/api/draft';
|
||||
export const publicStorefrontPreviewUrl = 'https://km-storefront.vercel.app/api/preview';
|
@@ -1,8 +1,8 @@
|
||||
import {ListItemBuilder} from 'sanity/desk'
|
||||
import defineStructure from '../utils/define-structure'
|
||||
import { EyeOpenIcon, MasterDetailIcon } from '@sanity/icons'
|
||||
import { SanityDocument } from 'sanity'
|
||||
import Iframe from 'sanity-plugin-iframe-pane'
|
||||
import {SanityDocument} from 'sanity'
|
||||
import {EyeOpenIcon, MasterDetailIcon} from '@sanity/icons'
|
||||
import { ListItemBuilder } from 'sanity/desk'
|
||||
import defineStructure from '../utils/define-structure'
|
||||
import getPreviewUrl from '../utils/get-preview-url'
|
||||
|
||||
export default defineStructure<ListItemBuilder>((S) =>
|
||||
|
@@ -1,15 +1,31 @@
|
||||
import { createClient } from "next-sanity";
|
||||
import { cache } from 'react';
|
||||
import type { SanityClient } from "@sanity/client";
|
||||
import { createClient } from "@sanity/client";
|
||||
import { cache } from "react";
|
||||
|
||||
export const projectId = process.env.NEXT_PUBLIC_SANITY_PROJECT_ID!;
|
||||
export const dataset = process.env.NEXT_PUBLIC_SANITY_DATASET!;
|
||||
const apiVersion = process.env.NEXT_PUBLIC_SANITY_API_VERSION!;
|
||||
export function getClient(preview?: {token?: string}): SanityClient {
|
||||
const client = createClient({
|
||||
projectId: process.env.NEXT_PUBLIC_SANITY_PROJECT_ID,
|
||||
dataset: process.env.NEXT_PUBLIC_SANITY_DATASET,
|
||||
apiVersion: process.env.NEXT_PUBLIC_SANITY_API_VERSION,
|
||||
useCdn: true,
|
||||
perspective: 'published',
|
||||
})
|
||||
if (preview) {
|
||||
if (!preview.token) {
|
||||
throw new Error('You must provide a token to preview drafts')
|
||||
}
|
||||
return client.withConfig({
|
||||
token: preview.token,
|
||||
useCdn: false,
|
||||
ignoreBrowserTokenWarning: true,
|
||||
perspective: 'previewDrafts',
|
||||
})
|
||||
}
|
||||
return client
|
||||
}
|
||||
|
||||
export const client = createClient({
|
||||
projectId,
|
||||
dataset,
|
||||
apiVersion,
|
||||
useCdn: true,
|
||||
});
|
||||
export const getCachedClient = (preview?: {token?: string}) => {
|
||||
const client = getClient(preview);
|
||||
|
||||
export const clientFetch = cache(client.fetch.bind(client));
|
||||
return cache(client.fetch.bind(client));
|
||||
};
|
@@ -1,7 +1,7 @@
|
||||
import createImageUrlBuilder from '@sanity/image-url'
|
||||
import { client } from './sanity.client'
|
||||
import { getClient } from './sanity.client'
|
||||
|
||||
export const imageBuilder = createImageUrlBuilder(client)
|
||||
export const imageBuilder = createImageUrlBuilder(getClient())
|
||||
|
||||
export const urlForImage = (source: any) =>
|
||||
imageBuilder.image(source).auto('format').fit('crop')
|
||||
|
@@ -1,20 +1,24 @@
|
||||
// @ts-nocheck
|
||||
import {isDev, SanityDocument} from 'sanity'
|
||||
import { isDev, SanityDocument } from 'sanity'
|
||||
import { localStorefrontPreviewUrl, publicStorefrontPreviewUrl } from '../constants'
|
||||
|
||||
const SANITY_STUDIO_API_READ_TOKEN = "skYG2HXNga8uxSL7rFIreJEnP0SdVjCZ2nzB8rUHD4wRWxXPGceXTuR5vCVBP99mWZ9ULhghmpUyX7EtzDmJusSk6Gwvdr3nLAsdWI9ZktIWvSWUNpHbu0Xfrrt0UUaktrLglk7ToABvjXlaPHLpOIR3dnjl4MGByutPmyra0b5t20kgDrmF"
|
||||
// Customise this function to show the correct URL based on the current document
|
||||
export default function getPreviewUrl(doc: SanityDocument) {
|
||||
export default async function getPreviewUrl(doc: SanityDocument) {
|
||||
|
||||
if (isDev) {
|
||||
// Home page have no slugs.
|
||||
if (!doc.slug) {
|
||||
return
|
||||
return `${localStorefrontPreviewUrl}?locale=${doc.language}&type=${doc._type}&secret=${SANITY_STUDIO_API_READ_TOKEN}`
|
||||
}
|
||||
|
||||
return `${localStorefrontPreviewUrl}?slug=${doc.slug.current}&locale=${doc.language}&secret=secret&type=${doc._type}`
|
||||
return `${localStorefrontPreviewUrl}?slug=${doc.slug.current}&locale=${doc.language}&type=${doc._type}&secret=${SANITY_STUDIO_API_READ_TOKEN}`
|
||||
} else {
|
||||
// Home page have no slugs.
|
||||
if (!doc.slug) {
|
||||
return
|
||||
return `${localStorefrontPreviewUrl}?locale=${doc.language}&type=${doc._type}&secret=${SANITY_STUDIO_API_READ_TOKEN}`
|
||||
}
|
||||
|
||||
return `${publicStorefrontPreviewUrl}?slug=${doc.slug.current}&locale=${doc.language}&secret=secret&type=${doc._type}`
|
||||
return `${publicStorefrontPreviewUrl}?slug=${doc.slug.current}&locale=${doc.language}&type=${doc._type}&secret=${SANITY_STUDIO_API_READ_TOKEN}`
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user