Skip to content

Commit 1ed6573

Browse files
committed
chore: update
1 parent f73c0c8 commit 1ed6573

2 files changed

Lines changed: 28 additions & 17 deletions

File tree

packages/devframe/src/utils/serve-static.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,25 @@ describe('serveStaticHandler', () => {
168168
})
169169

170170
describe('mountStaticHandler', () => {
171+
it('serves files when mounted at the root', async () => {
172+
const dir = makeTmp('devframe-serve-root-')
173+
writeFileSync(join(dir, 'index.html'), 'root-index', 'utf-8')
174+
writeFileSync(join(dir, 'app.js'), 'root-app', 'utf-8')
175+
176+
const app = new H3()
177+
mountStaticHandler(app, '/', dir)
178+
179+
const indexResponse = await app.request('/')
180+
const assetResponse = await app.request('/app.js')
181+
const missingResponse = await app.request('/missing.js')
182+
183+
expect(indexResponse.status).toBe(200)
184+
expect(await indexResponse.text()).toBe('root-index')
185+
expect(assetResponse.status).toBe(200)
186+
expect(await assetResponse.text()).toBe('root-app')
187+
expect(missingResponse.status).toBe(404)
188+
})
189+
171190
it('keeps static bases with overlapping prefixes isolated', async () => {
172191
const viteDir = makeTmp('devframe-serve-vite-')
173192
const vitestDir = makeTmp('devframe-serve-vitest-')

packages/devframe/src/utils/serve-static.ts

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import type { EventHandler, H3 } from 'h3'
1+
import type { EventHandler } from 'h3'
22
import type { IncomingMessage, ServerResponse } from 'node:http'
33
import { createReadStream } from 'node:fs'
44
import { stat } from 'node:fs/promises'
55
import { Readable } from 'node:stream'
6-
import { defineHandler, withBase } from 'h3'
6+
import { defineHandler, H3 } from 'h3'
77
import { lookup } from 'mrmime'
88
import { extname, join, normalize, resolve, sep } from 'pathe'
99

@@ -168,29 +168,21 @@ export function serveStaticHandler(
168168
}
169169

170170
/**
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`.
173172
*
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`.
178176
*/
179177
export function mountStaticHandler(
180178
app: H3,
181179
base: string,
182180
dir: string,
183181
options?: ServeStaticOptions,
184182
): 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)
194186
}
195187

196188
/**

0 commit comments

Comments
 (0)