-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
[source-contentful] offline mode #5869
Conversation
Deploy preview for using-drupal ready! Built with commit 4bde403 |
very first impression - change from env var to config might be problematic here - people would need to set it in advance - because when they would change |
Deploy preview for gatsbygram ready! Built with commit 4bde403 |
Ooh, that makes sense! I will revert that change |
// If the user knows they are offline, serve them cached result | ||
if (process.env.GATSBY_CONTENTFUL_OFFLINE === `true`) { | ||
console.log(`Using Contentful Offline cache ⚠️`) | ||
console.log(`Cache may be invalidated if you edit package.json, gatsby-node.js or gatsby-config.js files`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe those 2 lines could be moved after we read cached data, so it would would print out only if we actually can use it? Not totally sure on this, but this might be better develop experience?
// Fetch articles. | ||
console.time(`Fetch Contentful data`) | ||
|
||
// If the user knows they are offline, serve them cached result | ||
if (process.env.GATSBY_CONTENTFUL_OFFLINE === `true`) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should address very valid comment that is being removed below:
// For prod builds though always fail if we can't get the latest data
and add check here to only try read cache if we are running gatsby develop
. What do You think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sound good to me how do we know that gatsby develop
was used is there a command line var set?
@@ -65,8 +65,8 @@ exports.sourceNodes = async ( | |||
syncToken, | |||
spaceId, | |||
accessToken, | |||
environment, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this removal intentional? I think fetchData
does need environment
param to work correctly
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🙈
One more thing I think I will change the PR a bit so the offline fallback kicks in only when the server is not reachable, I can use is-reachable library |
Any reason you're not using touchNode? Gatsby garbage collects nodes that aren't created again but if a source plugin touches a node, it won't be GCed. See gatsby-source-drupal for an example of a plugin that uses this. |
@@ -19,7 +19,8 @@ module.exports = async ({ spaceId, accessToken, host, syncToken, cacheDir, envir | |||
console.time(`Fetch Contentful data`) | |||
|
|||
// If the user knows they are offline, serve them cached result | |||
if (process.env.GATSBY_CONTENTFUL_OFFLINE === `true`) { | |||
// For prod builds though always fail if we can't get the latest data | |||
if (process.env.GATSBY_CONTENTFUL_OFFLINE === `true` && process.env.ACTIVE_ENV === `development`) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe
- if (process.env.GATSBY_CONTENTFUL_OFFLINE === `true` && process.env.ACTIVE_ENV === `development`) {
+ if (process.env.GATSBY_CONTENTFUL_OFFLINE === `true` && process.env.NODE_ENV !== `production`) {
?
But yeah, we should probably make it easier to determine if we are in build
or develop
@KyleAMathews oh I was not aware of that, do you mean instead of saving the data to a json file we just touch the nodes? |
Yeah, gatsby already saves the cache. Instead you just grab all nodes created by this plugin and touch them. |
Thanks for running with this @Khaledgarbaya 👍 |
ok @pieh @KyleAMathews I made the change to use |
process.env.ACTIVE_ENV !== `production` | ||
) { | ||
|
||
_.values(store.getState().nodes) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can use
-_.values(store.getState().nodes)
+getNodes()
here
) { | ||
|
||
_.values(store.getState().nodes) | ||
.filter(n => n.internal.type === `gatsby-source-contentful`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think You need owner
. not type
host, | ||
environment, | ||
cacheDir: `${store.getState().program.directory}/.cache`, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cacheDir
not needed anymore I think?
@pieh I think this is ready for review |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
working as expected, very nice!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Re-approving and merging! Sorry you had to resolve package.json conflicts because I forgot to merge this in!
* Add fallback to offline * Fix lint * Cleanup * small fixes * format
closes #3977
This PR adds an option to the plugin
fallbackToOffline
that allows the user to continue working on their website if they don't have internet connection