I'm trying to run multiple React SPA apps using ASP.NET Core 3.1 with the lastest SpaServices extension and have a problem serving static assets. Much of what I've built comes from a similar question at: How to configure ASP.net Core server routing for multiple SPAs hosted with SpaServices but that was related to Angular and an older version of SpaServices.
My code has two React apps, ClientApp
and AdminApp
and I have configured each using the following startup code:
app.Map("/client", clientApp =>
{
clientApp.UseSpa(spa =>
{
spa.Options.SourcePath = "ClientApp";
if (env.IsDevelopment())
{
spa.UseReactDevelopmentServer(npmScript: "start");
}
});
});
app.Map("/admin", adminApp =>
{
adminApp.UseSpa(spa =>
{
spa.Options.SourcePath = "AdminApp";
if (env.IsDevelopment())
{
spa.UseReactDevelopmentServer(npmScript: "start");
}
});
});
Going to the /client
and /admin
routes serves the correct React index.html
file, but the linked bundled .js
files do not load. Here's an example of the final index HTML for the ClientApp
SPA:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<base href="/client" />
<title>Client App</title>
</head>
<body>
<h1>Client App</h1>
<div id="root"></div>
<script src="/static/js/bundle.js"></script>
<script src="/static/js/0.chunk.js"></script>
<script src="/static/js/main.chunk.js"></script>
</body>
</html>
The problem is that the links are relative to the root of the site /static/js/bundle.js
and need to be relative to the React app's path. The example file paths should be /client/static/js/bundle.js
and /admin/static/js/bundle.js
, respectively.
How do I get the system that writes the paths into the index.html file to use the correct root path for the static files? Thanks.
homepage
property in package.json did add the new path for the script src paths. But now seeing uncaught syntaxerror logs in the console. – Swaneehomepage
property works, but only in the published output. It doesn't work when debugging, which means it is pointless for me. I ended up building a separate admin site unfortunately. – Illogicalitybuild
each react project so I could point to the prod build directories of each app. Which does defeat the purpose. Still haven't been able to get a proxy setup working just yet either. – Chkalov