Skip to content

Commit 1d02c7e

Browse files
committed
fix: use redirect: "manual"
1 parent c9e6eea commit 1d02c7e

File tree

3 files changed

+20
-16
lines changed

3 files changed

+20
-16
lines changed

src/PrismicPreviewClient.tsx

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,17 @@ export function PrismicPreviewClient(props: PrismicPreviewClientProps): null {
4343
if (hasCookieForRepository && !isDraftMode) {
4444
console.log("starting preview link");
4545

46-
// We check for `res.redirected` rather than `res.ok`
47-
// since the update preview endpoint may redirect to a
48-
// 404 page. As long as it redirects, we know the
49-
// endpoint exists and at least attempted to set preview
50-
// data.
46+
// We check `opaqueredirect` because we don't care if
47+
// the redirect was successful or not. As long as it
48+
// redirects, we know the endpoint exists and draft mode
49+
// is active.
5150
globalThis
52-
.fetch(updatePreviewURL, { signal: controller.signal })
51+
.fetch(updatePreviewURL, {
52+
redirect: "manual",
53+
signal: controller.signal,
54+
})
5355
.then((res) => {
54-
if (!res.redirected) {
56+
if (res.type !== "opaqueredirect") {
5557
console.error(
5658
`[<PrismicPreview>] Failed to start the preview using "${updatePreviewURL}". Does it exist?`,
5759
);

src/pages/PrismicPreview.tsx

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,16 @@ export function PrismicPreview(props: PrismicPreviewProps): JSX.Element {
101101
}
102102

103103
function start() {
104-
// We check for `res.redirected` rather than `res.ok`
105-
// since the update preview endpoint may redirect to a
106-
// 404 page. As long as it redirects, we know the
107-
// endpoint exists and at least attempted to set preview
108-
// data.
109-
fetch(router.basePath + updatePreviewURL, { signal: controller.signal })
104+
// We check `opaqueredirect` because we don't care if
105+
// the redirect was successful or not. As long as it
106+
// redirects, we know the endpoint exists and at least
107+
// attempted to set preview data.
108+
fetch(router.basePath + updatePreviewURL, {
109+
redirect: "manual",
110+
signal: controller.signal,
111+
})
110112
.then((res) => {
111-
if (!res.redirected) {
113+
if (res.type !== "opaqueredirect") {
112114
console.error(
113115
`[<PrismicPreview>] Failed to start or update the preview using "${updatePreviewURL}". Does it exist?`,
114116
);

tests/previews.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ test("supports custom update endpoint", async ({ appPage, repo, pageDoc }) => {
8383
pageDoc,
8484
content({ payload: "foo" }),
8585
);
86-
await appPage.preview(updatedDocument);
86+
await repo.createPreviewSession(updatedDocument);
8787
await appPage.goToDocument(updatedDocument, "/with-custom-preview-endpoints");
8888
await expect(appPage.payload).toContainText("foo", { timeout: 30000 });
8989
await expect(appPage.toolbar).toHaveCount(1);
@@ -96,7 +96,7 @@ test("supports custom exit endpoint", async ({ appPage, repo, pageDoc }) => {
9696
pageDoc,
9797
content({ payload: "foo" }),
9898
);
99-
await appPage.preview(updatedDocument);
99+
await repo.createPreviewSession(updatedDocument);
100100
await appPage.goToDocument(updatedDocument, "/with-custom-preview-endpoints");
101101
await expect(appPage.payload).toContainText("foo", { timeout: 30000 });
102102
await expect(appPage.toolbar).toHaveCount(1);

0 commit comments

Comments
 (0)