Configure redirects on Firebase Hosting SPA + 2 subfolders firebase.json

泪湿孤枕 提交于 2020-01-17 01:35:07

问题


I have a public folder like:

/public
   index.html

   /landing
     index.html

   /membership
     index.html

/public/index.html is a SPA, so each request to /** should me rewritten to /index.html

/landing/ and /membership/ are two static HTML landings so each request should not be rewritten

firebase.json suggested by Google support:

{
    "functions": {
        "source": "functions"
    },
    "hosting": {
        "public": "public",
        "redirects": [{
                "source": "/landing",
                "destination": "index.html"
            },
            {
                "source": "/membership",
                "destination": "index.html"
            }
        ],
        "rewrites": [{
                "source": "/membership/**",
                "destination": "/membership/index.html"
            }
        ],
        "ignore": [
            "firebase.json",
            "**/.*",
            "**/node_modules/**"
        ]
    }
}

Everything just gets redirected to /index.html...

Even tried with:

{
  "functions": {
      "source": "functions"
  },
  "hosting": {
      "public": "public",
      "redirects": [{
              "source": "/landing/**",
              "destination": "/landing/index.html"
          },
          {
              "source": "/membership/**",
              "destination": "/membership/index.html"
          }
      ],
      "rewrites": [{
              "source": "**",
              "destination": "/index.html"
          }
      ],
      "ignore": [
          "firebase.json",
          "**/.*",
          "**/node_modules/**"
      ]
  }
}

Same result, everything just goes to /


回答1:


You don't have to use "redirects". Juste use these rewrites rules :

{
    "functions": {
        "source": "functions"
    },
    "hosting": {
        "public": "public",
        "ignore": [
            "firebase.json",
            "**/.*",
            "**/node_modules/**"
        ],
        "rewrites": [
            {
                "source": "!/@(landing|membership)/**",
                "destination": "/index.html"
            },
            {
                "source": "/landing/**",
                "destination": "/landing/index.html"
            },
            {
                "source": "/membership/**",
                "destination": "/membership/index.html"
            }
        ]
    }
}

The first rule will rewrite everything except what is starting with landing OR membership. The second rule will take care of the landing paths. And the third will specify what to do with all of the membership paths.



来源:https://stackoverflow.com/questions/52297991/configure-redirects-on-firebase-hosting-spa-2-subfolders-firebase-json

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!