UseStaticFiles behavior when accessed through branched IAppBuilder. #25864
-
I have two React projects with build directories indicated below, with the prod builds of each React app (ReactApp1/Build and ReactApp2/Build). /Project I am serving these two SPA’s conditionally from my asp.net core pipeline. Here is a section of my Startup.cs
Currently, this code works how I expect. When I visit /MyApps/app1, the build packages from ReactApp1/Build are served. Same with /MyApps/app2 (which is similar to app1 in my pipeline logic). Relating to the line: It took me a while to figure out that I had to invoke UseSpaStaticFiles from the original IApplicationBuilder app object ( I thought I could just do Only when Why is this? Perhaps it is due to my lack of understanding regarding these anonymous lambda branching expressions and how each nested |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 8 replies
-
Map makes a specific change to the request that other APIs like MapWhen can't. It trims the matched portion of the request.Path and moves it to request.PathBase. Most middleware ignore the PathBase and only look at the remaining Path. Why this would mess up UseSpaStaticFiles is a good question, but as a rule, nothing inside any of these Map* Actions should ever reference the outer IApplicationBuilder. That's the same as not putting it inside the Map Action at all. Similarly, I don't think that MapWhen is doing anything, you never reference myappsBuilder. Clean up the sample to remove the MapWhen and nest UseSpaStaticFiles and then we can start tracking down why it doesn't work as expected. |
Beta Was this translation helpful? Give feedback.
-
I'll put this here for anybody who stumbles on this thread. There are a lot of examples out there modifying the base href in angular SPAs, but not a lot of information for React. The solution for this scenario involved me changing the "homepage" route. Once I changed the default production route for the react app, I was able to call the UseStaticFiles from the branched IAppBuilder with no issues. The initial issue was that my react app index.html was being served properly, but the static content was being fetched at the root of the url (as per a default create-react-app project). Changing the "homepage" route (in package.json) to match the app.map("/route") branch (in startup.cs) which I was enabling static file usage under for that individual spa, resolved it. |
Beta Was this translation helpful? Give feedback.
Map makes a specific change to the request that other APIs like MapWhen can't. It trims the matched portion of the request.Path and moves it to request.PathBase. Most middleware ignore the PathBase and only look at the remaining Path.
Why this would mess up UseSpaStaticFiles is a good question, but as a rule, nothing inside any of these Map* Actions should ever reference the outer IApplicationBuilder. That's the same as not putting it inside the Map Action at all.
Similarly, I don't think that MapWhen is doing anything, you never reference myappsBuilder.
Clean up the sample to remove the MapWhen and nest UseSpaStaticFiles and then we can start tracking down why it doesn't work as expected.