问题
How to enable GATSBY_CONTENTFUL_OFFLINE=true
in GatsbyJS to access content offline?
The documentation suggests to export GATSBY_CONTENTFUL_OFFLINE=true
.
I have tried adding it in the config but still it doesn't work. Can anyone tell me where to add this export or else if the functionality actually works?
回答1:
You would use export …
in your .bashrc or similar. You're exporting a variable declaration from the shell script into your shell session, making it available as an environment variable. This prevents you from having to declare the variable on each invocation of gatsby [command]
.
You can also use .env.development
to declare this value (without export
) and Gatsby will pick it up, or you can use .env
with the dotenv package configured.
回答2:
It works, but:
- you have to be actually offline
NODE_ENV
is notproduction
(a.k.a notgatsby build
)
See the implementation here
So I think it's just a convenient flag that'll allow you to develop without an internet connection. I'm not sure what the doc meant by export ...
, but since they're checking for process.env.GATSBY_CONTENTFUL_OFFLINE
, you can set it by prepending it to whatever command you run in the terminal (ignore the $
):
$ gatsby develop
# GATSBY_CONTENTFUL_OFFLINE is undefined
$ GATSBY_CONTENTFUL_OFFLINE=true gatsby develop
# GATSBY_CONTENTFUL_OFFLINE is true
$ GATSBY_CONTENTFUL_OFFLINE=true gatsby build
# GATSBY_CONTENTFUL_OFFLINE is true, but contenful doesn't care
# because NODE_ENV is always set to `production`
When doing so (after turning off your network), you should see this logged out in the console:
Using Contentful Offline cache ⚠️
Cache may be invalidated if you edit package.json, gatsby-node.js or gatsby-config.js files
来源:https://stackoverflow.com/questions/54948364/how-to-enable-flag-for-gatsby-contentful-offline-true