Hi,
I love working with prismic. It fits exactly my need. There is only one thing I can't make it work showing the current draft preview inside an astro project.
Does anyone have an example I could use to show a simple preview inside Astro
Thank you @Priyanka for your message.
I already setup prismic and astro. I show my published pages without any issues.
My problem concerns the previews.
I can't make it work with astro. Also I already search on the forum for an answer but I didn't find any.
Create preview pages like pages/preview/home.astro taking the above as an example
---
export const prerender = false; // needs to be server side rendered
const previewCookie = Astro.cookies.get("io.prismic.preview").value;
if (!previewCookie) {
return Astro.redirect("/404");
}
const data = await client.get(..., {
ref: previewCookie
})
---
// render with the preview data
// Note: don't allow this page to be indexable, etc.
Change the astro.config.mjs
export default defineConfig({
output: "server", // so you can use the `export const prerender`
...
})
Note: create the redirect preview link on Prismic Repo Settings to http://localhost:3000/preview or other domain.
To add more previews just create a preview page & add a condition to preview.astro
How this works is that you're using the prismic cookie to understand what entity you're previewing. Then you can fetch data when serving pages via SSR.
There are ways you can automate this, e.g you can create a dynamic [dynamic].astro preview that does the logic above automatically when using slices. But this is more complex.
There are no built-in solutions for this, I kinda created my own!
Thank you for you feedback!
This is pretty much what I did and I found my problem.
I was logging the prismic document into the console and I'm using Brave browser. I found that I had to allow cookies as described here Troubleshooting: Previews
Now everything is working fine.
Thank you again for you answer
Thanks, @g.rosado, for the working example. I will pass this on to my dedicated team and see if we can implement documentation.
Thanks, @prismic27, for the feedback.