svelte-adapter-firebase: bug: npm run build error

Describe the Bug

I installed the adapter, added it to my svelte.config.js and initialized firebase with firebase init.

When I run npm run build I get the following error:

> Error: Required "hosting[].rewrites" field not found for matched hosting configuration. Specify your Cloud Function with rewrite rule matching "source":"**"
TypeError: Error: Required "hosting[].rewrites" field not found for matched hosting configuration. Specify your Cloud Function with rewrite rule matching "source":"**"
    at parseFirebaseConfiguration (file:///[project-path]/node_modules/svelte-adapter-firebase/src/utils.js:124:9)
    at adapt (file:///[project-path]/node_modules/svelte-adapter-firebase/src/index.js:26:35)
    at adapt (file:///[project-path]/alohi/node_modules/@sveltejs/kit/dist/chunks/index4.js:377:8)
    at file:///[project-path]/alohi/node_modules/@sveltejs/kit/dist/cli.js:878:11

Steps to Reproduce

  1. Install sveltekit
  2. Install adapter
  3. Create github project
  4. Initialize firebase
  5. run npm run build

Expected Behaviour

I expect the site to build.

svelte-adapter-firebase version

0.13.0

sveltejs/kit version

1.0.0-next.178

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 1
  • Comments: 24 (13 by maintainers)

Most upvoted comments

If I update the hosting object to include rewrites, do you have an example of what that function in <functionName> should look like in functions/index.js?

"hosting": {
        "public": "public",
        "rewrites": [
            {
                "source": "**",
                "function": "<functionName>"
            }
        ],
        "ignore": [
            "firebase.json",
            "**/.*",
            "**/node_modules/**"
        ]
    }

I updated it to

"rewrites": [
            {
                "source": "**",
                "function": "ssrServer"
            }
        ],

and now I’m getting the function in the terminal

Thanks for sharing more. Once you put the <functionName> in the firebase.json file and re-run the build it will output the code to the CLI for you to copy/paste into the functions/index.js

Perhaps I don’t fully understand how the Cloud Function connects to everything. I’ve been using SvelteKit mostly with the static adapter, so this is my first time really leaning into the SSR side. I really want to configure everything correctly and do this in the “sveltekit way” if possible.

My firebase.json after firebase init looks like so:

{
    "functions": {
        "source": "functions",
        "predeploy": [
            "npm --prefix \"$RESOURCE_DIR\" run lint"
        ]
    },
    "firestore": {
        "rules": "firestore.rules",
        "indexes": "firestore.indexes.json"
    },
    "hosting": {
        "public": "public",
        "ignore": [
            "firebase.json",
            "**/.*",
            "**/node_modules/**"
        ]
    }
}

I included firestore, functions, and hosting in the init and i have them all turned on in the firebase app dashboard.

It is difficult to write a guide for this that covers all cases first time for each person as some people already have Firebase apps, some are initializing them for the first time with this. I will tweak the initial setup section because firebase init is not the only step for that step. Ultimately you need a Firebase Hosting config with a Cloud Function matching the default rewrite rule for the adapter which is "**" as the log states.

This is expected behaviour. You need to setup your Firebase project config for an SSR app. The output states this:

Specify your Cloud Function with rewrite rule matching “source”:“**”

You do not have a Cloud Function rewrite rule in your Firebase Hosting config. See the docs for examples: https://github.com/jthegedus/svelte-adapter-firebase#firebasejson-configurations