Next JS为Sanity CMS重写,登录后给出404页面



我目前正在Netlify上托管一个Next JS网站,该网站具有用于内容编辑的Sanity CMS后端。我已经将其配置为在单域IE上运行-examplesite.com有前端网站,examplesite.com/sanity在那里打开sanity Studio编辑器。

如果编辑器已经有一个登录cookie(来自以前的登录等(,这将正常工作,但如果编辑器必须登录,则在完成登录过程后,网站将重定向到/s健全/tesk,Next将显示404页面。然后,编辑器必须访问/s健全性才能访问CMS,在那里它成功地重定向到/s健全性/tesk(编号404(。

根据官方的Sanity设置指南,我在Next配置中应用了以下重写。

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"],
},
});

为了完整起见,这里还有我的sanity.json:

{
"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"
}
]
}

我的假设是,/s健全之后的任何路径都会重定向到build命令(下面的build命令(输出的健全/index.html文件,但除了/s健全之外,它似乎对任何路径都不能正常工作。

Build命令(在package.json中(:

{
"scripts": {
...
"prebuild": "echo 'Building Sanity to public/sanity' && cd sanity && yarn && npx @sanity/cli build ../public/sanity -y && echo 'Done'",
...
},

非常感谢您的帮助!

不知道你是否成功地对它进行了排序,但这对我来说是有效的。

/next.config.js

/** @type {import('next').NextConfig} */
const STUDIO_REWRITE = {
source: "/studio/:path*",
destination: process.env.NODE_ENV === "development" ? 
"http://localhost:3333/studio/:path*" : "/studio/index.html",
};
const nextConfig = {
i18n: {
locales: ["en"],
defaultLocale: "en",
},
rewrites: async () => [STUDIO_REWRITE],
reactStrictMode: true,
images: {
domains: ['cdn.sanity.io'],
},
}
module.exports = nextConfig

/netlify.toml

[[redirects]]
from = "/studio/*"
to = "/studio/index.html"
status = 200
force = false

相关内容

  • 没有找到相关文章

最新更新