Dan
03/14/2024, 12:27 PMusing Vite.AspNetCore;
using Vite.AspNetCore.Extensions;
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
builder.CreateUmbracoBuilder()
.AddBackOffice()
.AddWebsite()
.AddDeliveryApi()
.AddComposers()
.Build();
builder.Services.AddViteServices(new ViteOptions()
{
Server = new ViteServerOptions
{
Port = 5173,
Https = true,
}
});
WebApplication app = builder.Build();
await app.BootUmbracoAsync();
app.UseHttpsRedirection();
app.UseUmbraco()
.WithMiddleware(u =>
{
u.UseBackOffice();
u.UseWebsite();
})
.WithEndpoints(u =>
{
u.UseInstallerEndpoints();
u.UseBackOfficeEndpoints();
u.UseWebsiteEndpoints();
});
if (app.Environment.IsDevelopment())
{
app.UseViteDevelopmentServer();
}
await app.RunAsync();
Everything else is pretty much the same (similar`package.json`, identical vite.config.js
, same folder structure, same reserved paths [including ~/@vite/,~/@id/
] in appsettings.Development.json
etc). The assets are output from Master.cshtml file like this:
<environment include="Development">
<script type="module" defer src="~/@@vite/client"></script> <!-- Load Vite -->
<script type="module" defer src="~/scripts/main.js"></script> <!-- Define the entry point -->
</environment>
However, when running the new site both /@vite/client
and /scripts/main.js
are throwing 404s.
The middleware configuration (above) looks okay AFAIK and all else is very similar so I'm struggling to understand why one works and the other doesn't.
Could anyone suggest what might be awry here?Sebastiaan
03/14/2024, 12:38 PMapp.UseViteDevelopmentServer()
to before app.UseUmbraco
? Not sure if that would make a difference though. You might need to enable static files app.UseStaticFiles();
maybe?
That one if a bit problematic though (see https://github.com/umbraco/Umbraco-CMS/issues/12666) - but try if that's needed first and then you can work on exclusions for it as described in the issue.Dan
03/14/2024, 1:02 PMapp.UseViteDevelopmentServer()
to before app.UseUmbraco
doesn't seem to fix it, and I'm aware of this https://github.com/Eptagone/Vite.AspNetCore/issues/37#issuecomment-1637403136 which suggests that it needs to be after. I'll take a look at app.UseStaticFiles()
but am curious as to why that would be needed since the other working project doesn't modify that in Startup.cs. I'll have a play anyway, thanks for your input 🙂Jason
03/14/2024, 1:28 PMUseViteDevelopmentServer(true);
to enable the middleware.
Also, I have options.Server.AutoRun = true;
to avoid having to run the npm command separately.Jason
03/14/2024, 1:31 PMDan
03/14/2024, 1:35 PMtrue
in UseViteDevelopmentServer()
! I think what's happened is that previously I'd initially used app.UseViteDevMiddleware();
which defaulted to true
but swapped that out for UseViteDevelopmentServer()
as it's deprecated. Works nicely now, thank you, sir! 🙂Dan
03/14/2024, 1:36 PMSebastiaan
03/14/2024, 1:40 PMDan
03/14/2024, 1:44 PMJason
03/14/2024, 4:02 PMDomitnator
03/15/2024, 8:08 AMJason
03/15/2024, 8:15 AMDomitnator
03/15/2024, 8:22 AMDan
03/15/2024, 10:30 AMvite-plugin-mkcert
rather than having the custom /certs.js
if you've gone in that direction (happy to share if not and you'd find it useful?).