Skip to content

Commit 48222f2

Browse files
committed
used on still requires refreshes (TBD)
1 parent 17530fa commit 48222f2

File tree

4 files changed

+51
-17
lines changed

4 files changed

+51
-17
lines changed

apps/sanity/sanity.config.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ const apiVersion = process.env.SANITY_STUDIO_API_VERSION || "2025-09-30";
5656

5757
// Set SANITY_STUDIO_DISABLE_PRESENTATION=true if you get network errors to api.sanity.io (e.g. firewall/VPN)
5858
const presentationEnabled =
59-
process.env.SANITY_STUDIO_DISABLE_PRESENTATION !== "true";
59+
process.env.SANITY_STUDIO_DISABLE_PRESENTATION !== "true";
6060

6161
// Use local Astro dev server for presentation preview when running Studio locally
6262
const isLocal =
@@ -69,6 +69,8 @@ function resolveHref(type: string, slug?: string): string | undefined {
6969
switch (type) {
7070
case "post":
7171
return slug ? `/post/${slug}` : undefined;
72+
case "podcast":
73+
return slug ? `/podcast/${slug}` : undefined;
7274
case "page":
7375
return slug ? `/${slug}` : undefined;
7476
default:
@@ -222,6 +224,22 @@ function buildPlugins(previewUrl: string): PluginOptions[] {
222224
],
223225
}),
224226
}),
227+
podcast: defineLocations({
228+
select: {
229+
title: "title",
230+
slug: "slug.current",
231+
},
232+
resolve: (doc) => ({
233+
locations: [
234+
{
235+
title: doc?.title || "Untitled",
236+
// biome-ignore lint/style/noNonNullAssertion: resolveHref returns string for known types
237+
href: resolveHref("podcast", doc?.slug)!,
238+
},
239+
homeLocation,
240+
],
241+
}),
242+
}),
225243
},
226244
},
227245
previewUrl: {
@@ -237,10 +255,7 @@ function buildPlugins(previewUrl: string): PluginOptions[] {
237255
]
238256
: []),
239257
structureTool({ structure: podcastStructure() }),
240-
singletonPlugin([
241-
settings.name,
242-
engineConfig.name,
243-
]),
258+
singletonPlugin([settings.name, engineConfig.name]),
244259
assistWithPresets(),
245260
media(),
246261
codeInput(),

apps/web/src/middleware.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,5 +77,23 @@ export const onRequest = defineMiddleware(async (context, next) => {
7777
return context.redirect("/login");
7878
}
7979

80-
return next();
80+
const response = await next();
81+
82+
// Prevent caching when in Sanity draft/preview mode so iframe refresh always shows current content
83+
const cookieHeader = context.request.headers.get("Cookie") ?? "";
84+
if (cookieHeader.includes("__sanity_preview=")) {
85+
const headers = new Headers(response.headers);
86+
headers.set(
87+
"Cache-Control",
88+
"no-store, no-cache, must-revalidate, max-age=0, proxy-revalidate",
89+
);
90+
headers.set("Pragma", "no-cache");
91+
return new Response(response.body, {
92+
status: response.status,
93+
statusText: response.statusText,
94+
headers,
95+
});
96+
}
97+
98+
return response;
8199
});

apps/web/src/pages/api/draft-mode/enable.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ export const GET: APIRoute = async ({ url, cookies, request }) => {
7979

8080
if (isValid) {
8181
const isLocal = url.hostname === "localhost";
82-
const redirectPath = safeRedirectPath(redirectTo ?? "/", origin);
82+
// redirectTo comes from validatePreviewUrl (sanity-preview-pathname param); fallback to param directly
83+
const pathFromParam = url.searchParams.get("sanity-preview-pathname") ?? undefined;
84+
const redirectPath = safeRedirectPath(redirectTo ?? pathFromParam ?? "/", origin);
8385
const location = `${origin}${redirectPath}`;
8486

8587
cookies.set("__sanity_preview", "1", {

apps/web/src/scripts/visual-editing.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
/**
2-
* Sanity Visual Editing (Plain JS).
3-
* Loaded only when visual editing is enabled; integrates with History API for Presentation tool.
4-
* Listens for presentation/perspective postMessages so switching perspective in Studio reloads
5-
* the preview with the correct sanity-preview-perspective URL param (outer context URL change
6-
* is not reflected in the iframe, so we must handle the message).
2+
* Sanity Visual Editing (Plain JS) — minimal integration per official docs.
3+
* https://reference.sanity.io/_sanity/visual-editing/
4+
*
5+
* History update uses pushState/replaceState only so the Studio can drive
6+
* iframe navigation (e.g. by loading the preview URL when you click locations).
7+
* We only handle presentation/perspective so perspective switches reload with the right param.
78
*/
89
import { enableVisualEditing } from "@sanity/visual-editing";
910

@@ -57,27 +58,25 @@ export function run(): () => void {
5758
switch (update.type) {
5859
case "push":
5960
window.history.pushState(null, "", update.url);
60-
// Reload so the new URL (e.g. perspective param) is loaded and server fetches correct data
61-
window.location.reload();
6261
break;
6362
case "pop":
6463
window.history.back();
6564
break;
6665
case "replace":
6766
window.history.replaceState(null, "", update.url);
68-
window.location.reload();
6967
break;
7068
default:
7169
throw new Error(`Unknown update type: ${(update as { type: string }).type}`);
7270
}
7371
},
7472
},
7573
zIndex: 1000,
74+
// Let default behavior apply: manual = reload, mutation = no default for Plain JS
7675
refresh: async (payload) => {
77-
// Reload preview when user clicks Refresh or when a document is saved in Studio
78-
if (payload.source === "manual" || payload.source === "mutation") {
76+
if (payload.source === "manual") {
7977
window.location.reload();
8078
}
79+
// source === 'mutation': no default for Plain JS per docs; don't reload unless you want to
8180
},
8281
});
8382

0 commit comments

Comments
 (0)