Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/vite/src/node/server/middlewares/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,10 @@ export function transformMiddleware(
return async function viteTransformMiddleware(req, res, next) {
const environment = server.environments.client

if (req.method !== 'GET' || knownIgnoreList.has(req.url!)) {
if (
(req.method !== 'GET' && req.method !== 'HEAD') ||
knownIgnoreList.has(req.url!)
) {
return next()
}

Expand Down
6 changes: 5 additions & 1 deletion packages/vite/src/node/server/send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ export function send(
}

res.statusCode = 200
res.end(content)
if (req.method === 'HEAD') {
res.end()
} else {
res.end(content)
}
return
}
27 changes: 25 additions & 2 deletions playground/resolve/__tests__/resolve.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import fs from 'node:fs'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import { expect, test } from 'vitest'
import { isBuild, isWindows, page, testDir, viteTestUrl } from '~utils'
import { describe, expect, test } from 'vitest'
import { isBuild, isServe, isWindows, page, testDir, viteTestUrl } from '~utils'

test('bom import', async () => {
expect(await page.textContent('.utf8-bom')).toMatch('[success]')
Expand Down Expand Up @@ -263,3 +263,26 @@ test.runIf(isBuild)('sideEffects field glob pattern is respected', async () => {
)
expect(sideEffectValues).toStrictEqual(['success'])
})

describe.runIf(isServe)('HEAD request handling', () => {
test('HEAD request to JS file returns correct Content-Type', async () => {
const response = await fetch(new URL('/absolute.js', viteTestUrl), {
method: 'HEAD',
})
expect(response.headers.get('content-type')).toBe('text/javascript')
expect(response.status).toBe(200)
const text = await response.text()
expect(text).toBe('')
})

test('HEAD request to CSS file returns correct Content-Type', async () => {
const response = await fetch(new URL('/style.css', viteTestUrl), {
method: 'HEAD',
headers: {
Accept: 'text/css',
},
})
expect(response.headers.get('content-type')).toBe('text/css')
expect(response.status).toBe(200)
})
})
5 changes: 5 additions & 0 deletions playground/resolve/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/* Simple CSS for HEAD request testing */
body {
margin: 0;
padding: 0;
}
Loading