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

Wordpress and gatsby cache/refesh wordpress content #18040

Closed
nelsonmiew opened this issue Oct 2, 2019 · 14 comments
Closed

Wordpress and gatsby cache/refesh wordpress content #18040

nelsonmiew opened this issue Oct 2, 2019 · 14 comments
Labels
stale? Issue that may be closed soon due to the original author not responding any more. status: awaiting author response Additional information has been requested from the author topic: source-wordpress Related to Gatsby's integration with WordPress type: question or discussion Issue discussing or asking a question about Gatsby

Comments

@nelsonmiew
Copy link

Hello,
I'm trying to build a gatsby solution with Wordpress CMS content.
When I update Posts on WordPress, i need to restart the gatsby server to get new content on gatsby solution.

What's the best solution to keep content updated on frontend gatsby? thanks.

@jonniebigodes jonniebigodes added status: awaiting author response Additional information has been requested from the author type: question or discussion Issue discussing or asking a question about Gatsby labels Oct 2, 2019
@jonniebigodes
Copy link

@nelsonmiew i'm inclined to believe that you're using gatsby with the appropriate plugin gatsby-source-wordpress, correct? That plugin is intented only to fetch items you've configured during the build process. When you update, add or delete the content. You'll have to restart the development server. A while ago there was a addition to gatsby that would allow you to refresh the data, more on that here, see if this helps in with your issue.

Feel free to provide feedback so that we can close this issue or continue to work on it until we find a suitable solution.

@DmacTorstar
Copy link

DmacTorstar commented Oct 3, 2019

@jonniebigodes I'm having this same issue.
It seems that this __refresh endpoint is extremely useful, and should be more prominent, and easier to trigger. Perhaps gatsby should have a configuration option to refresh the data on every page reload, or on every build?
This would be invaluable for when a developer is building out the UI and the content model in parallel, because currently the flow of updating/debugging the model and adding/updating data in the CMS is extremely tedious and painful.
I was almost ready to give up on gatsby until I found this tidbit, and I'm so glad I did, because it's exactly what I was looking for.

Currently, the workaround is just to add axios.post('/__refresh') at the top of the index.js file so that it is called on every refresh, but I think this could be handled more elegantly and in a way that is more obvious in the documentation.

@jonniebigodes
Copy link

@DmacTorstar from what i remember this was introduced to address this type of issue, it's a bit cumbersome i understand, but when it was added, it was to allow plugins to fetch content like this one and other sources. Fetching the data at every page load would defeat a bit the purpose of the plugin. I think a better approach would probably be refetch at regular intervals something like gatsby-source-graphql does here.

@DmacTorstar
Copy link

@jonniebigodes Yes I think its a step in the right direction. Having a refresh interval would be good, but a manual way to do it would be great also while developing. In production, there is much less of a need to refresh the data so often. But during development, even 60 seconds is kind of a long time when you are going back and forth editing the data.

@jonniebigodes
Copy link

@DmacTorstar the refresh interval would be applied to development mode. For production mode i believe the safe bet would be the de facto stance on how gatsby does it. As it's really hard for gatsby to know what changed and that could lead to more bugs and inconsistencies in the data.

@leepowelldev
Copy link

Just been playing with the __refresh hook - very handy. Thanks. Seems to work great with Wordpress except on trashing/deleting pages ... even when they're deleted via the admin area and non-existant in the DB the graphql query is still returning them upon refresh as being published. Even in GraphiQL I can query and see them.

@leepowelldev
Copy link

leepowelldev commented Oct 17, 2019

By the way, I'm using refresh from my wordpress theme like so:

function refresh_gatsby() {
  $http = new WP_Http();
  $http->post('http://host.docker.internal:8002/__refresh');
}
add_action('wp_insert_post', 'refresh_gatsby');

@leepowelldev
Copy link

After some investigation last night it seems that Gatsby (maybe this is the responsibility of gatsby-source-wordpress) quite happily adds new nodes upon refresh but isn't removing nodes that are not returned from the refresh of source data. I've managed to get this working as a POC for now by utilising the normalizer callback in gatsby-config.js:

normalizer: function({ entities, deleteNode, getNode, getNodes }) {
  const currentNodeIds = getNodes()
    .filter(node => node.internal.owner === 'gatsby-source-wordpress')
    .map(entity => entity.id);

  const nextNodeIds = entities.map(entity => entity.id);

  const deletedNodeIds = currentNodeIds.filter(
    x => !nextNodeIds.includes(x)
  );

  deletedNodeIds.forEach(id => {
    const node = getNode(id);
    deleteNode({ node });
  });

  return entities;
},

I've only tested this with removing pages/posts from Wordpress - so not sure how it'd adapt to other node types - but I'm thinking that shouldn't Gatsby be doing this internally?

@gatsbot gatsbot bot added the stale? Issue that may be closed soon due to the original author not responding any more. label Nov 8, 2019
@gatsbot
Copy link

gatsbot bot commented Nov 8, 2019

Hiya!

This issue has gone quiet. Spooky quiet. 👻

We get a lot of issues, so we currently close issues after 30 days of inactivity. It’s been at least 20 days since the last update here.

If we missed this issue or if you want to keep it open, please reply here. You can also add the label "not stale" to keep this issue open!

As a friendly reminder: the best way to see this issue, or any other, fixed is to open a Pull Request. Check out gatsby.dev/contribute for more information about opening PRs, triaging issues, and contributing!

Thanks for being a part of the Gatsby community! 💪💜

@github-actions
Copy link

Hiya!

This issue has gone quiet. Spooky quiet. 👻

We get a lot of issues, so we currently close issues after 30 days of inactivity. It’s been at least 20 days since the last update here.
If we missed this issue or if you want to keep it open, please reply here. You can also add the label "not stale" to keep this issue open!
As a friendly reminder: the best way to see this issue, or any other, fixed is to open a Pull Request. Check out gatsby.dev/contribute for more information about opening PRs, triaging issues, and contributing!

Thanks for being a part of the Gatsby community! 💪💜

@github-actions github-actions bot added the stale? Issue that may be closed soon due to the original author not responding any more. label Dec 23, 2019
@github-actions
Copy link

github-actions bot commented Jan 2, 2020

Hey again!

It’s been 30 days since anything happened on this issue, so our friendly neighborhood robot (that’s me!) is going to close it.
Please keep in mind that I’m only a robot, so if I’ve closed this issue in error, I’m HUMAN_EMOTION_SORRY. Please feel free to reopen this issue or create a new one if you need anything else.
As a friendly reminder: the best way to see this issue, or any other, fixed is to open a Pull Request. Check out gatsby.dev/contribute for more information about opening PRs, triaging issues, and contributing!

Thanks again for being part of the Gatsby community! 💪💜

@github-actions github-actions bot closed this as completed Jan 2, 2020
@sriinnu
Copy link

sriinnu commented Feb 7, 2020

This is what I have in my functions.php file. When a page or post or a menu is updated, there is a refresh.


add_action('save_post', 'reload_gatsby', 10, 11);
function reload_gatsby($post_ID, $post)
{
	// $post_id = $post["post_ID"];
	if (!(wp_is_post_revision($post) || wp_is_post_autosave($post))) {
		$curl_refresh_gatsby = curl_init('http://localhost:8000/__refresh');
		curl_setopt($curl_refresh_gatsby, CURLOPT_POST, true);
		$response_2 = curl_exec($curl_refresh_gatsby);

	}
}

@Tasemu
Copy link

Tasemu commented Feb 14, 2020

This is what I have in my functions.php file. When a page or post or a menu is updated, there is a refresh.


add_action('save_post', 'reload_gatsby', 10, 11);
function reload_gatsby($post_ID, $post)
{
	// $post_id = $post["post_ID"];
	if (!(wp_is_post_revision($post) || wp_is_post_autosave($post))) {
		$curl_refresh_gatsby = curl_init('http://localhost:8000/__refresh');
		curl_setopt($curl_refresh_gatsby, CURLOPT_POST, true);
		$response_2 = curl_exec($curl_refresh_gatsby);

	}
}

This is perfect, however what would you do in production when you want the gatsby front-end to rebuild on wordpress changes? I've not found an answer anywhere for this. :(

@nelsonmiew
Copy link
Author

Well, I have hosted my project in Netlify. It provides a link to rebuild the entire website. You can use the code from @Tasemu (not tested yet!)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale? Issue that may be closed soon due to the original author not responding any more. status: awaiting author response Additional information has been requested from the author topic: source-wordpress Related to Gatsby's integration with WordPress type: question or discussion Issue discussing or asking a question about Gatsby
Projects
None yet
8 participants