如何配置 Undertow 处理程序以支持 SPA 书签的正确重写?

Mar*_*ark 5 jboss bookmarks url-rewriting single-page-application undertow

我正在尝试配置 JBoss EAP 7(通过 Undertow)以index.html使用 Undertow 处理程序正确地将任何 SPA URL 重写回 SPA。不幸的是,我的API位于/api,所以我需要让任何以 开头的请求通过/api

这是我当前的配置(摘自另一个 SO 答案):

not equals(%R, '/my-app') and 
not equals(%R, '/my-app/') and 
not equals(%R, '/my-app/index.html') and 
not path-prefix('/my-app/api') and 
not regex('/my-app/.*\.js') and 
regex('/my-app/.+') -> rewrite('/my-app/index.html')
Run Code Online (Sandbox Code Playgroud)

不幸的是,这似乎并没有重写任何东西。如何更新此配置以属性重写 URL?

Mar*_*ark 4

首先,尝试以下配置WEB-INF/undertow-handlers.conf

path-prefix('/api') -> done
path-suffix('.js') -> done
path-prefix('/') -> rewrite('/')
Run Code Online (Sandbox Code Playgroud)

您不需要/my-app任何规则的前缀,因为它们已经在您的应用程序上下文中运行。

但是,您可能需要添加其他谓词以防止重写其他资源,例如样式表、网站图标、源地图等。谓词和处理程序的完整列表有助于生成更具体、有针对性的规则。

请注意,path-suffix仍然占像这样的路径/app?thing.js。尽管您可能永远不会使用这样的查询参数,但最好记住它会被重写。