使用Dropwizard服务多个静态资产

问题描述 投票:0回答:1

我有一个正在构建并尝试使用AssetBundle服务的react应用,例如:

    @Override
    public void initialize(final Bootstrap<PersonalWebsiteConfiguration> bootstrap) {
        bootstrap.addBundle(new SwaggerBundle<PersonalWebsiteConfiguration>() {
            protected SwaggerBundleConfiguration getSwaggerBundleConfiguration(final PersonalWebsiteConfiguration configuration) {
                return configuration.swaggerBundleConfiguration;
            }
        });
        bootstrap.addBundle(new AssetsBundle("/build", "/", "index.html"));
    }

我还添加了配置

server:
  rootPath: /api

因此不会与我的API发生冲突。

这仅适用于我的React应用的登录页面。每当我尝试路由/ login / dashboard时,都不会从UI找到该页面。因此,我尝试添加更多的包来解决路由问题:

        bootstrap.addBundle(new AssetsBundle("/build", "/", "index.html"));
        bootstrap.addBundle(new AssetsBundle("/build", "/login", "index.html"));
        bootstrap.addBundle(new AssetsBundle("/build", "/dashboard", "index.html"));

现在,只有仪表板正在工作。有谁知道如何为具有多个路由/页面的React构建提供服务。

java reactjs dropwizard
1个回答
0
投票

对于单页应用程序,您需要所有客户端路由来返回index.html(以支持浏览器重新加载或登陆/以外的路径)据我所知,Dropwizard AssetBundle无法做到这一点,即使用index.html服务所有路由。参见类似的(旧的)question

您可以自己实现servlet过滤器,也可以使用this one之类的社区插件。

我必须说另一种方法对我来说更好,不要完全使用dropwizard服务静态资产,而只能将其用作后端API。对API和静态资产使用CDN路由或其他子域。这样,您可以在www.mydomain.com上拥有静态资产,并在api.mydomain.com上拥有API(或使用相同的域,并基于路径前缀,例如/ api到后端或静态资源的路由)

© www.soinside.com 2019 - 2024. All rights reserved.