@sanity/next-loader
Advanced tools
Comparing version 1.2.0 to 1.2.1
# Changelog | ||
## [1.2.1](https://github.com/sanity-io/visual-editing/compare/next-loader-v1.2.0...next-loader-v1.2.1) (2024-12-03) | ||
### Bug Fixes | ||
* support upcoming client perspectives ([8e30e0c](https://github.com/sanity-io/visual-editing/commit/8e30e0c7eba7bd6b680737d83f7ef19653076892)) | ||
## [1.2.0](https://github.com/sanity-io/visual-editing/compare/next-loader-v1.1.12...next-loader-v1.2.0) (2024-12-02) | ||
@@ -4,0 +11,0 @@ |
@@ -0,9 +1,9 @@ | ||
import { validateApiPerspective } from "@sanity/client"; | ||
const perspectiveCookieName = "sanity-preview-perspective"; | ||
function sanitizePerspective(perspective, fallback) { | ||
switch (perspective) { | ||
case "previewDrafts": | ||
case "published": | ||
return perspective; | ||
default: | ||
return fallback; | ||
function sanitizePerspective(_perspective, fallback) { | ||
const perspective = typeof _perspective == "string" && _perspective.includes(",") ? _perspective.split(",") : _perspective; | ||
try { | ||
return validateApiPerspective(perspective), perspective === "raw" ? fallback : perspective; | ||
} catch (err) { | ||
return console.warn("Invalid perspective:", _perspective, perspective, err), fallback; | ||
} | ||
@@ -10,0 +10,0 @@ } |
@@ -19,3 +19,3 @@ import { | ||
params?: QueryParams | ||
perspective?: Omit<ClientPerspective, 'raw'> | ||
perspective?: Exclude<ClientPerspective, 'raw'> | ||
stega?: boolean | ||
@@ -22,0 +22,0 @@ initial: Promise<React_2.ReactNode> |
@@ -18,3 +18,3 @@ import { | ||
params?: QueryParams | Promise<QueryParams> | ||
perspective?: Omit<ClientPerspective, 'raw'> | ||
perspective?: Exclude<ClientPerspective, 'raw'> | ||
stega?: boolean | ||
@@ -69,3 +69,3 @@ tag?: string | ||
params?: QueryParams | Promise<QueryParams> | ||
perspective?: Omit<ClientPerspective, 'raw'> | ||
perspective?: Exclude<ClientPerspective, 'raw'> | ||
stega?: boolean | ||
@@ -72,0 +72,0 @@ tag?: string |
@@ -18,3 +18,3 @@ import { | ||
params?: QueryParams | Promise<QueryParams> | ||
perspective?: Omit<ClientPerspective, 'raw'> | ||
perspective?: Exclude<ClientPerspective, 'raw'> | ||
stega?: boolean | ||
@@ -69,3 +69,3 @@ tag?: string | ||
params?: QueryParams | Promise<QueryParams> | ||
perspective?: Omit<ClientPerspective, 'raw'> | ||
perspective?: Exclude<ClientPerspective, 'raw'> | ||
stega?: boolean | ||
@@ -72,0 +72,0 @@ tag?: string |
@@ -1,7 +0,7 @@ | ||
import type {SyncTag} from '@sanity/client' | ||
import type {ClientPerspective, SyncTag} from '@sanity/client' | ||
export declare function revalidateSyncTags(tags: SyncTag[]): Promise<void> | ||
export declare function setPerspectiveCookie(perspective: string): Promise<void> | ||
export declare function setPerspectiveCookie(perspective: ClientPerspective): Promise<void> | ||
export {} |
"use server"; | ||
import { perspectiveCookieName, sanitizePerspective } from "./_chunks-es/utils.js"; | ||
import { sanitizePerspective, perspectiveCookieName } from "./_chunks-es/utils.js"; | ||
import { revalidateTag } from "next/cache.js"; | ||
@@ -17,8 +17,12 @@ import { draftMode, cookies } from "next/headers.js"; | ||
throw new Error(`Invalid perspective: ${perspective}`); | ||
(await cookies()).set(perspectiveCookieName, perspective, { | ||
httpOnly: !0, | ||
path: "/", | ||
secure: !0, | ||
sameSite: "none" | ||
}); | ||
(await cookies()).set( | ||
perspectiveCookieName, | ||
Array.isArray(sanitizedPerspective) ? sanitizedPerspective.join(",") : sanitizedPerspective, | ||
{ | ||
httpOnly: !0, | ||
path: "/", | ||
secure: !0, | ||
sameSite: "none" | ||
} | ||
); | ||
} | ||
@@ -25,0 +29,0 @@ export { |
{ | ||
"name": "@sanity/next-loader", | ||
"version": "1.2.0", | ||
"version": "1.2.1", | ||
"homepage": "https://github.com/sanity-io/visual-editing/tree/main/packages/next-loader#readme", | ||
@@ -88,4 +88,4 @@ "bugs": { | ||
"devDependencies": { | ||
"@sanity/client": "^6.23.0", | ||
"@sanity/pkg-utils": "6.11.13", | ||
"@sanity/client": "^6.24.1", | ||
"@sanity/pkg-utils": "6.11.14", | ||
"@types/react": "^18.3.12", | ||
@@ -97,11 +97,11 @@ "eslint": "^8.57.1", | ||
"typescript": "5.6.3", | ||
"vitest": "^2.1.6", | ||
"vitest": "^2.1.8", | ||
"@repo/eslint-config": "0.0.0", | ||
"@repo/package.config": "0.0.0", | ||
"@repo/prettier-config": "0.0.0", | ||
"@repo/package.config": "0.0.0", | ||
"@sanity/preview-url-secret": "2.0.5", | ||
"@repo/visual-editing-helpers": "0.7.6" | ||
"@repo/visual-editing-helpers": "0.7.6", | ||
"@sanity/preview-url-secret": "2.0.5" | ||
}, | ||
"peerDependencies": { | ||
"@sanity/client": "^6.23.0", | ||
"@sanity/client": "^6.24.1", | ||
"next": "^14.1 || ^15.0.0-0", | ||
@@ -108,0 +108,0 @@ "react": "^18.3 || ^19.0.0-0" |
@@ -27,3 +27,3 @@ 'use server' | ||
export async function setPerspectiveCookie(perspective: string): Promise<void> { | ||
export async function setPerspectiveCookie(perspective: ClientPerspective): Promise<void> { | ||
if (!(await draftMode()).isEnabled) { | ||
@@ -38,8 +38,12 @@ // throw new Error('Draft mode is not enabled, setting perspective cookie is not allowed') | ||
;(await cookies()).set(perspectiveCookieName, perspective satisfies ClientPerspective, { | ||
httpOnly: true, | ||
path: '/', | ||
secure: true, | ||
sameSite: 'none', | ||
}) | ||
;(await cookies()).set( | ||
perspectiveCookieName, | ||
Array.isArray(sanitizedPerspective) ? sanitizedPerspective.join(',') : sanitizedPerspective, | ||
{ | ||
httpOnly: true, | ||
path: '/', | ||
secure: true, | ||
sameSite: 'none', | ||
}, | ||
) | ||
} | ||
@@ -46,0 +50,0 @@ |
@@ -1,17 +0,20 @@ | ||
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */ | ||
import {validateApiPerspective, type ClientPerspective} from '@sanity/client' | ||
import type {ClientPerspective} from '@sanity/client' | ||
/** @internal */ | ||
export function sanitizePerspective( | ||
perspective: string | undefined, | ||
_perspective: unknown, | ||
fallback: 'previewDrafts' | 'published', | ||
) { | ||
switch (perspective) { | ||
case 'previewDrafts': | ||
case 'published': | ||
return perspective satisfies ClientPerspective | ||
default: | ||
return fallback satisfies ClientPerspective | ||
): Exclude<ClientPerspective, 'raw'> { | ||
const perspective = | ||
typeof _perspective === 'string' && _perspective.includes(',') | ||
? _perspective.split(',') | ||
: _perspective | ||
try { | ||
validateApiPerspective(perspective) | ||
return perspective === 'raw' ? fallback : perspective | ||
} catch (err) { | ||
// eslint-disable-next-line no-console | ||
console.warn(`Invalid perspective:`, _perspective, perspective, err) | ||
return fallback | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
249784
2710