Skip to content
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

Enable preview using a query string passed? #1044

Open
notflip opened this issue Aug 14, 2024 · 0 comments
Open

Enable preview using a query string passed? #1044

notflip opened this issue Aug 14, 2024 · 0 comments
Assignees
Labels
question Further information is requested

Comments

@notflip
Copy link

notflip commented Aug 14, 2024

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!

@notflip notflip added the question Further information is requested label Aug 14, 2024
@rdunk rdunk self-assigned this Aug 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants