ctrlplusb / react-universally

A starter kit for universal react applications.
MIT License
1.7k stars 244 forks source link

Using starter with new Firebase cloud functions Hosting #452

Closed jasan-s closed 6 years ago

jasan-s commented 7 years ago

Firebase recently introduced the feature for server side rendering using cloud functions. ANy guide of how to implement this starter with it?

birkir commented 7 years ago

Hey, I just tried this out, its awesome service built by the Firebase team (but darn expensive). What you need to do is actually not start the server and let the cloud functions take care of requests.

I won't go into a lot of details because this concept is something react-universally is not taking into consideration right now. Even though this works in theory there are lots of parts missing like public assets binding, client bundle loading, firebase connections, cache controlling, etc. And still if we solve those problems, it's way to much load for these cloud functions to execute.

1) Replace the following lines with: https://github.com/ctrlplusb/react-universally/blob/master/server/index.js#L51-L57

export default app;

2) Now you can setup up firebase functions and move your build to the functions folder.

firebase init functions
yarn run build
cp -r build functions/build
cp package.json functions/package.json
cd functions
npm install -S firebase-functions
npm install

3) Write your function to use the built server and pipe it to the response.

const functions = require('firebase-functions');
const app = require('./build/server').default;
exports.app = functions.https.onRequest(app);

Do a firebase deploy and now your app cloud function endpoint should render your app.

jasan-s commented 7 years ago

@birkir In terms of pricing and your expensive comment , are you comparing the google cloud functions pricing to AWS Lambda. OR running your own server?

birkir commented 7 years ago

Just comparing to something like heroku, where one can get a 1X dyno for $25 per month.

In GCE you have to pay every time a function is invoked, for every second for every byte of memory allocated, for every CPU second and for every outgoing network byte. Even though the pricing is per GB or 1 cent per thousand something, it quickly adds up.

Think of an application that takes 1G of memory, does a 1G outbound network data per day, serves the request in 1 sec, with about 1M hits per day (~10 req/sec). You can fiddle with their price calculator, i got about $300 a month.

Its totally cool for hobby to small/medium projects, because it can be absolutely free.