Reputation: 18765
My AspNetCore/React project works fine in the Development environment but as soon as I run it in any other environment I get System.InvalidOperationException: The SPA default page middleware could not return the default page '/index.html' because it was not found, and no other middleware handled the request.
The end of the startup.cs configure method looks like this
app.UseSpa(spa =>
{
spa.Options.SourcePath = "ClientApp";
if (env.IsDevelopment())
{
spa.UseReactDevelopmentServer(npmScript: "start");
}
});
but since this is identical to the corresponding code in the app generated by dotnet new react
I suppose something else is upsetting it.
The webserver does start and I can access Swagger pages. It's just the app proper that can't be found. Hacking the above code so that spa.UseReactDevelopmentServer(npmScript: "start");
runs causes the app to be found for all environments (environment specific config is loaded) but served by the React dev server.
I would have said this line
spa.Options.SourcePath = "ClientApp";
was what governs the finding of /index.html
and friends, but something is preventing them from being found.
What else I should look at?
I've seen this The SPA default page middleware could not return the default page but I don't think this is the problem I'm facing.
A search of the project reveals than there is only one file index.html
and it is located in ClientApp/public
. The Configure method starts like this
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days...
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseSpaStaticFiles();
Upvotes: 0
Views: 975
Reputation: 18765
React assets are not built by dotnet build
or dotnet run
not even with -c Release
.
To also build the React assets you need dotnet publish
.
This requirement may be masked by the presence of assets due to earlier use of dotnet publish
. In that case they are potentially stale. Therefore, for any environment other than dev, you must use dotnet publish
.
Why don't dotnet build
and dotnet run
build the React assets? In development you don't need them because you're using the dev server for hot swap. In production you're almost certainly using assets prepared by dotnet publish
.
Why did I not remember this? I almost never do builds for environments other than dev. I don't build for production, CICD does that. Professional testers are more likely to be aware of this than programmers simply because they build for environments other than dev.
Upvotes: 2