Cloud Commerce: the new Store template
-
Start creating new project on Firebase console:
- Set a nice project name (ID) and remember it;
- Disable (recommended) Google Analytics for Firebase, will not be used by default;
-
Go to Creation > Firestore Database page (on sidebar) and Create database:
- Just bypass with default production mode and rules;
- Select region
us-east4
(recommended, or multi-regionnam5 (us-central)
);
-
Go to Creation > Storage page and click Let's start:
- Just bypass with default production mode and same (pre-selected) region;
-
Go to Creation > Authentication and click Get started:
- Click Native providers > Email/password and enable Email link (login without password);
- Go to Authentication configurations and add your store domain(s) to authorized list;
- Optionally, add other provides by Smartphone (SMS, generates additional costs) and/or social media;
-
Go to ⚙️ > Project configurations e edit:
- Default location for GCP resources: Same Firestore region (
us-east4
); - Public settings > Publicly displayed name: Your store name;
- Public settings > Support email (optional);
- Under Your apps create a new Web app:
- Re-enter the store name and enable Firebase Hosting for the new app;
- Copy only the value of the
firebaseConfig
object and replace atfunctions/ssr/src/scripts/InlineScripts.astro
; - Just click to continue in the next steps until confirmation and return to the console;
- Default location for GCP resources: Same Firestore region (
-
Firebase free plan doesn't support sending external HTTP requests, so you'll need to upgrade to Blaze (on demand) plan;
-
Use this template to generate a new repository for your store;
- Setup and first deploy from your terminal with Firebase CLI:
# Install `firebase-tools` and login
npm install -g firebase-tools && firebase login
# Clone your new store repository
git clone [email protected]:{gh-user}/{new-store}.git
cd {new-store}
npm i
With gcloud CLI (optional) installed
# Run project configuration and deploy on GitHub Actions
FIREBASE_PROJECT_ID={project-id} npm run setup
Account key is created automatically with only required permissions using gcloud CLI (skip steps 8 and 9).
Without gcloud CLI
# Run project configuration and first deploy
FIREBASE_PROJECT_ID={project-id} npm run setup -- --no-gcloud
npm run deploy
-
Create a service account for your Firebase project directly on Google Cloud Platform:
- Name it Cloud Commerce GH Actions (YOUR REPOSITORY);
- Describe it A service account with permission to deploy Cloud Commerce from the GitHub repository to Firebase;
- Continue and select the following roles to the service account:
- Firebase Admin
- API Keys Viewer
- Cloud Run Viewer
- Cloud Functions Admin
- Artifact Registry Admin
- App Engine Creator
- App Engine Admin
- Cloud Scheduler Admin
- Service Account User
-
Back in the service accounts list, click the 3 dots (actions) and select Manage keys, generate and download a JSON key for the created account;
- Set the following secrets to your GitHub repository (Settings > Secrets > Actions):
FIREBASE_SERVICE_ACCOUNT
: Paste the generated Google Cloud key JSONECOM_AUTHENTICATION_ID
: Get from CLI setup outputECOM_API_KEY
: Get from CLI setup output
🏁 🏁 🏁 All done, congrats!
Firebase Hosting CDN is fast, but doesn't support cache Stale-While-Revalidate (context and feature request) and Hosting proxy + Cloud Functions (even without cold starts) will never take less than 1s (TTFB will probably take ~2s). We like "instant" responses but want to keep dynamic server rendered views (for less client-side JS), so stale caching is a must and so we need another CDN layer on production (when pointing the custom domain).
-
Recommended way using bunny.net CDN with Perma Cache and Edge Rules for ISR:
- Get your API key from bunny.net dashboard account details;
- Save it with a GitHub repository secret named
BUNNYNET_API_KEY
; - Edit .github/build-and-deploy file (add 1) and commit with message [run:bunny-setup].
-
OR using bunny.net CDN with Stale Cache for SWR:
- Pull zone with your Firebase Hosting https://project.web.app domain as origin URL;
- SSL + Force SSL enabled (prevents http://* redirects to origin domain);
- Smart Cache disabled (cache all MIME types respecting response headers);
- Caching Query String Sort enabled (awesome for image transformations);
- Caching Strip Response Cookies enabled;
- Stale Cache while origin offline and while updating enabled;
- Other origin and caching configurations may be disabled;
- You might want to disable some zones in routing configuration depending on store target.
-
OR using Cloudflare Worker for ISR/SWR:
- SSL full;
- Page rule for */* (any route) with Cache Level: Cache Everything;
- Cache Reserve with Tiered Cache;
- DNS proxied A entry pointing to your Firebase Hosting IP;
- Worker swr script with source (quick edit) copied from
cloud-commerce/packages/ssr/cloudflare/swr-worker.js
.
Note
You may want to remove or edit the default LICENSE file before publishing your store content.