Skip to content
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

feat: add firebase functions v2 preset #1142

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
b2c1fde
Add the preset
JamieCurnow Apr 14, 2023
baabcd2
update the docs
JamieCurnow Apr 14, 2023
72465c7
Merge branch 'main' into feat/firebase-functions-v2
JamieCurnow Apr 15, 2023
e4ca149
updated ts-ignore to ts-expect-error with comment.
JamieCurnow Apr 18, 2023
9679aa1
Merge branch 'main' into feat/firebase-functions-v2
JamieCurnow Apr 18, 2023
a61a507
Merge branch 'main' into feat/firebase-functions-v2
JamieCurnow Apr 18, 2023
b97e832
Merge branch 'main' into feat/firebase-functions-v2
JamieCurnow May 3, 2023
f8ed16e
Adding the firebase v2 options to the PresetOptions interface
JamieCurnow May 3, 2023
cbbc21d
Documentation for the firebaseV2 options as part of nitro config
JamieCurnow May 3, 2023
0a929c4
Merge branch 'main' into feat/firebase-functions-v2 and fix pnpm lock…
JamieCurnow May 6, 2023
07a4d4b
Merge branch 'main' into feat/firebase-functions-v2
JamieCurnow Jun 30, 2023
7266ac1
re-sync pnpm-lock with main
JamieCurnow Jun 30, 2023
9d1ff24
fresh pnpm install to get firebase-admin in
JamieCurnow Jun 30, 2023
da30242
clean up hanging comment
JamieCurnow Jun 30, 2023
ca0e1ed
Merge branch 'main' into feat/firebase-functions-v2
JamieCurnow Jun 30, 2023
52fbf62
update firebase v2 link from docs
JamieCurnow Jul 2, 2023
dd2d2a1
update httpRequestOptions link in docs
JamieCurnow Jul 2, 2023
a4e703e
change firebase import to specify that it's a type
JamieCurnow Jul 2, 2023
7990f8f
move firebase functions dep to dev deps
JamieCurnow Jul 2, 2023
7163338
rename preset to firebase-v2
JamieCurnow Jul 2, 2023
41fbaa5
chore: move firebase functions to deps
Hebilicious Jul 2, 2023
5746f46
add warning to update firebase tools cli when using firebase-v2
JamieCurnow Jul 3, 2023
01f3e4e
updates to the firebase docs to be explicit about installing firebase…
JamieCurnow Jul 3, 2023
5f53f9f
Merge branch 'main' into feat/firebase-functions-v2
JamieCurnow Jul 3, 2023
bd29d15
fix firebase v2 http options passing through from nitro config
JamieCurnow Jul 3, 2023
37b400d
tidy docs
JamieCurnow Jul 3, 2023
6711bb9
Add message about non-public functions
JamieCurnow Jul 3, 2023
424fe2d
Change docs to recommend using npx firebase-tools
JamieCurnow Jul 4, 2023
d4ea814
Merge branch 'main' into feat/firebase-functions-v2
JamieCurnow Jul 4, 2023
efc5bd0
nest httpRequestOptions under the v2 key in the firebase preset options
JamieCurnow Jul 4, 2023
112caa4
Update docs/content/2.deploy/providers/firebase.md
Hebilicious Jul 5, 2023
8797fbc
update nitro entry build command to be more correct
JamieCurnow Jul 6, 2023
881d932
Merge branch 'main' into feat/firebase-functions-v2
JamieCurnow Jul 6, 2023
9bf2f3f
Merge branch 'main' into feat/firebase-functions-v2
JamieCurnow Jul 6, 2023
ac61871
Merge main and pnpm install to fix lock file
JamieCurnow Jul 12, 2023
937c6df
import only the onRequest function for firebase v2 entry
JamieCurnow Jul 12, 2023
4c4a81b
Merge main and pnpm install to update lock file
JamieCurnow Jul 12, 2023
a22b4f1
Merge branch 'main' into feat/firebase-functions-v2
JamieCurnow Jul 13, 2023
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
127 changes: 116 additions & 11 deletions docs/content/2.deploy/providers/firebase.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@ Nitro supports [Firebase Hosting](https://firebase.google.com/docs/hosting) with

**Note**: You need to be on the **Blaze plan** to use Nitro with Cloud Functions.

If you don't already have a `firebase.json` in your root directory, Nitro will create one the first time you run it. In this file, you will need to replace `<your_project_id>` with the ID of your Firebase project.
**Note**: This preset will deploy to firebase functions v1. If you want to deploy to firebase functions v2, please use the [`firebase-v2`](#firebase-functions-v2) preset.

This file should then be committed to version control. You can also create a `.firebaserc` file if you don't want to manually pass your project ID to your `firebase` commands (with `--project <your_project_id>`):
### Setup Using Nitro

If you don't already have a `firebase.json` in your root directory, Nitro will create one the first time you run it. In this file, you will need to replace `<your_project_id>` with the ID of your Firebase project. This file should then be committed to version control.

#### Create a .firebaserc file

It is also recommended to create a `.firebaserc` file so you don't need to manually pass your project ID to your `firebase` commands (with `--project <your_project_id>`):

```json [.firebaserc]
{
Expand All @@ -20,19 +26,35 @@ This file should then be committed to version control. You can also create a `.f
}
```

Then, just add Firebase dependencies to your project:
#### Install Firebase dependencies

Then, add Firebase dependencies to your project:

yarn:

```bash
# yarn
yarn add --dev firebase-admin firebase-functions firebase-functions-test
yarn add --dev firebase-admin firebase-functions firebase-functions-test firebase-tools
```

# npm
npm install -D firebase-admin firebase-functions firebase-functions-test
npm:

```bash
npm install -D firebase-admin firebase-functions firebase-functions-test firebase-tools
```

#### Log into the firebase cli

Make sure you are authenticated with the firebase cli. Run this command and follow the prompts:

```bash
npx firebase-tools login
```

### Using Firebase CLI

You may instead prefer to set up your project with the Firebase CLI, which will fetch your project ID for you, add required dependencies (see above) and even set up automated deployments via GitHub Actions.

### Alternative Setup Using Firebase CLI

You may instead prefer to set up your project with the Firebase CLI, which will fetch your project ID for you, add required dependencies (see above) and even set up automated deployments via GitHub Actions. [Learn about installing the firebase CLI](https://firebase.google.com/docs/cli#windows-npm).

#### Install Firebase CLI globally

Expand Down Expand Up @@ -82,11 +104,94 @@ NITRO_PRESET=firebase yarn build
firebase emulators:start
```

## Deploy to Firebase Hosting via CLI
## Build and Deploy

Deploying to Firebase Hosting is a simple matter of just running the `firebase deploy` command.
Deploy to Firebase Hosting by running a nitro build and then running the `firebase deploy` command.

```bash
NITRO_PRESET=firebase yarn build
```

```bash
npx firebase-tools deploy
```

If you chose the alternative setup using the Firebase CLI, you can also run:

```bash
firebase deploy
```

## Firebase Functions v2

**Preset:** `firebase-v2` ([switch to this preset](/deploy/#changing-the-deployment-preset))

This preset will deploy to [firebase functions V2](https://firebase.google.com/docs/functions/version-comparison) - the 2nd generation of cloud functions.

It works the same as the [`firebase`](#firebase) preset above, but uses the V2 functions runtime.

### Setup

Follow the instructions for the `firebase` preset above for setup and deploy.

::alert{type="warning"}
New HTTP and HTTP callable functions deployed with any Firebase CLI lower than version 7.7.0 are private by default and throw HTTP 403 errors when invoked. Either explicitly [make these functions public](https://cloud.google.com/functions/docs/securing/managing-access-iam#allowing_unauthenticated_http_function_invocation) or [update your Firebase CLI](https://firebase.google.com/docs/cli#setup_update_cli) before you deploy. If you deploy the function and it is not publicly accessible, then you will need to visit the [Functions Console](https://console.cloud.google.com/functions/list) on Google cloud platform, click your function name, select the 'Permissions' tab and follow the instructions on how to make the function public.
::

### Options

The `firebase-v2` preset also supports passing options to the firebase function via the nitro config. These options are defined in the nitro config object under `firebase.v2.httpRequestOptions`.

The `httpRequestOptions` object is passed to `functions.https.onRequest()` function as the first argument as per the [firebase docs](https://firebase.google.com/docs/functions/http-events?gen=2nd#trigger_a_function_with_an_http_request).

For example, to set the region and max instances of the function, you can add the following to your `nitro.config.ts`:

```ts
import { defineNitroConfig } from 'nitropack/config'

export default defineNitroConfig({
// Nitro options...
firebase: {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better to name this something different to keep the firebase config option for the firebase preset only. It will avoid any possible conflicts.

Copy link
Contributor Author

@JamieCurnow JamieCurnow Jul 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll let @pi0 weigh in on this, as we've already been through a review on it. See the resolved issue here @posva:
#1142 (review)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just my 2 cents, but for #1377 my idea was to have the config option shared between v1 and v2 functions, so you could seamlessly switch between them without worrying about the config. As someone using this in production, it would be nice to switch between v1 and v2 without worrying about accidentally overwriting function deployments.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AlexLavoie42 Why would you switch between 1st and 2nd gen in production? Normally one would upgrade to gen 2 because they are better but they would never switch back. Even more when talking about the function that handles SSR. Or are you talking about upgrading too? In that case, I also think adding the same options where possible would make sense

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, the vast majority of the time it will be just upgrading from v1 to v2, although I could see some circumstances where we would downgrade to v1 temporarily for debugging/testing purposes.

The main purpose behind sharing the config would be to simply upgrade to v2 without changing config. I have not actually tested this, but I assume publishing a v1 and v2 function with the same name will overwrite each other? If the config is separated there is definitely a possibility that when upgrading users will forget to move the config over and overwrite the default server function.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you detail those circumstances? I don't think I see any reason not to upgrade the currently named server function that renders the Nuxt app to gen 2 and then rollback to gen 1 πŸ€”

Publishing a gen 2 function with the same name will error, you have to name it differently

The main purpose behind sharing the config would be to simply upgrade to v2 without changing config.

Totally πŸ™Œ

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Publishing a gen 2 function with the same name will error, you have to name it differently

This is good to know, so in that case you would not be able upgrade to v2 with the same function name unless you delete the original first? Perhaps in that case it should actually be a different config value to avoid an error. Maybe even the default name should be different as well then?

As for downgrading to v1, I am imagining a case where we downgrade temporarily either to debug or rollback in case of any bugs that arise after upgrading. Granted, I don't think this scenario is very likely.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO the name should be different yes.

so in that case you would not be able upgrade to v2 with the same function name unless you delete the original first?

Not exactly: upgrading is a bit different as you want to go through a one-time step where you have both gen functions co-existing. In more complex scenarios: https://firebase.google.com/docs/functions/2nd-gen-upgrade.
You could also just redeploy the second gen with a different name and remove the 1st one in one deployment. I'm not completely sure but I imagine you risk losing a fraction of traffic during that deployment, which in some projects is fine, in others isn't.

v2: {
httpRequestOptions: {
region: "europe-west1",
maxInstances: 3,
// etc.
}
}
}
})
```

The documentation for the `httpRequestOptions` option can be found in the [firebase functions 2nd gen docs](https://firebase.google.com/docs/reference/functions/2nd-gen/node/firebase-functions.https.httpsoptions).

### Runtime

Firebase uses the engines.node version in your `package.json` to determine which node version to use for your functions. If you want to use a different version of node for your functions, you should set the `engines.node` version in your `package.json` to the version you want to use:

```json [package.json]
{
"engines": {
"node": "18"
}
}
```

For some node versions you will also need to add a runtime key to your firebase.json file:

```json [firebase.json]
{
"functions": {
"source": ".output/server",
"runtime": "nodejs18"
}
}
```

## If your firebase project has other cloud functions

You may be warned that other cloud functions will be deleted when you deploy your nitro project. This is because nitro will deploy your entire project to firebase functions. If you want to deploy only your nitro project, you can use the `--only` flag:

```bash
firebase deploy --only functions:server,hosting
```
6 changes: 6 additions & 0 deletions docs/content/3.config.md
Original file line number Diff line number Diff line change
Expand Up @@ -469,3 +469,9 @@ Rollup specific option.
### `commonJS`

Rollup specific option. Specifies additional configuration for the rollup CommonJS plugin.

## Preset options

### `firebase`

The options for the firebase functions v2 preset. See [Preset](/deploy/providers/firebase#firebase-functions-v2)
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
"esbuild": "^0.18.11",
"escape-string-regexp": "^5.0.0",
"etag": "^1.8.1",
"firebase-functions": "^4.4.1",
"fs-extra": "^11.1.1",
"globby": "^13.2.2",
"gzip-size": "^7.0.0",
Expand Down Expand Up @@ -152,4 +153,4 @@
]
}
}
}
}
Loading