Skip to content

Commit 1b8c7df

Browse files
committed
fix(site): allow sanity.io in CSP frame-ancestors and resilient iframe visual editing
1 parent 4ea5ab1 commit 1b8c7df

4 files changed

Lines changed: 21 additions & 16 deletions

File tree

‎apps/site/src/components/DraftModeToggle.astro‎

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const returnTo = Astro.url.pathname + Astro.url.search;
1010
<form
1111
method="POST"
1212
action="/api/draft-mode/disable"
13+
data-sanity-preview
1314
class="fixed bottom-4 right-4 z-50"
1415
>
1516
<input type="hidden" name="returnTo" value={returnTo} />
@@ -20,17 +21,3 @@ const returnTo = Astro.url.pathname + Astro.url.search;
2021
Exit draft mode
2122
</button>
2223
</form>
23-
24-
<script>
25-
import { enableVisualEditing } from "@sanity/visual-editing-standalone";
26-
27-
enableVisualEditing({
28-
refresh: (payload) => {
29-
if (payload.source === "mutation" || payload.source === "manual") {
30-
window.location.reload();
31-
return Promise.resolve();
32-
}
33-
return false;
34-
},
35-
});
36-
</script>

‎apps/site/src/layouts/BaseLayout.astro‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,24 @@ const graph = buildGraph([
193193
<Footer />
194194
</div>
195195
{preview && <DraftModeToggle />}
196+
<script>
197+
import { enableVisualEditing } from "@sanity/visual-editing-standalone";
198+
199+
if (
200+
window.self !== window.top ||
201+
document.querySelector("[data-sanity-preview]")
202+
) {
203+
enableVisualEditing({
204+
refresh: (payload) => {
205+
if (payload.source === "mutation" || payload.source === "manual") {
206+
window.location.reload();
207+
return Promise.resolve();
208+
}
209+
return false;
210+
},
211+
});
212+
}
213+
</script>
196214
<ChatModal />
197215
<AnalyticsBeacon />
198216
</body>

‎apps/site/src/lib/sanity/preview.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export function resolvePreview({
4646
perspectiveParam,
4747
}: ResolvePreviewInput): PreviewContext {
4848
const siteWide = env?.PUBLIC_SANITY_VISUAL_EDITING_ENABLED === "true";
49-
if (!siteWide && !hasPreviewCookie) {
49+
if (!siteWide && !hasPreviewCookie && !perspectiveParam) {
5050
return { enabled: false };
5151
}
5252

‎apps/site/src/middleware.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ export const onRequest = defineMiddleware(async (context, next) => {
9393
response.headers.set("Referrer-Policy", "strict-origin-when-cross-origin");
9494
response.headers.set(
9595
"Content-Security-Policy",
96-
"frame-ancestors 'self' https://*.sanity.studio http://localhost:3333",
96+
"frame-ancestors 'self' https://*.sanity.studio https://*.sanity.io https://sanity.io http://localhost:3333",
9797
);
9898

9999
// AIPREF / AI Preference Headers

0 commit comments

Comments
 (0)