next-drupal icon indicating copy to clipboard operation
next-drupal copied to clipboard

context.preview returns undefined

Open Kerchik opened this issue 3 years ago • 4 comments
trafficstars

I tried to implement simmillar logic to this ticket https://github.com/chapter-three/next-drupal/issues/23 . But no matter what i do, context.preview is undefined.

I also have configured preview.ts file:

import { NextApiRequest, NextApiResponse } from 'next'

import { drupal } from 'lib/drupal'

export default async function handler(
  request: NextApiRequest,
  response: NextApiResponse
) {
  // @TODO FIX ANY ERROR
  const anyResponse = response as any
  
  return await drupal.preview(request, anyResponse)
}

Because of this, I cannot make preview work for unpublished content.

Kerchik avatar Sep 14 '22 09:09 Kerchik

@Kerchik Is this on local? It's probably the cookie not being set when in an iframe. Can you try the following where you create the DrupalClient?

export const drupal = new DrupalClient(
  process.env.NEXT_PUBLIC_DRUPAL_BASE_URL,
  {
    forceIframeSameSiteCookie: process.env.NODE_ENV === "development", // <-------
  }
)

From the docs:

If you're running your site in development and the host address is different from the iframe preview, you might run into issues with SameSite cookies.

You can read more about this here: https://next-drupal.org/docs/configuration#forceiframesamesitecookie

shadcn avatar Sep 14 '22 09:09 shadcn

@shadcn Yes it happens on local, but even when I add forceIframeSameSiteCookie preopert context.preview still returns undefined:

import { DrupalClient } from 'next-drupal'

export const drupal = new DrupalClient(
  process.env.NEXT_PUBLIC_DRUPAL_BASE_URL,
  {
    auth: {
      clientId: process.env.DRUPAL_CLIENT_ID,
      clientSecret: process.env.DRUPAL_CLIENT_SECRET,
    },
    previewSecret: process.env.DRUPAL_PREVIEW_SECRET,
    forceIframeSameSiteCookie: true,
  }
)

Kerchik avatar Sep 14 '22 09:09 Kerchik

Hmm, what happens if you copy the iframe URL and open it in a new tab? Does it enable preview?

shadcn avatar Sep 14 '22 11:09 shadcn

@shadcn No, context.preview is still undefined

Kerchik avatar Sep 14 '22 11:09 Kerchik

@Kerchik Did you figure this out?

shadcn avatar Nov 09 '22 07:11 shadcn