datocms / gatsby-source-datocms

Official GatsbyJS source plugin to pull content from DatoCMS
MIT License
140 stars 50 forks source link

previewMode not working correctly on gatsby cloud with gatsby v3 version #165

Closed Sarankrishna closed 3 years ago

Sarankrishna commented 3 years ago

In gatsby cloud, for our website project, we have set the value of previewMode to true, but the plugin gatsby-source-datocms uses only the published content from datocms. Ideally, it should use the non-published content as well. Refer to the below code for plugin configuration in our website project.

{ resolve:gatsby-source-datocms, options: { apiToken: process.env.DATO_API_TOKEN, environment: process.env.DATO_ENVIRONMENT ||main, previewMode: (process.env.DATO_PREVIEW_MODE || 'FALSE').toLocaleUpperCase() === "TRUE" }, }, In gatsby cloud environment variable we have the variable DATO_PREVIEW_MODE with value 'true'

Note that we are using the gatsby-source-datocms version 2.6.6. Then I tried upgrading to the latest version 2.6.15 and tested. But the issue still exists.

Surprisingly previewMode is working correctly when we make the build from the local machine.

Another point to note is that previewMode working correctly on gatsby cloud with the website project using the gatsby v2 version.

we came across this issue while migrating our website gatsby project from gatsby v2 to gatsby v3 version. So this issue is holding us on the release of our new version of the website. Please help us to resolve this issue.

Sarankrishna commented 3 years ago

Note: I saw issue #157, And added the environment variable GATSBY_IS_PREVIEW with value "true" in gatsby cloud and checked. But the issue still exists.

stefanoverna commented 3 years ago

Are you able to add the following line to your gatsby config file and report back the result you see in Gatsby Cloud please?

console.log("----------------------- DATO PREVIEW MODE ---------------------------");
console.log((process.env.DATO_PREVIEW_MODE || 'FALSE').toLocaleUpperCase());
console.log("---------------------------------------------------------------------");
Sarankrishna commented 3 years ago

Below is the log from gatsby cloud

21:28:27 PM:TRUE
21:28:27 PM:----------------------- DATO PREVIEW MODE ---------------------------
21:28:27 PM:---------------------------------------------------------------------

the order off the log is mismatching with the code , that might be because of the gatsby cloud log rendering issue. But value of (process.env.DATO_PREVIEW_MODE || 'FALSE').toLocaleUpperCase() is "TRUE" only

stefanoverna commented 3 years ago

Thanks, can you also try with this one please?

console.log("----------------------- GATSBY_IS_PREVIEW ---------------------------");
console.log('GATSBY_IS_PREVIEW', JSON.stringify(process.env.GATSBY_IS_PREVIEW) || 'NOT_SET');
console.log("---------------------------------------------------------------------");
Sarankrishna commented 3 years ago

I added the log and below is the log from the gatsby cloud build

15:13:44 PM:----------------------- GATSBY_IS_PREVIEW ---------------------------
15:13:44 PM:GATSBY_IS_PREVIEW "false"

But I have set the value GATSBY_IS_PREVIEW true in gatsby cloud environment variable.

Screenshot 2021-06-09 at 3 17 13 PM
stefanoverna commented 3 years ago

Thanks! That's the issue I think. Could you try to ask Gatsby Cloud why the env variable is set as "false" then?

Sarankrishna commented 3 years ago

@stefanoverna Thanks for the help. I will get in touch with gatsby cloud team regarding GATSBY_IS_PREVIEW variable.

However I have a question, this variable GATSBY_IS_PREVIEW I added recently only after the below code for previewMode setting in gatsby-source-datocms plugin is not working. It was working without GATSBY_IS_PREVIEW variable only by having the below code in gatsby-config.js. The only difference now in our code is that, now we are using the gatsby v3 version. I think the gatsby version should not make any problem. Please let me know is there anything I can do to make it work without GATSBY_IS_PREVIEW variable?.

    {
      resolve: `gatsby-source-datocms`,
      options: {
        apiToken: process.env.DATO_API_TOKEN,
        environment: process.env.DATO_ENVIRONMENT || `main`,
        previewMode: (process.env.DATO_PREVIEW_MODE || 'FALSE').toLocaleUpperCase() === "TRUE"
      },
stefanoverna commented 3 years ago

Can you try 2.6.16 and see if it solves the issue? I've tried to only consider GATSBY_IS_PREVIEW env if it's true

Sarankrishna commented 3 years ago

@stefanoverna 2.6.16 worked. Now it is using the previewMode value set in gatsby-config.js file. Thanks a lot for the fix.

Sarankrishna commented 3 years ago

@stefanoverna , we have got the response from Gatsby cloud team on why "GATSBY_IS_PREVIEW" was not getting updated, when we set the value in the Environment variable section of the gatsby cloud. "GATSBY_IS_PREVIEW" is a reserved variable name used by gatsby team. So they are suggesting to use a different unique variable name. Below is the response from gatsby team.

Good news,  I have determined the issue here!  The `GATSBY_IS_PREVIEW` variable name is a reserved name for Gatsby Cloud that we set and use.  Unfortunately it was undocumented so there was no way for you to know.  We are adding it to the documentation, but you should change that variable name to something else unique.  Thanks!

So I think you should not use the variable "GATSBY_IS_PREVIEW" in this plugin.

stefanoverna commented 3 years ago

Thanks for the update, but when exactly GATSBY_IS_PREVIEW is set as true/false then? 😄

Sarankrishna commented 3 years ago

@stefanoverna I don't have details on the variable GATSBY_IS_PREVIEW, since that variable is used by the gatsby cloud system. Gatsby cloud team said that they will update documentation about this variable. If find a document related to this variable I will update it here.

So are you going to change the variable name GATSBY_IS_PREVIEW that is used in this plugin?

stefanoverna commented 3 years ago

It really depends on what the env variable means! The variable name is not casual on our end, we wanted to automatically turn on preview mode if Gatsby Cloud was in.. duh, preview mode :) Turns out that sometimes it's not set. We'll wait for the documentation and decide what to do when we have the info.

Thanks!

— Stefano Verna Founder and CEO DatoCMS

--- original message --- On June 24, 2021 at 5:08 PM GMT+2 @.*** wrote:

@stefanoverna I don't have details on the variable GATSBY_IS_PREVIEW, since that variable is used by the gatsby cloud system. Gatsby cloud team said that they will update documentation about this variable. If find a document related to this variable I will update it here.

So are you going to change the variable name GATSBY_IS_PREVIEW that is used in this plugin?

You are receiving this because you were mentioned.

Reply to this email directly, view it on GitHub, or unsubscribe. --- end of original message ---

graysonhicks commented 3 years ago

👋 Here is the list of reserved variables in Gatsby Cloud: https://support.gatsbyjs.com/hc/en-us/articles/360052322954-Environment-Variables-Specific-to-Gatsby-Cloud

stefanoverna commented 2 years ago

Thanks @graysonhicks. Maybe you can coordinate with @Sarankrishna to understand why the GATSBY_IS_PREVIEW environment variable is set to "false" for their Gatsby Cloud project then?

Thanks!