Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: upgrade sanity init for Next.js to next-sanity v9 #6644

Merged
merged 2 commits into from
May 21, 2024

Conversation

stipsan
Copy link
Member

@stipsan stipsan commented May 13, 2024

Description

This is an alternative take to #6617.

Updates the sanity init cli when used in Next.js apps that embed Studios to the best practices in next-sanity v9:

The biggest difference is that next-sanity v9 only supports App Router, so the CLI no longer asks if you want to use App Router or not.
That doesn't mean the whole app has to be in App Router, it's only relevant to the route that embeds the Studio. In fact, the entire rest of the app can be in Pages Router.
Secondly, the latest NextStudio component supports fast SSR hydration, and it works best if the sanity.config.ts has the 'use client' directive at the top, instead of the [[...tool]]/page.tsx file.

What to review

sanity init inside of next v14 apps should work fine, wether or not the app is using Pages Router or App Router. If it can load the studio on http://localhost:3000/studio then it works.

Testing

Existing tests should cover the changes, as it's a refactor of the template itself, and removal of a prompt that's no longer needed (the prompt that asks if you want to use App Router or not).

Notes for release

The sanity init command for Next.js embedded Studios are updated to next-sanity v9 and next v14 best practices.

Closes #6617

@stipsan stipsan requested a review from a team as a code owner May 13, 2024 23:01
@stipsan stipsan requested review from jtpetty and removed request for a team May 13, 2024 23:01
Copy link

vercel bot commented May 13, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
performance-studio ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 19, 2024 5:43pm
studio-workshop 🔄 Building (Inspect) Visit Preview 💬 Add feedback May 19, 2024 5:43pm
test-next-studio ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 19, 2024 5:43pm
test-studio ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 19, 2024 5:43pm

Copy link
Contributor

No changes to documentation

Copy link
Contributor

github-actions bot commented May 13, 2024

Component Testing Report Updated May 19, 2024 5:48 PM (UTC)

File Status Duration Passed Skipped Failed
comments/CommentInput.spec.tsx ✅ Passed (Inspect) 35s 15 0 0
formBuilder/ArrayInput.spec.tsx ✅ Passed (Inspect) 7s 3 0 0
formBuilder/inputs/PortableText/Annotations.spec.tsx ✅ Passed (Inspect) 25s 6 0 0
formBuilder/inputs/PortableText/copyPaste/CopyPaste.spec.tsx ✅ Passed (Inspect) 31s 11 7 0
formBuilder/inputs/PortableText/Decorators.spec.tsx ✅ Passed (Inspect) 14s 6 0 0
formBuilder/inputs/PortableText/FocusTracking.spec.tsx ✅ Passed (Inspect) 36s 15 0 0
formBuilder/inputs/PortableText/Input.spec.tsx ✅ Passed (Inspect) 1m 20s 19 0 0
formBuilder/inputs/PortableText/ObjectBlock.spec.tsx ❌ Failed (Inspect) 1m 9s 17 0 1
formBuilder/inputs/PortableText/PresenceCursors.spec.tsx ✅ Passed (Inspect) 7s 3 9 0
formBuilder/inputs/PortableText/RangeDecoration.spec.tsx ✅ Passed (Inspect) 20s 9 0 0
formBuilder/inputs/PortableText/Styles.spec.tsx ✅ Passed (Inspect) 14s 6 0 0
formBuilder/inputs/PortableText/Toolbar.spec.tsx ✅ Passed (Inspect) 28s 12 0 0

@@ -39,27 +41,7 @@ const dataset = process.env.NEXT_PUBLIC_SANITY_DATASET
export default defineCliConfig({ api: { projectId, dataset } })
`

export const sanityStudioPagesTemplate = `import Head from 'next/head'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are changing the name of one exported const and deleting two others. I'm assuming this is intended but just wanted to call this out to make sure.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, that's correct.
The name sanityStudioPagesTemplate allude to the format used to mount the studio in pages/studio/[[...tool]].tsx paths, which is handled by the Next.js Pages Router: https://nextjs.org/docs/pages/building-your-application/routing/pages-and-layouts

next-sanity v9 only supports App Router routes app/studio/[[...tool]]/page.tsx mount points: https://nextjs.org/docs/app/building-your-application/routing/defining-routes

Due to the two routes starting to diverge too much in how they handle meta data tags, and other APIs, we decided to stop supporting Pages Router and started to simplify the API surface and docs.
As a result sanityStudioPagesTemplate is no longer needed and produces invalid code.

For example this export no longer exists:

import { metadata } from 'next-sanity/studio/metadata'

And since there are no longer a need to maintain two templates it no longer makes sense to specify AppTemplate in the name :)

@stipsan stipsan force-pushed the upgrade-next-sanity-9-init branch from 64c024a to c28f7c3 Compare May 16, 2024 09:52
@stipsan stipsan enabled auto-merge May 16, 2024 09:53
@stipsan stipsan changed the title feat: upgrade sanity init for Next.js to next-sanity v9 best practices feat: upgrade sanity init for Next.js to next-sanity v9 May 16, 2024
@stipsan stipsan added this pull request to the merge queue May 21, 2024
Merged via the queue into next with commit f48e38b May 21, 2024
37 of 38 checks passed
@stipsan stipsan deleted the upgrade-next-sanity-9-init branch May 21, 2024 10:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants