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

Draft data not returning #799

Open
EmilyFlocc opened this issue May 22, 2023 · 5 comments
Open

Draft data not returning #799

EmilyFlocc opened this issue May 22, 2023 · 5 comments
Labels
bug Something isn't working

Comments

@EmilyFlocc
Copy link

EmilyFlocc commented May 22, 2023

Version

module: 0.9.1
nuxt: 2.15.5

Nuxt configuration

{
  projectId: '*****',
  dataset: 'production',
  apiVersion: '2021-10-21', (have tried with and without this)
}

Reproduction

I have a 'preview mode' on my site for clients to see their drafts as they build them, which has been working for over a year, but my sanity client now isn't returning any draft data. It used to, but for whatever reason it no longer is?

If the document is published, then it returns correctly, but if unpublished nothing returns. This is the case on both complex and simple queries, so my test *[_type == 'blog' && slug.current == 'test'] should return data, and does in the sanity vision tab, but doesn't in my nuxt application unless I publish this blog.

Has something changed with the sanity end point? Do I need to update my queries or my client to replicate that?

I know I am using a deprecated version, but its a big job for me to upgrade all my sites to v3, and one I don't have time for at the moment.

@EmilyFlocc EmilyFlocc added the bug Something isn't working label May 22, 2023
Copy link
Collaborator

Hm. Nothing has changed in this module. But it sounds like an issue with authentication? Maybe you are no longer providing your token when fetching data from sanity? Can you inspect the request in your browser?

@EmilyFlocc
Copy link
Author

Thank you for getting back to me @danielroe, you are completely right I needed to add withCredentials: true, to my config. My dataset is set to public, you don't need a token to access it, so I'm not sure why I need this boolean? Would you be able to explain it to me?

@danielroe
Copy link
Collaborator

I don't know what might have changed here upstream with Sanity, but I don't think Sanity returns draft posts unless credentials are passed. Without setting withCredentials, I don't think these would be passed by default.

Is there any chance that you were previously setting a token that bypassed the need to reuse existing credentials?

@EmilyYond
Copy link

Hi @danielroe, I am having a similar issue and hoping you can help. I have set my config to withCredentials: true and most of the time it picks up peoples credentials because they are logged into sanity from the same browser. However, for some of my clients its not working and so they can never preview the pages, even when they are logged in to sanity. I want to pass a read access token I've generated from sanity into the nuxt/sanity module so that it always uses this token and therefore will always work if they are in preview mode.

I have added the following to nuxt.config (I'm using nuxt 2)

privateRuntimeConfig: {
    sanity: {
      token: process.env.SANITY_TOKEN,
    },
  },

And then I have added a sanity.js plugin with the following:

export default async ({ req, $sanity, $config: { SANITY_TOKEN } }) => {
  // https://v0.sanity.nuxtjs.org/getting-started/usage#settoken
  //   const token = getTokenFromReq(req) //not sure where this is coming from?
  $sanity.setToken(SANITY_TOKEN)
}

But I still can't preview the pages if I'm on an incognito window. Do you know what I'm missing?

@danielroe
Copy link
Collaborator

@EmilyYond privateRuntimeConfig will only be accessible on the server. If you want to access this on the client-side (which likely you do), you can instead use publicRuntimeConfig.

Or if you need to 'guard' access to this token, you should probably keep it in privateRuntimeConfig, create a Nuxt 2 server middleware, and then only return this token from the middleware once you've authenticated your user.

@danielroe danielroe removed their assignment Sep 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants