Closed BernardHymmen closed 6 months ago
Hi, which value are you using in the Manifest option?
I'm using `"Vite:Manifest": "assets.manifest.json" (and just doublechecked it for typos :) ). Here's my appsettings.json file (minus the app-specific stuff):
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning",
"System.Net.Http.HttpClient.Vite.AspNetCore.DevHttpClient": "Warning"
}
},
"AllowedHosts": "*",
"Vite": {
"Manifest": "assets.manifest.json"
}
}
Can I see your Program.cs file? Only the Vite stuff.
Sure! You'll notice there's some code at the top to set ASPNETCORE_ENVIRONMENT based on the build configuration, but the problem I'm reporting here was happening before that code went in, so I don't think it's contributing (plus it seems to be working just fine otherwise and has proven very handy for me). Other than that, there isn't anything special or proprietary going on, so this is my entire Program.cs file.
using System.Reflection;
using Vite.AspNetCore;
// Set the value for ASPNETCORE_ENVIRONMENT based on build configuration if it's not already set externally
if (string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")))
{
AssemblyConfigurationAttribute? assemblyConfigurationAttribute = Assembly.GetExecutingAssembly().GetCustomAttribute<AssemblyConfigurationAttribute>();
string? buildConfigurationName = assemblyConfigurationAttribute?.Configuration;
Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT",
string.Equals(buildConfigurationName, "Debug", StringComparison.OrdinalIgnoreCase)
? "Development"
: "Production");
}
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
builder.Services.AddRazorPages();
builder.Services.AddViteServices(options =>
{
options.Server.AutoRun = true;
options.Server.Https = true;
});
WebApplication app = builder.Build();
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapRazorPages();
if (app.Environment.IsDevelopment())
{
app.UseWebSockets();
app.UseViteDevelopmentServer(true);
}
app.UseStatusCodePagesWithReExecute("/index");
app.Run();
Ahh you are passing options to the AddViteServices
method. If you do that, then the library will use those values instead.
Use the method without parameters or add the manifest name to the method.
builder.Services.AddViteServices(options =>
{
options.Manifest = "assets.manifest.json";
options.Server.AutoRun = true;
options.Server.Https = true;
});
Ah HA! Yes, adding options.Manifest = ...
did the trick. Thank you so much for taking the time to help me!
(Whoops, I meant to close this issue with my last reply.)
I'm migrating an existing site from Webpack to Vite so that I can use a Razor page to do some necessary server-side processing and I'm using Vite.AspNetCore to do it. My _Layout.cshtml page (edited down for brevity) looks like this:
and the relevant part of my index.cshtml page looks like this:
When I run this with ASPNECTCORE_ENVIRONMENT set to "Development" I get this rendered HTML output, and my site works as expected.
My understanding from the documentation is that in "Production" the
vite-src
tag helper should render the exact same things, but with paths corrected to read fromwwwroot
. However, when I try that, I get this instead and my site is obviously deeply broken for lack of access to the main script.I tried working around this by using this pattern:
but I ran into the same problem: works with "Development", but not with "Production". When I inspected the value of
@Manifest
in the debugger I found that it was an enumeration that had no results. This is surprising because when I look inwwwroot
I can seeassests.manifest.json
and it looks well-formed to my Vite-newbie eyes. In fact, I'm pretty confident that it's well-formed because my site works when I read the manifest myself and pass it to the Razor code through my page model like so:So, there's clearly a bug somewhere. I'm not sure if it's in my code (wouldn't surprise me) or in Vite.AspNetCore (which I assume is unlikely given the high-level nature of the issue). Here's my
vite.config.ts
file (for brevity I've chopped out the stuff related to creating the SSL certifcate for localhost since it seems unrelated and is working):