Visual editor integration in Next.js App Router
Maintainers usually reply within 2 days
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 30/100
- Issue type
- Documentation
- Clarity
- Mostly clear
- Activity status
- Stale
- Tech stack
- next.js, react, typescript
- Domain
- documentation, web-dev
Research direction
Start by reading the existing Directus visual editor documentation and compare its integration approach with the Next.js App Router and React Server Components examples provided here. Confirm the maintainers want a framework-specific, RSC-compliant update; done means the relevant guide explains the approved approach clearly and is validated against the documented Next.js setup.
Written by the indexing model from the issue text.
Description
Hey Guys,
i recently integrated the directus visual editor in my Next.js App router project.
I have also done the same with other headless CMS, namely Contentful and Storyblok.
Generally, i understand that the goal is to make Guides/Tutorials on the directus docs framework agnostic. However, there is two perspectives from which this task could be implemented framework agnostic. The one being the way it is currently documented, the other being from Next.js perspective, where all visual editors and similar tools are implemented in the same way as well, ever since Next.js moved to App router.
The introduction of React Server Components (RSC) meant a shift in approach for lots of stuff and it is obvious that all services and tools can't change their docs over night to accomodate for this. The original approach of implementing such services into Next.js (and all other frameworks) is now contradicting with the recommended usage of RSC, where all components are meant to be server-side by default.
Therefore, i would advocate for changing the docs regarding this topic to the modern RSC-compliant way.
I am going to give a quick example of what i mean:
import CmsProvider from '@/_lib/cms/provider'
/*
* create a Wrapper component for all additional client-side services and use them in the root layout.
* in this example i created the <CmsProvider> for implementing the directus visual editor
*/
export default async function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html lang="de" suppressHydrationWarning>
<body suppressHydrationWarning>
<CmsProvider>{children}</CmsProvider>
</body>
</html>
)
}
What is the benefit of this?
Well, the concept of RSC implies that client components cannot not contain server components, meaning that any usage of a client component at the top of the component hierarchy, will automatically negate the whole purpose of RSCs.
The only exception to this rule is the root layout, where client-side wrapper components or providers can exist without transforming every child component into a client-side one (even though, this is technically a violation of the rule).
The provider in this case would look somewhere similar to this:
'use client'
import { useEffect } from 'react'
import { usePathname } from 'next/navigation'
import { suspendVisualEditor, initializeVisualEditor } from '@/_lib/cms/visualEditor'
export default function CmsProvider({ children }: { children: React.ReactNode }) {
const pathname = usePathname()
useEffect(() => {
// check if embedded in an iframe
if (typeof window === 'undefined' || window.top === window.self) return
// persist directus embedded status in session storage and toggle draft mode + visual editor accordingly
const isDirectusIframe = document.referrer.includes(process.env.NEXT_PUBLIC_CMS_BASE_URL)
const wasInDirectus = typeof window !== 'undefined' && sessionStorage.getItem('directus-embedded') === '1'
const acceptVisualEditing = isDirectusIframe || wasInDirectus
const hasDirectusCookie = document.cookie.includes('preview_client')
if (acceptVisualEditing && process.env.NEXT_PUBLIC_ENV === 'staging') {
document.body.classList.add('draft-mode')
sessionStorage.setItem('directus-embedded', '1')
// init visual editor if cookie is already present, enable draft mode (sets cookie) otherwise
if (hasDirectusCookie) {
initializeVisualEditor())
} else {
fetch(`/api/draft?slug=${pathname}`)
.then((response) => response.json())
.then((data) => {
console.info('draft mode enabled')
if (!data.redirect) return
initializeVisualEditor()
})
.catch((error) => console.error(error))
}
} else {
document.body.classList.remove('draft-mode')
sessionStorage.removeItem('directus-embedded')
// disable draft mode + suspend visual editor
fetch('/api/draft?disable=true')
.then(() => console.info('draft mode disabled'))
.catch((error) => console.error(error))
suspendVisualEditor()
}
}, [pathname])
return <>{children}</>
}
I would be willing to create a pull request for this, but i wanted to ask beforehand, if this is generally in the spirit of the directus docs.
Cheers!
- Dominant language
- TypeScript
- Stars
- 24
- Forks
- 90
- Avg merge
- 3d 22h
- Merged PRs (30d)
- 19
Getting set up
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from directus/docs
-
Docs Platform
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
directus/docs#88 · 1 comment ·
Maintainers usually reply within 2 days
-
llms.txt links agents to /raw/ copies where partials are empty; the inlined .md copies aren't linkedOpen
Difficulty 3/5 1-2 days Newbie friendliness 67/100
directus/docs#865 · 1 comment ·
Maintainers usually reply within 2 days
-
Document required special flags per field typeMay be free again A pull request for this issue was closed without being merged. Open
Difficulty 2/5 1-3 hours Newbie friendliness 58/100
directus/docs#850 · 3 comments ·
Maintainers usually reply within 2 days
-
Difficulty 3/5 1-2 days Newbie friendliness 55/100
directus/docs#771 · 3 comments ·
Maintainers usually reply within 2 days
-
content-improvement
Difficulty 5/5 Over a week Newbie friendliness 35/100
directus/docs#726 · 3 comments ·
Maintainers usually reply within 2 days
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
ranxianglei/billion-context#7597 ·
Maintainers usually reply within 1 day
-
kubernetes-ingestor: fetchCRDMapping() runs even when crossplane.enabled is false, logging a 403 ERROR every runPossibly taken @kc9ddi claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
TeraSky-OSS/backstage-plugins#228 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
zmkfirmware/zmk#3571 ·
Maintainers usually reply within 10 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
[Windows] Hidden title bar: a maximized window restored from minimized on a secondary monitor gets the primary monitor's work area as its client areaPossibly taken @lijy91 claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 67/100
libnativeapi/nativeapi#53 · 1 reaction ·
Maintainers usually reply within 1 day