Client Side Deep Links with WebpackDevMiddleware 4

2020-08-17 06:11发布

问题:

I am using the WebpackDevMiddleware for Development builds to serve up a Vue.js application that uses client-side routing. The SPA application is served up from the root url just fine, but if I attempt to use any client-side deep links, I get a 404.

Note running as Production works as expected.

What I want:

  • http://locahost/ - serve up the vue app.
  • http://localhost/overlays/chat - serve up the vue app.
  • http://localhost/api/* - serve up the api routes handled server side.

There is a minimum viable reproduction of the problem in this repository. You can run it using vscode debugging as Development environment where the bug happens. There is also a script /scripts/local-production that will build and run as Production environment, where it works as expected.

Relevant portions of my Startup.cs looks like this:

public class Startup
{
  public IConfiguration Configuration { get; }

  public Startup(IConfiguration configuration)
  {
    Configuration = configuration;
  }

  public void ConfigureServices(IServiceCollection services)
  {


    services.AddMvc();

    // In production, the Vue files will be served
    //  from this directory
    services.AddSpaStaticFiles(configuration =>
    {
        configuration.RootPath = Configuration["Client"];
    });
  }

  public void Configure(IApplicationBuilder app, IHostingEnvironment env)
  {

    //set up default mvc routing
    app.UseMvc(routes =>
    {
      routes.MapRoute("default", "api/{controller=Home}/{action=Index}/{id?}");
    });

    //setup spa routing for both dev and prod
    if (env.IsDevelopment())
    {
      app.UseWebpackDevMiddleware(new WebpackDevMiddlewareOptions {
          HotModuleReplacement = true,
          ProjectPath = Path.Combine(env.ContentRootPath, Configuration["ClientProjectPath"]),
          ConfigFile = Path.Combine(env.ContentRootPath, Configuration["ClientProjectConfigPath"])
      });
    }
    else
    {
      app.UseWhen(context => !context.Request.Path.Value.StartsWith("/api"),
        builder => {
          app.UseSpaStaticFiles();
          app.UseSpa(spa => {
            spa.Options.DefaultPage = "/index.html";
          });

          app.UseMvc(routes => {
            routes.MapSpaFallbackRoute(
                name: "spa-fallback",
                defaults: new { controller = "Fallback", action = "Index" });
          });
        });
    }
  }
}

回答1:

I was able to get around this using the status code pages middleware to handle all status codes and re-execute using the root path. This will cause the spa app to be served up for all status codes in the 400-599 range which is not quite what I want but gets me working again at least.

//setup spa routing for both dev and prod
if (env.IsDevelopment())
{
  //force client side deep links to render the spa on 404s
  app.UseStatusCodePagesWithReExecute("/");
  app.UseWebpackDevMiddleware(new WebpackDevMiddlewareOptions {
    HotModuleReplacement = true,
     ProjectPath = Path.Combine(env.ContentRootPath, Configuration["ClientProjectPath"]),
     ConfigFile = Path.Combine(env.ContentRootPath, Configuration["ClientProjectConfigPath"])
  });
}

Hopefully, this will help someone in the future that might be bumping up against this issue.