-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/tinloof/medusa-b2c-starter
- Loading branch information
Showing
10 changed files
with
103 additions
and
182 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,7 @@ | ||
import config from "@/config"; | ||
import {client} from "@/data/sanity/client"; | ||
import {validatePreviewUrl} from "@sanity/preview-url-secret"; | ||
import {draftMode} from "next/headers"; | ||
import {redirect} from "next/navigation"; | ||
import {defineEnableDraftMode} from "next-sanity/draft-mode"; | ||
|
||
const clientWithToken = client.withConfig({token: config.sanity.token}); | ||
|
||
export async function GET(request: Request) { | ||
const {isValid, redirectTo = "/"} = await validatePreviewUrl( | ||
clientWithToken, | ||
request.url, | ||
); | ||
if (!isValid) { | ||
return new Response("Invalid secret", {status: 401}); | ||
} | ||
|
||
(await draftMode()).enable(); | ||
|
||
redirect(redirectTo); | ||
} | ||
export const {GET} = defineEnableDraftMode({ | ||
client: client.withConfig({token: config.sanity.token}), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import type {QueryParams} from "next-sanity"; | ||
|
||
import config from "@/config"; | ||
import {client} from "@/data/sanity/client"; | ||
import {draftMode} from "next/headers"; | ||
import "server-only"; | ||
|
||
export async function sanityFetch<QueryResponse>({ | ||
params = {}, | ||
query, | ||
}: { | ||
params?: Promise<QueryParams> | QueryParams; | ||
query: string; | ||
}): Promise<QueryResponse> { | ||
const isDraftMode = (await draftMode()).isEnabled; | ||
|
||
const perspective = isDraftMode ? "previewDrafts" : "published"; | ||
|
||
const token = config.sanity.token; | ||
|
||
const stega = | ||
perspective === "previewDrafts" || process.env.VERCEL_ENV === "preview"; | ||
|
||
if (perspective === "previewDrafts") { | ||
return client.fetch(query, await params, { | ||
// And we can't cache the responses as it would slow down the live preview experience | ||
next: {revalidate: 0}, | ||
perspective: "previewDrafts", | ||
stega, | ||
// The token is required to fetch draft content | ||
token, | ||
// The `previewDrafts` perspective isn't available on the API CDN | ||
useCdn: false, | ||
}); | ||
} | ||
|
||
return client.fetch(query, await params, { | ||
// When using the `published` perspective we use time-based revalidation to match the time-to-live on Sanity's API CDN (60 seconds) | ||
next: {revalidate: 120}, | ||
perspective: "published", | ||
stega, | ||
// Only enable Stega in production if it's a Vercel Preview Deployment, as the Vercel Toolbar supports Visual Editing | ||
// The `published` perspective is available on the API CDN | ||
useCdn: true, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.