|
1 | | -import type { EventHandler, H3 } from 'h3' |
| 1 | +import type { EventHandler } from 'h3' |
2 | 2 | import type { IncomingMessage, ServerResponse } from 'node:http' |
3 | 3 | import { createReadStream } from 'node:fs' |
4 | 4 | import { stat } from 'node:fs/promises' |
5 | 5 | import { Readable } from 'node:stream' |
6 | | -import { defineHandler, withBase } from 'h3' |
| 6 | +import { defineHandler, H3 } from 'h3' |
7 | 7 | import { lookup } from 'mrmime' |
8 | 8 | import { extname, join, normalize, resolve, sep } from 'pathe' |
9 | 9 |
|
@@ -168,29 +168,21 @@ export function serveStaticHandler( |
168 | 168 | } |
169 | 169 |
|
170 | 170 | /** |
171 | | - * Mount {@link serveStaticHandler} on an h3 app at `base`, handling the |
172 | | - * route pattern and prefix-stripping required by h3 v2. |
| 171 | + * Mount {@link serveStaticHandler} on an h3 app at `base`. |
173 | 172 | * |
174 | | - * h3 v2's `app.use(base, handler)` only matches the exact `base` path and |
175 | | - * does not strip the prefix from `event.url.pathname`. Static serving |
176 | | - * needs an explicit segment-boundary match plus a stripped URL so the file |
177 | | - * resolver sees paths relative to `dir` — this helper bundles both. |
| 173 | + * h3's sub-app mount provides segment-boundary matching and strips `base` |
| 174 | + * from `event.url.pathname`, so the file resolver sees paths relative to |
| 175 | + * `dir`. |
178 | 176 | */ |
179 | 177 | export function mountStaticHandler( |
180 | 178 | app: H3, |
181 | 179 | base: string, |
182 | 180 | dir: string, |
183 | 181 | options?: ServeStaticOptions, |
184 | 182 | ): void { |
185 | | - const trimmed = base.replace(/\/$/, '') |
186 | | - const handler = serveStaticHandler(dir, options) |
187 | | - if (trimmed === '') { |
188 | | - app.use('/**', handler) |
189 | | - return |
190 | | - } |
191 | | - app.use(withBase(trimmed, handler), { |
192 | | - match: event => event.url.pathname === trimmed || event.url.pathname.startsWith(`${trimmed}/`), |
193 | | - }) |
| 183 | + const staticApp = new H3() |
| 184 | + staticApp.use(serveStaticHandler(dir, options)) |
| 185 | + app.mount(base.replace(/\/$/, ''), staticApp) |
194 | 186 | } |
195 | 187 |
|
196 | 188 | /** |
|
0 commit comments