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(firebase): add support for 2nd generation functions #1500

Merged
merged 48 commits into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
1bb3e2b
Add the preset
JamieCurnow Apr 14, 2023
e310920
update the docs
JamieCurnow Apr 14, 2023
7a30e30
updated ts-ignore to ts-expect-error with comment.
JamieCurnow Apr 18, 2023
12fd06e
Adding the firebase v2 options to the PresetOptions interface
JamieCurnow May 3, 2023
040e224
Documentation for the firebaseV2 options as part of nitro config
JamieCurnow May 3, 2023
4911e25
clean up hanging comment
JamieCurnow Jun 30, 2023
2e1789d
update firebase v2 link from docs
JamieCurnow Jul 2, 2023
11597d3
update httpRequestOptions link in docs
JamieCurnow Jul 2, 2023
7e38800
change firebase import to specify that it's a type
JamieCurnow Jul 2, 2023
d4a5f49
move firebase functions dep to dev deps
JamieCurnow Jul 2, 2023
6e865e3
rename preset to firebase-v2
JamieCurnow Jul 2, 2023
35e0701
chore: move firebase functions to deps
Hebilicious Jul 2, 2023
6980498
add warning to update firebase tools cli when using firebase-v2
JamieCurnow Jul 3, 2023
3ebaf0c
updates to the firebase docs to be explicit about installing firebase…
JamieCurnow Jul 3, 2023
c4aeedb
fix firebase v2 http options passing through from nitro config
JamieCurnow Jul 3, 2023
e2eb1e2
tidy docs
JamieCurnow Jul 3, 2023
db04aff
Add message about non-public functions
JamieCurnow Jul 3, 2023
3f9bf1b
Change docs to recommend using npx firebase-tools
JamieCurnow Jul 4, 2023
0c612af
nest httpRequestOptions under the v2 key in the firebase preset options
JamieCurnow Jul 4, 2023
9af22bf
Update docs/content/2.deploy/providers/firebase.md
Hebilicious Jul 5, 2023
547056f
update nitro entry build command to be more correct
JamieCurnow Jul 6, 2023
fd7e592
Merge main and pnpm install to fix lock file
JamieCurnow Jul 12, 2023
6b4b00a
import only the onRequest function for firebase v2 entry
JamieCurnow Jul 12, 2023
75bceb4
refactor: use env variable to specify the functions version
posva Jul 28, 2023
a9f8ca3
docs: adapt docs
posva Jul 28, 2023
c4610e6
Update docs/content/2.deploy/providers/firebase.md
posva Jul 28, 2023
22261b2
docs: add nuxt version too
posva Jul 28, 2023
aa4cbf4
docs: simplify
posva Jul 28, 2023
41e7c43
Feat/firebase gen two single config (#1)
JamieCurnow Jul 31, 2023
0712e9a
feat(firebase): warn on missing gen version
posva Jul 31, 2023
58b164e
refactor: hierarchical naming
posva Jul 31, 2023
7a590a6
chore: apply automated lint fixes
autofix-ci[bot] Jul 31, 2023
b70180b
docs: updates
posva Jul 31, 2023
79e3071
Update src/types/presets.ts
pi0 Jul 31, 2023
328dab3
Update src/presets/firebase.ts
pi0 Jul 31, 2023
9ede5b1
chore: review
posva Aug 1, 2023
8d50422
feat(firebase): detect if the user is passing a gen to warn them to u…
posva Aug 1, 2023
8fba168
Merge branch 'main' into feat/support-firebase-gen-two
posva Aug 1, 2023
6fe9496
chore: apply automated lint fixes
autofix-ci[bot] Aug 1, 2023
c54760a
Merge branch 'main' into feat/support-firebase-gen-two
posva Aug 7, 2023
ae6a188
style: lint
posva Aug 7, 2023
3cdd864
chore: apply automated lint fixes
autofix-ci[bot] Aug 7, 2023
1a7cc34
Merge branch 'main' into feat/support-firebase-gen-two
pi0 Aug 7, 2023
849fc05
move firebase-functions to dev dependencies
pi0 Aug 7, 2023
13f6061
small updates
pi0 Aug 7, 2023
f4f738c
Merge branch 'main' into pr/posva/1500
pi0 Aug 7, 2023
cfcd2f5
fix build
pi0 Aug 7, 2023
a66ded5
update docs
pi0 Aug 7, 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,6 @@ Temporary Items
.vercel
staticwebapp.config.json
.eslintcache
playground/firebase.json

test/fixture/functions
10 changes: 1 addition & 9 deletions build.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,5 @@ export default defineBuildConfig({
"src/cli/cli",
{ input: "src/runtime/", outDir: "dist/runtime", format: "esm" },
],
dependencies: [
"@cloudflare/kv-asset-handler",
"@netlify/functions",
"@nuxt/devalue",
"destr",
"ofetch",
"ora",
],
externals: ["@nuxt/schema"],
externals: ["@nuxt/schema", "firebase-functions"],
});
220 changes: 187 additions & 33 deletions docs/content/2.deploy/providers/firebase.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,47 +6,35 @@ Deploy Nitro apps to Firebase.

Nitro supports [Firebase Hosting](https://firebase.google.com/docs/hosting) with Cloud Functions out of the box.

::alert{type=""}
**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.
::alert
**Note**: This preset will deploy to firebase functions 1st gen by default. If you want to deploy to firebase functions 2nd gen, see the [instructions below](#using-2nd-generation-firebase-functions).
pi0 marked this conversation as resolved.
Show resolved Hide resolved
::

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>`):
::alert{type="warning"}
**Note:** Firebase preset is under development. Consider using [Edge Release Channel](/guide/getting-started#edge-release-channel) in order to use latest updates and gen 2.
::

```json [.firebaserc]
{
"projects": {
"default": "<your_project_id>"
}
}
```

Then, just add Firebase dependencies to your project:

```bash
# yarn
yarn add --dev firebase-admin firebase-functions firebase-functions-test
## Project Setup

# npm
npm install -D firebase-admin firebase-functions firebase-functions-test
```
### Using Firebase CLI (recommended)

### 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 (for hosting only). [Learn about installing the firebase CLI](https://firebase.google.com/docs/cli#windows-npm).

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.
#### 1. Install Firebase CLI Globally

#### Install Firebase CLI globally
Always try to use the latest version of the Firebase CLI.

```bash
# yarn
yarn global add firebase-tools

# npm
npm install -g firebase-tools
npm install -g firebase-tools@latest
```

**Note**: You need to be on [^11.18.0](https://github.com/firebase/firebase-tools/releases/tag/v11.18.0) to deploy a nodejs18 function.
**Note**: You need to be on [^11.18.0](https://github.com/firebase/firebase-tools/releases/tag/v11.18.0) to deploy a `nodejs18` function.

#### Initialize your Firebase project
#### 2. Initialize your Firebase Project

```bash
firebase login
Expand All @@ -73,20 +61,186 @@ Once complete, add the following to your `firebase.json` to enable server render

You can find more details in the [Firebase documentation](https://firebase.google.com/docs/hosting/quickstart).

## Local preview
### Alternative Method

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 the git.

#### 1. Create a `.firebaserc` file

It is 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]
{
"projects": {
"default": "<your_project_id>"
}
}
```

This file is usually generated when you initialize your project with the Firebase CLI. But if you don't have one, you can create it manually.

#### 2. Install Firebase Dependencies

Then, add Firebase dependencies to your project:

::code-group
```bash [npm]
npm install -D firebase-admin firebase-functions firebase-functions-test
```
```bash [yarn]
yarn add --dev firebase-admin firebase-functions firebase-functions-test
```
```bash [pnpm]
pnpm install -D firebase-admin firebase-functions firebase-functions-test
```
::

#### 3. 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
```


## Local Preview

You can preview a local version of your site if you need to test things out without deploying.

```bash
NITRO_PRESET=firebase yarn build
NITRO_PRESET=firebase npm run build
pi0 marked this conversation as resolved.
Show resolved Hide resolved
firebase emulators:start
```

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

Deploy to Firebase Hosting by running a Nitro build and then running the `firebase deploy` command.

```bash
NITRO_PRESET=firebase npm run build
```

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

Deploying to Firebase Hosting is a simple matter of just running the `firebase deploy` command.
If you installed the Firebase CLI globally, you can also run:

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

## Using 2nd Generation Firebase Functions

- [Comparison between 1st and 2nd generation functions](https://firebase.google.com/docs/functions/version-comparison)

To switch to the more recent and, recommended generation of firebase functions, set the `firebase.gen` option to `2`:

::code-group
```ts{3} [nitro.config.ts]
export default defineNitroConfig({
firebase: {
gen: 2
// ...
}
})
```
```ts{4} [nuxt.config.ts]
export default defineNuxtConfig({
nitro: {
firebase: {
gen: 2
// ...
}
}
})
```
::

::alert
If you cannot use configuration for any reason, alternatively you can use `NITRO_FIREBASE_GEN` environment variable.
::

If you already have a deployed version of your website and want to upgrade to 2nd gen, [see the Migration process on Firebase docs](https://firebase.google.com/docs/functions/2nd-gen-upgrade). Namely, the CLI will ask you to delete your existing functions before deploying the new ones.

## Options

You can set options for the firebase functions in your `nitro.config.ts` file:

::code-group
```ts [nitro.config.ts]
export default defineNitroConfig({
firebase: {
gen: 2,
httpOptions: {
region: 'europe-west1',
maxInstances: 3,
},
},
});
```

```ts [nuxt.config.ts]
export default defineNuxtConfig({
nitro: {
firebase: {
gen: 2,
httpOptions: {
region: 'europe-west1',
maxInstances: 3,
},
},
},
});
```
::

You can also set options for 1st generation Cloud Functions if the `gen` option is set to `1`. Note these are different from the options for 2nd generation Cloud Functions.

### Runtime Node.js Version

You can set custom Node.js version in configuration:

::code-group
```ts [nitro.config.ts]
export default defineNitroConfig({
firebase: {
nodeVersion: 18 // Can be 16 or 18 or 20
},
});
```

```ts [nuxt.config.ts]
export default defineNuxtConfig({
nitro: {
firebase: {
nodeVersion: 18 // Can be 16 or 18 or 20
},
},
});
```
::

Firebase tools use the `engines.node` version in `package.json` to determine which node version to use for your functions. Nitro automatically writes to the `.output/server/package.json` with configured Node.js version.

You might also need to add a runtime key to your `firebase.json` file:

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

You can read more about this in [Firebase Docs](https://firebase.google.com/docs/functions/manage-functions?gen=2nd#set_nodejs_version).

## 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
```
17 changes: 15 additions & 2 deletions docs/content/3.config.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,11 @@ It's possible to set Cache-Control headers for assets using the `maxAge` option:
},
],
```

The config above generates the following header in the assets under `public/images/` folder:

`cache-control: public, max-age=604800, immutable`



### `compressPublicAssets`

- Default: `{ gzip: false, brotli: false }`{lang=ts}
Expand Down Expand Up @@ -464,3 +463,17 @@ Rollup specific option.
### `commonJS`

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

## Preset options

### `firebase`

The options for the firebase functions preset. See [Preset Docs](/deploy/providers/firebase#options)

### `vercel`

The options for the vercel preset. See [Preset Docs](/deploy/providers/vercel)

### `cloudflare`

The options for the cloudflare preset. See [Preset Docs](/deploy/providers/cloudflare)
Loading