代码之家  ›  专栏  ›  技术社区  ›  Davide Valdo

在firebase宿主spa+2子文件夹firebase.json上配置重定向

  •  0
  • Davide Valdo  · 技术社区  · 7 年前

    我有一个公用文件夹,如:

    /public
       index.html
    
       /landing
         index.html
    
       /membership
         index.html
    

    /public/index.html是一个spa,所以对/**的每个请求都应该重写为/index.html

    登陆/和/或成员资格/是两个静态HTML登陆,所以每个请求不应该被重写。

    google支持建议使用firebase.json:

    {
        "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/**"
            ]
        }
    }
    

    所有内容都被重定向到/index.html…

    甚至尝试过:

    {
      "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/**"
          ]
      }
    }
    

    同样的结果,一切都会/

    1 回复  |  直到 7 年前
        1
  •  2
  •   Gerard Walace    7 年前

    你不必使用“重定向”。 只需使用这些重写规则:

    {
        "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"
                }
            ]
        }
    }
    

    第一条规则将重写所有内容,除了从登陆或会员资格开始的内容。 第二条规则将处理着陆路径。 第三将指定如何处理所有的成员路径。

    推荐文章