Skip to content

Enable preview using a query string passed? #1044

Open
@notflip

Description

I'm trying to get the "Open Preview" button functionality working, see: https://www.sanity.io/docs/migrating-production-url-resolver

Screenshot 2024-08-14 at 11 07 02

most of the example code looks something like this:

productionUrl: async (prev, context) => {
  const { getClient, dataset, document } = context;
  
  const client = getClient({ apiVersion: '2022-06-07' }); // Ensure to specify the correct API version
  const slug = await client.fetch(`*[_id == $id][0].slug.current`, { id: document._id });

  // Define your base production URL
  const baseUrl = 'https://your-production-site.com';

  // Build the URL based on the document type
  let url;
  switch (document._type) {
    case 'page':
      url = `${baseUrl}/${slug}/`;
      break;
    case 'post':
      url = `${baseUrl}/posts/${slug}`;
      break;
    default:
      url = baseUrl;
  }

  // Optionally add any query parameters
  return `${url}?preview=true`; // <------------- see this line in particular
}

This uses a query string to enable the preview mode, instead of first browsing to /preview/enable.
Is this possible at the moment? To enable the preview mode by passing a query string?

EDIT 1:
I tried the following: created a sanity.client.js file in the plugins, but the previews mode is not enabled so it seems

export default defineNuxtPlugin((nuxtApp) => {
  const { query } = useRoute()

  if (query.preview) {
    useSanityLiveMode()
  }
})

EDIT 2:

I saw in the code that there's a redirectTo variable, so that we can use the default /preview/enable.

return `${process.env.SANITY_STUDIO_PREVIEW_URL}/preview/enable?redirectTo=${slug}`

The only issue now, is that we need to find a way to add the secret to this link, the token is stored in the web .env, should it be stored in the studio .env as well, and passed here?
Thanks!

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

Labels

questionFurther information is requested

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions