首页 > 解决方案 > Spring Gateway 如何路由深度 SPA 链接

问题描述

我有一个从 Spring Gateway 路由的 Angular 应用程序。根路由正常工作,但是当您深入嵌套路由时,它返回未找到状态。在Angular 文档和本教程上,您必须重写任何未找到的路由到 /index.html,但我在 Spring Cloud Gateway 文档中没有找到任何关于如何配置它的参考。

应用程序.yml

      - id: teams-frontend
        uri: lb://teams-frontend
        predicates:
          - Path=/teams,/teams/**
        filters:
          - StripPrefix=1
#         - RewritePath=/.*, /index.html  ## This breaks existing link resources such as the scripts linked on the index.html

找不到时,角度应用程序如何回退到 index.html?

标签: angularspringgateway

解决方案


我开发了一个自定义过滤器。如果请求包含标头“Accept: text/html”,则请求路径将被 /index.html 覆盖

- id: teams-frontend
    uri: lb://teams-frontend
    predicates:
      - Path=/teams,/teams/**
    filters:
      - StripPrefix=1
      - SPA

推荐阅读