I am currently running a Next JS site on Netlify with a Sanity CMS backend for content management. The setup involves having the front-end website on examplesite.com and accessing the Sanity Studio editor through examplesite.com/sanity.
Everything works as expected if the editor already has a login cookie, but when the editor needs to log in, upon completing the login process, the site redirects to /sanity/desk which causes Next to display a 404 page. To access the CMS, the editor then has to manually visit /sanity first before being redirected successfully to /sanity/desk without any errors.
Following the official Sanity setup guide, I have made the following adjustments in the Next configuration:
const SANITY_REWRITE = {
source: "/sanity/:path*",
destination:
process.env.NODE_ENV === "development"
? "http://localhost:3333/sanity/:path*"
: "/sanity/index.html",
};
const DESK_REWRITE = {
source: "/sanity/desk",
destination: "/sanity/index.html",
};
const withBundleAnalyzer = require("@next/bundle-analyzer")({
enabled: process.env.ANALYZE === "true",
});
module.exports = withBundleAnalyzer({
reactStrictMode: true,
async rewrites() {
return {
beforeFiles: [SANITY_REWRITE, DESK_REWRITE],
};
},
images: {
domains: ["cdn.sanity.io"],
},
});
Here is my sanity.json for further reference:
{
"root": true,
"project": {
"name": "main-site",
"basePath": "/sanity"
},
"api": {
"projectId": "ga8f69l8",
"dataset": "production"
},
"plugins": [
"@sanity/base",
"@sanity/components",
"@sanity/default-layout",
"@sanity/default-login",
"@sanity/desk-tool",
"@sanity/dashboard",
"dashboard-widget-netlify"
],
"env": {
"development": {
"plugins": ["@sanity/vision"]
}
},
"parts": [
{
"name": "part:@sanity/base/schema",
"path": "./schemas/schema"
},
{
"name": "part:@sanity/desk-tool/structure",
"path": "./structures/deskStructure.js"
},
{
"implements": "part:@sanity/dashboard/config",
"path": "src/dashboardConfig.js"
}
]
}
The assumption was that any path after /sanity would be routed to the sanity/index.html file generated by the build command (build command included below), however, it seems to be working only for the /sanity path and not for any other additional paths.
Build command (in package.json):
{
"scripts": {
...
"prebuild": "echo 'Building Sanity to public/sanity' && cd sanity && yarn & npx @sanity/cli build ../public/sanity -y && echo 'Done'",
...
},
Any assistance on this issue would be highly appreciated!