Skip to content

Commit 3efa4ab

Browse files
authored
Merge pull request #40 from RafalGoslawski/issue37
fix: fixed issues with serving index.html
2 parents be96e7d + b7f2de9 commit 3efa4ab

File tree

4 files changed

+41
-33
lines changed

4 files changed

+41
-33
lines changed

.prettierrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"useTabs": true,
2+
"useTabs": false,
33
"tabWidth": 4,
44
"semi": false,
55
"singleQuote": true,

bun.lockb

0 Bytes
Binary file not shown.

src/index.ts

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ export const staticPlugin = async <Prefix extends string = '/prefix'>(
8989
*/
9090
staticLimit?: number
9191
/**
92-
* @default false
92+
* @default false unless `NODE_ENV` is 'production'
9393
*
9494
* Should file always be served statically
9595
*/
@@ -210,31 +210,29 @@ export const staticPlugin = async <Prefix extends string = '/prefix'>(
210210
}
211211
})
212212

213+
const assetsDir = assets[0] === sep ? assets : resolve() + sep + assets
214+
213215
if (
214216
alwaysStatic ||
215217
(process.env.ENV === 'production' && files.length <= staticLimit)
216218
)
217-
for (let i = 0; i < files.length; i++) {
218-
const filePath = files[i]
219-
if (!filePath || shouldIgnore(filePath)) continue
220-
221-
let fileName = filePath
222-
.replace(resolve(), '')
223-
.replace(`${assets}${sep}`, '')
219+
for (const absolutePath of files) {
220+
if (!absolutePath || shouldIgnore(absolutePath)) continue
221+
let relativePath = absolutePath.replace(assetsDir, '')
224222

225223
if (noExtension) {
226-
const temp = fileName.split('.')
224+
const temp = relativePath.split('.')
227225
temp.splice(-1)
228226

229-
fileName = temp.join('.')
227+
relativePath = temp.join('.')
230228
}
231229

232-
const file = Bun.file(filePath)
230+
const file = Bun.file(absolutePath)
233231
const etag = await generateETag(file)
234232

235233
const pathName = isFSSepUnsafe
236-
? prefix + fileName.split(sep).join(URL_PATH_SEP)
237-
: join(prefix, fileName)
234+
? prefix + relativePath.split(sep).join(URL_PATH_SEP)
235+
: join(prefix, relativePath)
238236

239237
app.get(
240238
pathName,
@@ -243,7 +241,7 @@ export const staticPlugin = async <Prefix extends string = '/prefix'>(
243241
headers
244242
})
245243
: async ({ headers: reqHeaders }) => {
246-
if (await isCached(reqHeaders, etag, filePath)) {
244+
if (await isCached(reqHeaders, etag, absolutePath)) {
247245
return new Response(null, {
248246
status: 304,
249247
headers
@@ -263,7 +261,7 @@ export const staticPlugin = async <Prefix extends string = '/prefix'>(
263261

264262
if (indexHTML && pathName.endsWith('/index.html'))
265263
app.get(
266-
join(prefix, pathName.replace('/index.html', '')),
264+
pathName.replace('/index.html', ''),
267265
noCache
268266
? new Response(file, {
269267
headers
@@ -316,6 +314,9 @@ export const staticPlugin = async <Prefix extends string = '/prefix'>(
316314
statCache.set(path, status)
317315
}
318316

317+
if (!indexHTML && status.isDirectory())
318+
throw new NotFoundError()
319+
319320
let file =
320321
fileCache.get<ReturnType<(typeof Bun)['file']>>(
321322
path

test/index.test.ts

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ describe('Static Plugin', () => {
9696
})
9797

9898
it('ignore string pattern', async () => {
99-
const app = new Elysia({ forceErrorEncapsulation: true }).use(
99+
const app = new Elysia().use(
100100
staticPlugin({
101101
ignorePatterns: [`public${sep}takodachi.png`]
102102
})
@@ -141,7 +141,6 @@ describe('Static Plugin', () => {
141141
const app = new Elysia().use(
142142
staticPlugin({
143143
alwaysStatic: true,
144-
prefix: '',
145144
assets: join(import.meta.dir, '../public')
146145
})
147146
)
@@ -390,48 +389,56 @@ describe('Static Plugin', () => {
390389
})
391390

392391
it('serve index.html to default /', async () => {
393-
let app = new Elysia().use(staticPlugin())
392+
const app = new Elysia().use(staticPlugin())
394393
await app.modules
395394

396395
let res = await app.handle(req('/public'))
397396
expect(res.status).toBe(404)
398397

399398
res = await app.handle(req('/public/html'))
400399
expect(res.status).toBe(200)
400+
})
401401

402-
app = new Elysia().use(
402+
it('does not serve index.html to default / when not indexHTML', async () => {
403+
const app = new Elysia().use(
403404
staticPlugin({
404405
indexHTML: false
405406
})
406407
)
408+
await app.modules
407409

408-
res = await app.handle(req('/public'))
410+
let res = await app.handle(req('/public'))
409411
expect(res.status).toBe(404)
410412

411413
res = await app.handle(req('/public/html'))
412414
expect(res.status).toBe(404)
415+
})
413416

414-
// Not sure why this error but not in dev environment
415-
// app = new Elysia().use(
416-
// staticPlugin({
417-
// alwaysStatic: true
418-
// })
419-
// )
417+
it('serves index.html to default / when alwaysStatic', async () => {
418+
const app = new Elysia().use(
419+
staticPlugin({
420+
alwaysStatic: true
421+
})
422+
)
423+
await app.modules
420424

421-
// res = await app.handle(req('/public'))
422-
// expect(res.status).toBe(404)
425+
let res = await app.handle(req('/public'))
426+
expect(res.status).toBe(404)
423427

424-
// res = await app.handle(req('/public/html'))
425-
// expect(res.status).toBe(200)
428+
res = await app.handle(req('/public/html'))
429+
expect(res.status).toBe(200)
430+
})
426431

427-
app = new Elysia().use(
432+
it('does not serve index.html to default / when alwaysStatic and not indexHTML', async () => {
433+
const app = new Elysia().use(
428434
staticPlugin({
429435
alwaysStatic: true,
430436
indexHTML: false
431437
})
432438
)
439+
await app.modules
433440

434-
res = await app.handle(req('/public'))
441+
let res = await app.handle(req('/public'))
435442
expect(res.status).toBe(404)
436443

437444
res = await app.handle(req('/public/html'))

0 commit comments

Comments
 (0)