I'm currently hosting a Next JS site on Netlify with a Sanity CMS backend for content editing. I've got it configured to run on the single domain IE - examplesite.com has the front-end website, where examplesite.com/sanity opens the Sanity Studio editor.
This works as intended if the editor has a log-in cookie already (from previous login, etc), but if the editor has to login, after going through the login process the site is redirected to /sanity/desk and Next displays a 404 page. The editor then has to visit /sanity to access the CMS, where it redirects successfully to /sanity/desk (no 404).
Following the official Sanity setup guide, I have the following rewrite applied in the Next config.
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"],
},
});
For completeness, here is my sanity.json as well:
{
"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"
}
]
}
My assumption was that any path after /sanity would be redirected to the sanity/index.html file output by the build command (build command below), but it doesn't seem to be functioning correctly for any path except just /sanity.
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 help greatly appreciated!