Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a catch all redirect & a child function redirect

My domain seems to redirect to the index.html and all the sub-links just fine.
Problem is it won't redirect to the api rule "source": "/api/**"
I've checked the API manually using the whole firebase url (no custom domain) and it works.

How can I get both domain.com/somelink and domain.com/api/somelink to work in union?

Here's the configuration.

firebase.json

{
  "database": {
    "rules": "database.rules.json"
  },
  "hosting": {
    "public": "public",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ],
    "rewrites": [
      {
        "source": "/api/**",
        "function": "api"
      },
      {
        "source": "**",
        "destination": "/index.html"
      }
    ]
  },
  "storage": {
    "rules": "storage.rules"
  }
}
like image 906
Oliver Dixon Avatar asked Nov 08 '22 06:11

Oliver Dixon


1 Answers

As noted here https://stackoverflow.com/questions/44959652/firebase-hosting-with-dynamic-cloud-functions-rewrites/45224176#45224176

Create a main app which hosts all other top-level functions.

const funtions = require('firebase-functions');
const express = require('express');
const app = express();

app.get('/users/:userId/:userData/json', (req, res) => {
    // Do App stuff here
}
// A couple more app.get in the same format

// Create "main" function to host all other top-level functions
const main = express();
main.use('/api', app);

exports.main = functions.https.onRequest(main);

firebase.json

{
    "source": "/api/**", // "**" ensures we include paths such as "/api/users/:userId"
    "function": "main"
}

Use the main hook.

const hooks = express();
main.use('/hooks/, hooks);
like image 118
Oliver Dixon Avatar answered Nov 15 '22 07:11

Oliver Dixon