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

Preview with moderated content / node revisions not working

Open senguendk opened this issue 3 years ago • 29 comments

Our Drupal setup is using content moderation and we noticed that draft versions of node cannot be viewed the same as unpublished content. We also noticed that node revisions (used by content moderation) in general cannot be viewed with the nextjs preview feature. Is there any way to make it work?

senguendk avatar Jan 31 '22 06:01 senguendk

Sure. Node revisions are supported by default (except for nodes with paragraphs).

What happens when you visit the revision page? Do you see the working-copy or an error page?

shadcn avatar Jan 31 '22 06:01 shadcn

Thank you for the quick reply.

I have nothing I can show but I can try to explain. There is no error. The view page of the draft content just doesn’t show the iFrame preview with the nextjs render. It just shows the default drupal view.

So, yes, i see the working copy but not as a nextjs Preview.

senguendk avatar Jan 31 '22 06:01 senguendk

The iframe preview is there for the published node?

shadcn avatar Jan 31 '22 06:01 shadcn

Ah I found the issue. I'll push a fix. Hold on.

shadcn avatar Jan 31 '22 06:01 shadcn

I pushed a fix. Can you try the latest dev version and let us know?

composer require 'drupal/next:1.0.x-dev@dev'

Thank you

shadcn avatar Jan 31 '22 06:01 shadcn

(Didn't mean to close this)

shadcn avatar Jan 31 '22 06:01 shadcn

I just tried it and it did not change. Also, the initial draft does have a preview but it 404s. Once it published it works. When creating another draft version, the preview defaults to the drupal built-in view (see screenshot). cm-draft-without-nextjs-preview

senguendk avatar Jan 31 '22 08:01 senguendk

What's the path for latest version?

shadcn avatar Jan 31 '22 08:01 shadcn

node/6/latest

senguendk avatar Jan 31 '22 09:01 senguendk

Looking into it.

shadcn avatar Feb 01 '22 09:02 shadcn

OK I found the issue and pushed a fix for it. Can you try the latest dev and let me know?

composer require 'drupal/next:1.0.x-dev@dev'

shadcn avatar Feb 01 '22 12:02 shadcn

Thanks! It fixed part of it. Now it shows the nextjs preview but it renders the latest published version not the latest revision (draft).

senguendk avatar Feb 01 '22 15:02 senguendk

Hmm probably something with latest-version and working-copy. I tested this locally with a published revision and a draft latest revision. I had to go back to the docs to check this one.

These are the lines handling this:

https://github.com/chapter-three/next-drupal/blob/222295ac1f6577ed376dd87e183f9a3b65a59cb8/modules/next/src/Entity/NextSite.php#L151-L169

Can you swap line 157 and 167 and see if this fixes it?

Thanks for your help figuring this out.

shadcn avatar Feb 01 '22 15:02 shadcn

Swapped the lines, but it didn't change anything. Sorry! It still always shows the published version.

senguendk avatar Feb 02 '22 05:02 senguendk

hmm, can you temporarily disable preview mode for this entity type and check if you see the expected version?

shadcn avatar Feb 02 '22 08:02 shadcn

How do I disable Preview mode?

senguendk avatar Feb 02 '22 09:02 senguendk

I figured out a way to disable it. “View” and “Latest version” show the correct content.

Edit: When I create a new Page as draft that never had a published version and re-enable preview mode, the preview 404s.

senguendk avatar Feb 02 '22 10:02 senguendk

Here's what I have:

https://user-images.githubusercontent.com/124599/152150761-967aa473-2b3f-4d22-998a-62e8457d510b.mp4

shadcn avatar Feb 02 '22 12:02 shadcn

Yeah, looks perfect on your end. Is there anything special I have to do on the nextjs server? I am just using the drupal starter template.

senguendk avatar Feb 02 '22 15:02 senguendk

Nothing special on the Next.js site. Here's the file

https://github.com/chapter-three/next-drupal/blob/cd2ed5c86e23c8c5c364e6094a93033cb955edc0/examples/example-marketing/pages/%5B%5B...slug%5D%5D.tsx#L62-L127

On the Drupal side, I've enabled content_moderation and added Basic Page to the Editorial workflow.

shadcn avatar Feb 02 '22 17:02 shadcn

Tried pretty much everything, but I cannot get it to work with the basic starter kit/app. Thank you very much for your time and effort.

senguendk avatar Feb 03 '22 20:02 senguendk

We just made a last ditch effort and setup a new Drupal (patches applied) installation and Nextjs-app (your basic starter kit). Unfortunately, the outcome did not change. Revisions are not rendered. Only the latest default/published revision is being rendered.

Again, thank you very much for trying to help us. We would love to give this another shot in the future.

Please let me know if you need more information from our side, in case you want to look into this issue again at some point.

Edit: You can close this if your like.

senguendk avatar Feb 04 '22 16:02 senguendk

Sure. I'm going to see if I can reproduce the issue with the basic starter today.

shadcn avatar Feb 04 '22 16:02 shadcn

@senguendk Yep you were right. I updated the code in the basic starter to handle revision. Check it out.

export async function getStaticPaths(context): Promise<GetStaticPathsResult> {
  return {
    paths: await getPathsFromContext(["node--article", "node--page"], context),
    fallback: "blocking",
  }
}

export async function getStaticProps(
  context
): Promise<GetStaticPropsResult<NodePageProps>> {
  const path = await translatePathFromContext(context)

  if (!path) {
    return {
      notFound: true,
    }
  }

  // Check for redirect
  if (path.redirect?.length) {
    const [redirect] = path.redirect
    return {
      redirect: {
        destination: redirect.to,
        permanent: redirect.status === "301",
      },
    }
  }

  const type = path.jsonapi.resourceName

  let params = {}
  if (type === "node--article") {
    params = {
      include: "field_image,uid",
    }
  }

  const node = await getResourceFromContext<DrupalNode>(type, context, {
    params,
  })

  if (!node || (!context.preview && node?.status === false)) {
    return {
      notFound: true,
    }
  }

  return {
    props: {
      preview: context.preview || false,
      node,
    },
    revalidate: 900,
  }
}

And add import { translatePathFromContext } from "next-drupal"

Can you replace the code in your [...slug].tsx and let me know?

Thank you.

shadcn avatar Feb 04 '22 16:02 shadcn

Thank you for responding so fast. We replaced the code, cleared .next and drupal cache. It did not make a difference unfortunately. It is, as if the "resourceVersion" or "rel" parameters are being ignored. We think so, because if the initial version of a page is a draft (unpublished), it 404s.

senguendk avatar Feb 04 '22 17:02 senguendk

That's unfortunate. You reverted the change we made earlier to NextSite.php right?

I'm going to pull a fresh Drupal copy and see if I can reproduce this.

shadcn avatar Feb 04 '22 17:02 shadcn

Yes, we did. I also set up a clean drupal installation myself.

Let me know how it goes. Maybe we are still doing something wrong.

senguendk avatar Feb 04 '22 18:02 senguendk

@senguendk Have you been able to look into this again?

@j4-m has been able to successfully preview revisions with content moderation with the latest dev release.

See https://github.com/chapter-three/next-drupal/issues/95#issuecomment-1043161140

shadcn avatar Feb 22 '22 12:02 shadcn

@shadcn thanks for checking in again. Installed the latest dev release. Did not work unfortunately. Still the same issue.

senguendk avatar Feb 23 '22 15:02 senguendk