Skip to content

Commit c545eea

Browse files
urishjamesdaniels
authored andcommitted
docs(functions): avoid CORS preflight with hosting (#2104)
See #2097 for discussion
1 parent 8e5a995 commit c545eea

File tree

1 file changed

+46
-1
lines changed

1 file changed

+46
-1
lines changed

docs/functions/functions.md

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,49 @@ import { AngularFireFunctionsModule, FUNCTIONS_ORIGIN } from '@angular/fire/func
115115
})
116116
export class AppModule {}
117117

118-
```
118+
```
119+
120+
### Firebase Hosting integration
121+
122+
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).
123+
124+
To set this up, you first need to update your `hosting` section in `firebase.json` and add one `rewrite` rule per function:
125+
126+
```json
127+
"hosting": {
128+
"rewrites": [
129+
{
130+
"source": "/project-name/us-central1/someFunction",
131+
"function": "someFunction"
132+
},
133+
{
134+
"source": "/project-name/us-central1/anotherFunction",
135+
"function": "anotherFunction"
136+
},
137+
...
138+
]
139+
}
140+
```
141+
142+
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.
143+
144+
Next, configure functions origin to point at your app domain:
145+
146+
```ts
147+
import { NgModule } from '@angular/core';
148+
import { AngularFireFunctionsModule, FUNCTIONS_ORIGIN } from '@angular/fire/functions';
149+
150+
@NgModule({
151+
imports: [
152+
...
153+
AngularFireFunctionsModule,
154+
...
155+
],
156+
...
157+
providers: [
158+
{ provide: FUNCTIONS_ORIGIN, useValue: 'https://project-name.web.app' }
159+
]
160+
})
161+
export class AppModule {}
162+
163+
```

0 commit comments

Comments
 (0)