From 81930c268f1496199aabed3f19753991aa4b8512 Mon Sep 17 00:00:00 2001 From: Uri Shaked Date: Sat, 8 Jun 2019 16:26:04 +0300 Subject: [PATCH] docs(functions): avoid CORS preflight with hosting See #2097 for discussion --- docs/functions/functions.md | 47 ++++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/docs/functions/functions.md b/docs/functions/functions.md index 646dd028f..e3a8f0a4a 100644 --- a/docs/functions/functions.md +++ b/docs/functions/functions.md @@ -115,4 +115,49 @@ import { AngularFireFunctionsModule, FUNCTIONS_ORIGIN } from '@angular/fire/func }) export class AppModule {} -``` \ No newline at end of file +``` + +### Firebase Hosting integration + +If you serve your app using [Firebase Hosting](https://firebase.google.com/docs/hosting/), you can configure Functions to be served from the same domain as your app. This will avoid an extra round-trip per function call due to [CORS preflight request](https://developer.mozilla.org/en-US/docs/Glossary/Preflight_request). + +To set this up, you first need to update your `hosting` section in `firebase.json` and add one `rewrite` rule per function: + +```json + "hosting": { + "rewrites": [ + { + "source": "/project-name/us-central1/someFunction", + "function": "someFunction" + }, + { + "source": "/project-name/us-central1/anotherFunction", + "function": "anotherFunction" + }, + ... + ] + } +``` + +Replace `project-name` with your Firebase project id (you can find it by looking at the value of `projectId` field in the Firebase app config). Then deploy your hosting project so that the new settings go into effect. + +Next, configure functions origin to point at your app domain: + +```ts +import { NgModule } from '@angular/core'; +import { AngularFireFunctionsModule, FUNCTIONS_ORIGIN } from '@angular/fire/functions'; + +@NgModule({ + imports: [ + ... + AngularFireFunctionsModule, + ... + ], + ... + providers: [ + { provide: FUNCTIONS_ORIGIN, useValue: 'https://project-name.web.app' } + ] +}) +export class AppModule {} + +```