Add vite-prefix attribute to ViteTagHelper to allow prefixed/relative manifest asset paths #177
+507
−19
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR adds an optional
vite-prefixattribute to the existingViteTagHelper. When present, the helper will prepend the provided prefix to the manifest-resolved asset path before emitting thehreforsrcattribute. If the combined prefix+path begins with~, it is expanded usingUrlHelper.Content(soRequest.PathBasestill applies). Dev server behavior is unchanged, and manifest resolution is preserved.Motivation
When an ASP.NET Core app is hosted behind proxies that add dynamic external prefixes (for example
/sso/<guid>/...) or when you need assets referenced relative to the current document, emitting app-root absolute paths isn't flexible enough.This change preserves all the benefits of Vite's manifest (hashed filenames and CSS chunk discovery) while giving developers control over final URL generation through:
../for document-relative paths)/static/for CDN or specific mount points)~/prefix/forPathBase-aware URLs)Changes Made
ViteTagHelper.cs
[HtmlAttributeName("vite-prefix")] public string? VitePrefix { get; set; }ResolveCssFilePath()andResolveJsFilePath()helper methods to apply prefix logicVitePrefixis provided and non-empty, the tag helper prepends the normalized prefix to the resolved manifest path~,UrlHelper.Contentis used for expansion<link>tag generatedoutput.Attributes.RemoveAll(VITE_PREFIX_ATTRIBUTE)to ensure the custom attribute doesn't leak into HTML output'/','~') for better performanceDocumentation
README.md(root) withvite-prefixsection and examplessrc/library/Vite.AspNetCore/README.mdwith detailed usage examplesTests
src/tests/ViteNetProject.Tests/VitePrefixTests.cs../)/static/)~/prefix/)[Collection("Sequential")]attribute to prevent test isolation issuesDependencies
Backward Compatibility
✅ No breaking changes - this is a fully backward-compatible addition:
vite-prefixis not setvite-prefixattribute is completely optionalUsage Examples
Relative Prefix (Document-Relative Paths)
Static Prefix (CDN or Mount Point)
Tilde Prefix (PathBase-Aware)