-
Notifications
You must be signed in to change notification settings - Fork 36
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
ISR does not work in in presentation mode #998
Comments
@LKosters have you figured out a way to get presentation mode working? I have my site hosted on Vercel and I have not been able to get Sanity to connect. I get the same error messages. I tried setting |
@lwaldner I cannot get it to work either, for now I just turn of ISR but I would rather have it on for performance. |
I can confirm this issue. The presentation mode seems to break as soon as you add any, even just a single, As a temporary solution I deploy a |
@LKosters @marco-land I'm getting this error without any route rules, would you guys mind sharing how you set this up? |
My export default defineNuxtConfig({
// ...
ssr: true,
sanity: {
projectId: process.env.NUXT_SANITY_PROJECT_ID,
dataset: process.env.NUXT_SANITY_DATASET,
useCdn: false,
apiVersion: process.env.NUXT_SANITY_API_VERSION || "2024-03-15",
visualEditing: {
studioUrl: process.env.NUXT_SANITY_STUDIO_URL || "http://localhost:3333",
token: process.env.NUXT_SANITY_TOKEN,
stega: true,
},
},
}) my export default defineNuxtConfig({
// ...
ssr: true,
sanity: {
projectId: process.env.NUXT_SANITY_PROJECT_ID,
dataset: process.env.NUXT_SANITY_DATASET,
useCdn: false,
apiVersion: process.env.NUXT_SANITY_API_VERSION || "2024-03-15",
},
nitro: {
prerender: {
crawlLinks: true,
},
},
routeRules: {
"/**": { prerender: true },
},
}) my export default defineConfig({
// ...
plugins: [
// ...
presentationTool({
previewUrl: {
// make sure that the origin is the the `preview` branch URL
origin: process.env.SANITY_STUDIO_PREVIEW_URL || 'http://localhost:3000',
previewMode: {
enable: '/preview/enable',
disable: '/preview/disable',
},
},
}),
]
}); I hope this helps, in this case I was using |
I've done some testing of this today. I'm unable to replicate any "Unable to connect to visual editing" issues when ISR is enabled, everything does work as expected for me with one major caveat: Currently there is no cache separation for routes dependent on if they are rendered in preview or non-preview mode. As far as I'm aware this means that preview pages do get cached when using ISR, and those cached pages are potentially served/leaked to users who do not have preview mode enabled. I'm not aware of a method to set cache keys to differentiate preview rendered pages, so for now I'd recommend @marco-land's approach until we can work this one out. Thanks for raising and for your patience. |
I tested this out. Unfortunately @marco-land's approach does not work in all cases. For instance, Vercel (one of the bigger platforms) automatically adds a password protection on the non-production branches. I also noticed that end users have trouble understanding the different URL's and that keeping two branches with the same code base (but different configuration) is possible, but far from ideal (it requires more complex pipelines to ensure that code pushed to a I think this issue has a big impact on possible integrations of Sanity with Nuxt: all possible work-arounds are not working in a real case scenario. One must make a decision between:
All in all, there is no optimal, developer friendly way to use Sanity with preview mode with Nuxt. Would be great to get some guidance here from @rdunk (sanity) or @danielroe (nuxt), on how to get this in. I am more than OK with spending time to also help to get this working, but would be great to see/discuss what the integration method/starting point of this should be. @danielroe any suggestions? |
Sanity has now added a way to circumvent the Vercel's Deployment Protection using the deployment protection bypass for automation. How to use this new tool by Sanity, is explained here and here. With this, I now have the Presentation Tool working on a subdomain on Vercel, that is protected by their Deployment Protection. I have also separated our ISR rules, and created a custom environment for it (although this is a Vercel Pro/Enterprise only feature), that activates Visual Editing. Our custom environment tracks our main branch, and is therefore also always up to date export default defineNuxtConfig({
...,
$env: {
livepreview: {
routeRules: {
'/': { isr: false },
'/**': { isr: false },
'/other-routes': { isr: false },
},
sanity: {
visualEditing: {
studioUrl: process.env.NUXT_SANITY_STUDIO_URL,
token: process.env.NUXT_SANITY_VISUAL_EDITING_TOKEN,
stega: true,
},
},
},
},
routeRules: {
'/': { isr: 60 },
'/**': { isr: 1800 },
'/other-routes': { isr: 3600 },
},
sanity: {
projectId: process.env.NUXT_SANITY_PROJECT_ID,
dataset: process.env.NUXT_SANITY_DATASET,
apiVersion: process.env.NUXT_SANITY_API_VERSION || '2024-12-02',
useCdn: true,
},
} To build with Visual editing disabled in production, preview and development, I used Vercel's Custom Environments to make an environment named livepreview, I then added an environment variable to Vercel named I updated our package.json's build command to the following, per @danielroe's suggestion below: "build": "nuxt build --envName $NUXT_BUILD_ENV" Now, whenever the main branch is updated, both production and livepreview builds. Livepreview includes visual editing. Unfortunately, this does not solve it for most users, as Custom Environments on Vercel, are only available for Pro and Enterprise plans. Let me know if you need any clarifications, as this is definitely a complex way of making it work, and should be made much simpler. |
This is worth documenting for Vercel users, I think. I think the buildCommand + vercel.sh setup could be simplified by checking VERCEL_ENV in your build command, or even using a local module ( |
That makes sense, and simplifying it would definitely be optimal. I've contacted Vercel about overwriting the Unfortunately, I've run into another issue, after getting a co-worker to test it, and it seems like Vercel is still blocking his access through the Presentation Tool. I'm trying to figure out if it requires the user to be logged in on the domain that has the build with the presentation tool, which would kinda defeat the purpose. |
@danielroe I have since my last comment simplified the build process with your suggestion, and it works without issues. Vercel also suggested not overwriting their In regards to my previous comment, I tested it some more, and figured out that it was an internal firewall blocking the custom domain internally, causing the implementation to not work, when on our local network. The domain was whitelisted, and it now works out of the box. Unfortunately, Custom environments are still locked behind the Pro and Enterprise plans. The only alternative to paying for Vercel, as far as I can see, is living with Visual editing being included on the development or preview environments. |
Version
module: 1.10.1
nuxt: 3.9.3
Nuxt configuration
routeRules: { "/**": { isr: 3600, cors: true, }, "/preview/**": { isr: false, cors: true, }, },
Additional information
An alternative idea would be to turn off the ISR feature, however this would lead to a degraded page performance.
Checklist
Steps to reproduce
Clone nuxt sanity repo with the movie example, set ISR to 3600 (other values will also work), build it to a live environment (I use Vercel to host my live envoirment) and use that url in presentation mode from studio.
What is expected?
I can edit my file in presentation mode while ISR is turned on.
What is actually happening?
I get the following error in presentation mode:
"Unable to connect, check the browser console for more information."
Error in browser console:
"Unable to connect to visual editing. Make sure you've setup '@sanity/visual-editing' correctly"
The text was updated successfully, but these errors were encountered: