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

[source-contentful] offline mode #5869

Merged
merged 8 commits into from
Jun 21, 2018
Merged

[source-contentful] offline mode #5869

merged 8 commits into from
Jun 21, 2018

Conversation

Khaledgarbaya
Copy link
Contributor

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

@Khaledgarbaya
Copy link
Contributor Author

@pieh here is the continuation of #3977

@gatsbybot
Copy link
Collaborator

gatsbybot commented Jun 13, 2018

Deploy preview for using-drupal ready!

Built with commit 4bde403

https://deploy-preview-5869--using-drupal.netlify.com

@pieh
Copy link
Contributor

pieh commented Jun 13, 2018

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 gatsby-config.js when they are already offline it would cause .cache directory to be deleted

@gatsbybot
Copy link
Collaborator

gatsbybot commented Jun 13, 2018

Deploy preview for gatsbygram ready!

Built with commit 4bde403

https://deploy-preview-5869--gatsbygram.netlify.com

@Khaledgarbaya
Copy link
Contributor Author

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`)
Copy link
Contributor

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`) {
Copy link
Contributor

@pieh pieh Jun 13, 2018

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?

Copy link
Contributor Author

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,
Copy link
Contributor

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙈

@Khaledgarbaya
Copy link
Contributor Author

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

@KyleAMathews
Copy link
Contributor

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`) {
Copy link
Contributor

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

@Khaledgarbaya
Copy link
Contributor Author

@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?

@KyleAMathews
Copy link
Contributor

Yeah, gatsby already saves the cache. Instead you just grab all nodes created by this plugin and touch them.

@uttrasey
Copy link

Thanks for running with this @Khaledgarbaya 👍

@Khaledgarbaya
Copy link
Contributor Author

ok @pieh @KyleAMathews I made the change to use touchNode instead I hope I didn't miss anything

process.env.ACTIVE_ENV !== `production`
) {

_.values(store.getState().nodes)
Copy link
Contributor

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`)
Copy link
Contributor

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`,
Copy link
Contributor

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?

@Khaledgarbaya
Copy link
Contributor Author

@pieh I think this is ready for review

pieh
pieh previously approved these changes Jun 19, 2018
Copy link
Contributor

@pieh pieh left a 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!

Copy link
Contributor

@pieh pieh left a 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!

@pieh pieh merged commit bd5d76e into gatsbyjs:master Jun 21, 2018
@Khaledgarbaya Khaledgarbaya deleted the feat/contentful-offline branch June 21, 2018 13:27
KyleAMathews pushed a commit that referenced this pull request Jun 21, 2018
* Add fallback to offline

* Fix lint

* Cleanup

* small fixes

* format
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants