Expose a REST API from a Gatsby site.
An example is when I want to query my site for some data to be used in another site. My use case is that I create several sites and I want to fetch some data from one site to another site. This could be done of course by setting up a separate REST API but it would be easier to just get the data from the Gatsby site.
What I am currently planning on doing is to generate some pages with JSON data on them in a separate div with a unique id. I then need to do some REST GET to that page and then parse the data programmatically. What I would really want to do is to just read the body of the response but then I get all the Gatsby stuff such as renderings from html.js and I also need to take into account the layouts and such.
Jenkins has a similar REST API feature.
It would be easier for me with this feature to to a REST GET call to my Gatsby site to fetch some data I would be interested in.
Thank you for opening this @magnusarinell
Gatsby currently doesn't serve up any API during runtime since it compiles your site down to static assets. Based on your motivation and if I understand correctly, would it not be easier to separate out this _common_ data that you'd like to access in the two websites and build an API that serves it?
Then both Gatsby and the other site can consume this.
Parsing a response from a route and getting data from a specific div with a id in it sounds like scraping data and I think building a API that simply returns the data will much be simpler than scraping a Gatsby page.
You can also hook into onPostBuild in gatsby-node and output "body" to json files in your public directory (with normal node stuff like fs.writeFile). But I feel like in the end it would be nicer to do it with @sidharthachatterjee approach.
@pieh Thanks, that is probably a feasible approach. The thing is that I have the data I want to expose readily baked in my Redux store. Will try your suggestion out and see if I can write it to a file.
@sidharthachatterjee creating a separate API requires some additional setup. Also, since I'm running all those Gatsby sites on a common Docker swarm I differentiate them by their path prefix and with a REST API in Gatsby I get a convenient url to use when one site wants to get data from another (using a relative path).
With my suggestion one could say that Gatsby becomes not only a static site generator but also a static API generator, which might yield alot of interesting use cases to it.
Maybe https://github.com/gatsbyjs/gatsby/issues/10230 could help
@sidharthachatterjee #10230 is certainly very flexible and powerful, although the suggestion is that the API would be started running gatsby api. That would create some additional scaffolding to get running and it also would come with the drawback that the site and the API would be accessed on different path prefixes.
I solved my particular issue with inspiration from the suggestion by @pieh, although I generate the json files in createPages to get them during develop aswell. This setup works well for me.
The ideal use case for me would be to perhaps have a special purpose folder, api, that would behave similarly to the pages folder but instead create json files. Then I could write readily manipulated data from my Redux store. With the current setup I need to do an extra graphql query and data transformation in separate code.
This is very interesting, I have a similar use case - I want to expose the data for a given component to an entirely different site that doesn't use the Gatsby stack - but benefits from the Gatsby build.
In my case, I want to be able to build a 'staging/preview' site using Gatsby so authors can fairly easily see the results of their edits in the CMS but then have a workflow where another site, on a different stack in production then consumes the same data in a variety of ways.
In other words, I'm hoping to leverage Gatsby into a sort of hub for static (ish) content.
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鈥檚 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!
Thanks for being a part of the Gatsby community! 馃挭馃挏
Hey again!
It鈥檚 been 30 days since anything happened on this issue, so our friendly neighborhood robot (that鈥檚 me!) is going to close it.
Please keep in mind that I鈥檓 only a robot, so if I鈥檝e closed this issue in error, I鈥檓 HUMAN_EMOTION_SORRY. Please feel free to reopen this issue or create a new one if you need anything else.
Thanks again for being part of the Gatsby community!
@magnusarinell can you share some code (or you have a doc/blog about it)?
Most helpful comment
You can also hook into
onPostBuildingatsby-nodeand output "body" to json files in yourpublicdirectory (with normal node stuff likefs.writeFile). But I feel like in the end it would be nicer to do it with @sidharthachatterjee approach.