Skip to content

docs(functions): avoid CORS preflight with hosting #2104

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 8, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 46 additions & 1 deletion docs/functions/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,49 @@ import { AngularFireFunctionsModule, FUNCTIONS_ORIGIN } from '@angular/fire/func
})
export class AppModule {}

```
```

### 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 {}

```