Skip to content

Commit

Permalink
fix(dev-server): Fix issue in dev-server where requestListener would …
Browse files Browse the repository at this point in the history
…return null (#212)

* Fix issue in dev-server where requestListener would return null

* add patch changeset for dev-server fix

* add a test

---------

Co-authored-by: Yusuke Wada <yusuke@kamawada.com>
  • Loading branch information
gobengo and yusukebe authored Jan 16, 2025
1 parent e7d3a11 commit 01d28ca
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/long-panthers-decide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hono/vite-dev-server': patch
---

dev-server plugin getRequestListener fetchCallback now always returns Promise<Response> instead of sometimes returning Promise<null>
15 changes: 14 additions & 1 deletion packages/dev-server/e2e/e2e.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { test, expect } from '@playwright/test'
import { test, expect, request } from '@playwright/test'

test('Should return 200 response', async ({ page }) => {
const response = await page.goto('/')
Expand Down Expand Up @@ -122,3 +122,16 @@ test('Should return files in the public directory', async ({ page }) => {
const res = await page.goto('/hono-logo.png')
expect(res?.status()).toBe(200)
})

test('Should not crash when receiving a HEAD request', async () => {
const apiContext = await request.newContext()
const response = await apiContext.fetch('/', {
method: 'HEAD',
})

expect(response.status()).toBe(200)
expect(response.headers()['content-type']).toMatch(/^text\/html/)
const body = await response.body()
expect(body.length).toBe(0)
await apiContext.dispose()
})
4 changes: 2 additions & 2 deletions packages/dev-server/src/dev-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export function devServer(options?: DevServerOptions): VitePlugin {
}

getRequestListener(
async (request) => {
async (request): Promise<Response> => {
let env: Env = {}

if (options?.env) {
Expand Down Expand Up @@ -168,7 +168,7 @@ export function devServer(options?: DevServerOptions): VitePlugin {
.get('content-security-policy')
?.match(/'nonce-([^']+)'/)?.[1]
const script = `<script${nonce ? ` nonce="${nonce}"` : ''}>import("/@vite/client")</script>`
return injectStringToResponse(response, script)
return injectStringToResponse(response, script) ?? response
}
return response
},
Expand Down

0 comments on commit 01d28ca

Please sign in to comment.