Skip to content

Commit

Permalink
feat(serve-static): add path option (#331)
Browse files Browse the repository at this point in the history
  • Loading branch information
yusukebe authored Jun 27, 2022
1 parent 5125c78 commit 7766139
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/middleware/serve-static/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const app = new Hono()

app.use('/static/*', serveStatic({ root: './' }))
app.get('/', (c) => c.text('This is Home! You can access: /static/hello.txt'))
app.get('*', serveStatic({ path: './static/fallback.txt' }))

app.fire()
```
Expand Down Expand Up @@ -45,6 +46,7 @@ Asset files:
├── demo
│   └── index.html
├── hello.txt
├── fallback.txt
└── images
└── dinotocat.png
```
Expand Down
18 changes: 18 additions & 0 deletions src/middleware/serve-static/serve-static.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,24 @@ describe('With options', () => {
})
})

describe('With `file` options', () => {
const app = new Hono()
app.get('/foo/*', serveStatic({ path: './assets/static/hono.html' }))
app.get('/bar/*', serveStatic({ path: './static/hono.html', root: './assets' }))

it('Should return hono.html', async () => {
const res = await app.request('http://localhost/foo/fallback')
expect(res.status).toBe(200)
expect(await res.text()).toBe('<h1>Hono!</h1>')
})

it('Should return hono.html - with `root` option', async () => {
const res = await app.request('http://localhost/bar/fallback')
expect(res.status).toBe(200)
expect(await res.text()).toBe('<h1>Hono!</h1>')
})
})

describe('With middleware', () => {
const app = new Hono()
const md1 = async (c: Context, next: Next) => {
Expand Down
5 changes: 3 additions & 2 deletions src/middleware/serve-static/serve-static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { getContentFromKVAsset, getKVFilePath } from '../../utils/cloudflare'
import { getMimeType } from '../../utils/mime'

export type ServeStaticOptions = {
root: string
root?: string
path?: string
manifest?: object | string
namespace?: KVNamespace
}
Expand All @@ -22,7 +23,7 @@ export const serveStatic = (options: ServeStaticOptions = { root: '' }): Handler
const url = new URL(c.req.url)

const path = getKVFilePath({
filename: url.pathname,
filename: options.path ?? url.pathname,
root: options.root,
defaultDocument: DEFAULT_DOCUMENT,
})
Expand Down
2 changes: 1 addition & 1 deletion src/utils/cloudflare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export const getKVFilePath = (options: FilePathOptions): string => {
}

// /foo.html => foo.html
filename = filename.replace(/^\//, '')
filename = filename.replace(/^\.?\//, '')

// assets/ => assets
root = root.replace(/\/$/, '')
Expand Down

0 comments on commit 7766139

Please sign in to comment.