Gatsby: Would like to know if I can get PHP processing on Gatsby files to inject data

Created on 21 Aug 2018  路  5Comments  路  Source: gatsbyjs/gatsby

Summary

I'm pretty new to web development am rebuilding our company website and looking for performance, hence why I'm looking into Gatsby (And react-static). In our nav bar we have a shopping cart icon that needs to display the # of items in the cart and then drop down with an item list.

In order to avoid having to make another backend rest/ajax request to get cart info we were wanting to have PHP (Can't change the backend) process the files to be served and inject some Javascript objects into the resulting HTML.

My assumption is that because Gatsby turns all the files into who knows what we won't be able to do this.
1) Is this assumption correct?
2) If not what would be considered the best practice in this case? Do I have no choice but to resort to another round trip for cart data or is there some other library/system out there that I'm totally unaware of that would help?

Thanks

stale? question or discussion

Most helpful comment

No other way.

There are build-time and runtime. During the build, you can get all the data, but it's an expensive process (ideally do it once for production), unlike using a templating engine (which can be done on each request). But even with a templating engine, you'd use ajax to keep application state in sync with the backend to avoid unnecessary page reloads.

So the only choice is to query from the client-side. Graphql could help here, making requests smaller.

All 5 comments

No other way.

There are build-time and runtime. During the build, you can get all the data, but it's an expensive process (ideally do it once for production), unlike using a templating engine (which can be done on each request). But even with a templating engine, you'd use ajax to keep application state in sync with the backend to avoid unnecessary page reloads.

So the only choice is to query from the client-side. Graphql could help here, making requests smaller.

Thanks, I was afraid that was going to be the answer.

The more I look into some of these SSR/SSG frameworks, the more it seems that they are less useful than they look for a lot of situations. If a site is mainly static content anyway (which a lot are) with the odd repetition (For instance, a nav bar on all pages) is seems that just raw HTML would likely be the fastest way to serve the data at the cost of a little cut and paste if you modified the repetitive content like the nav bar. Then in the case of raw HTML I can make it serve via PHP pretty easily.

I wish someone had done some benchmarks of SSR frameworks via raw HTML.

If I understand you right, you are looking to avoid making an HTTP request for data to show the items in the cart?

Could you store it in local storage on the browser whenever they update the cart?

Well I just wanted in general to avoid making another request for data from the server and instead, have it injected when the initial request came through for the site. For instance if I had a index.php them obviously I can inject any data into that page when it's served. I can get IIS to push index.html through PHP but for php to do anything with that I'll need to inject some tags. I was just after a guaranteed place I could do this. I'm hoping I can use Helmet to inject some php into the head and that will end up in the generated index.html. That should work I think

Old issues will be closed after 30 days of inactivity. This issue has been quiet for 20 days and is being marked as stale. Reply here or add the label "not stale" to keep this issue open!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

3CordGuy picture 3CordGuy  路  3Comments

dustinhorton picture dustinhorton  路  3Comments

totsteps picture totsteps  路  3Comments

dustinhorton picture dustinhorton  路  3Comments

rossPatton picture rossPatton  路  3Comments