Commit 6fbe0dd
authored
fix(vite): basePath handling for static assets (freshframework#3394)
closes freshframework#3391
---
Generated PR message:
Fix base path handling for assets in plugin-vite
Fixes issue where Fresh apps mounted at non-root paths (e.g., /ui) in
frameworks like Hono would fail to load assets correctly.
Problem
When mounting a Fresh app with app.mount('/ui', UI.fetch), the HTML
would load but all assets (CSS, JS, images) would return 404 because
asset paths
were hardcoded to start with /assets/ instead of respecting the Vite
base configuration.
Solution
- Modified packages/plugin-vite/src/plugins/server_entry.ts to read
Vite's base config and apply it to asset paths
- Added getAssetPath() helper function to construct proper asset URLs
with base path
- Updated both CSS and asset file registration to use the base path
Testing
Added comprehensive test case "vite build - base path asset handling"
that:
- Builds demo with custom base path /my-app/
- Verifies generated server.js contains correctly prefixed asset paths
- Follows existing test patterns and utilities
Usage
Users can now properly mount Fresh apps by configuring both:
```ts
// vite.config.ts
export default defineConfig({
base: "/ui/",
plugins: [fresh()],
});
// main.ts
const app = new App({ basePath: "/ui" });
// hono-app.ts
app.mount('/ui', UI.fetch); // Assets now work correctly
```
Backward compatible - no changes needed for apps mounted at root path.1 parent 554644b commit 6fbe0dd
3 files changed
Lines changed: 38 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
| 19 | + | |
19 | 20 | | |
20 | 21 | | |
21 | 22 | | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
22 | 32 | | |
23 | 33 | | |
24 | 34 | | |
| |||
29 | 39 | | |
30 | 40 | | |
31 | 41 | | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
32 | 46 | | |
33 | 47 | | |
34 | 48 | | |
| |||
98 | 112 | | |
99 | 113 | | |
100 | 114 | | |
101 | | - | |
| 115 | + | |
102 | 116 | | |
103 | 117 | | |
104 | 118 | | |
| |||
110 | 124 | | |
111 | 125 | | |
112 | 126 | | |
113 | | - | |
| 127 | + | |
114 | 128 | | |
115 | 129 | | |
116 | 130 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
447 | 447 | | |
448 | 448 | | |
449 | 449 | | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
131 | 131 | | |
132 | 132 | | |
133 | 133 | | |
134 | | - | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
135 | 138 | | |
136 | 139 | | |
137 | 140 | | |
| |||
140 | 143 | | |
141 | 144 | | |
142 | 145 | | |
| 146 | + | |
143 | 147 | | |
144 | 148 | | |
145 | 149 | | |
| |||
0 commit comments