firebase / firebase-tools

The Firebase Command Line Tools
MIT License
4.01k stars 926 forks source link

Firebase Hosting does not respect Glob pattern #6742

Open AmitMY opened 7 months ago

AmitMY commented 7 months ago

[REQUIRED] Environment info

firebase-tools: 13.1.0

Platform: macOS, Ubuntu

[REQUIRED] Test case

# Step 1: Create a new directory
mkdir my-firebase-project
cd my-firebase-project

# Step 2: Initialize Firebase (assumes Firebase CLI is installed)
firebase init hosting

# During the Firebase init process, you'll need to:
# - Select an existing project or create a new one
# - Configure as a single-page app (rewrite all urls to /index.html): Yes
# - Set up automatic builds and deploys with GitHub: No

# Step 3: Set firebase.json configuration
{
  "hosting": {
    "target": "app",
    "ignore": [],
    "rewrites": [
      {
        "source": "!{/assets/**,/api/**}",
        "destination": "/index.html"
      }
    ]
  }
}

# Step 4: Deploy to Firebase Hosting
firebase deploy --only hosting

[REQUIRED] Steps to reproduce

Available above^

[REQUIRED] Expected behavior

If I set the glob pattern to be: !/assets/** or !{/assets/**,/api/**} it should result in a 404 for /assets/test.json

(Some libraries expect 404 response code to create a fallback behaviour, and a 200 code makes them crash)

[REQUIRED] Actual behavior

If I set the glob pattern to be: !/assets/** it does result in a 404 for /assets/test.json If I set the glob pattern to be: !{/assets/**,/api/**} it does not result in a 404 for /assets/test.json

aalej commented 7 months ago

Hey @AmitMY, thanks for the detailed report and for sharing your firebase.json configuration. I was to reproduce this issue. Let me notify our engineering team about this so they can take a look.

aalej commented 7 months ago

Hey @AmitMY, our team is currently investigating this issue. As an alternative, you can use a RE2-compatible regex pattern in your rewrites. Could you try the pattern below to see if it would match all other patterns except /assets/** and /api/**:

   "rewrites": [
     {
       "regex": "^\/(([^a]|a[^sp]|(as[^s]|ap[^i])|(ass[^e]|api[^\/])|asse[^t]|asset[^s]|assets[^\/])).*$",
       "destination": "/index.html"
     }
AmitMY commented 7 months ago

Thank you for reproducing, and for the alternative solution. Since this kind of pattern is "write only code" and not readable/easily modifiable, I would really advocate for a solution to use actual glob (or regex) patterns over this RE2.

Looking forward to seeing a good solution released soon :) For example, using https://github.com/Anadian/regex-translator to convert the paths from whatever glob pattern to RE2 automatically.

Let me know if I can further help (and where).