Skip to content

Commit

Permalink
feature: add server timing response header (#249)
Browse files Browse the repository at this point in the history
  • Loading branch information
levivilet authored Dec 27, 2024
1 parent 2366077 commit 0e16c40
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
34 changes: 23 additions & 11 deletions packages/preview-process/src/parts/GetResponse/GetResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,28 @@ import { NotFoundResponse } from '../Responses/NotFoundResponse.ts'
import * as Routes from '../Routes/Routes.ts'

export const getResponse = async (request: RequestOptions, options: HandlerOptions): Promise<Response> => {
if (request.method !== HttpMethod.Get && request.method !== HttpMethod.Head) {
return new MethodNotAllowedResponse()
}
const matchedRoute = FindMatchingRoute.findMatchingRoute(request.path, Routes.routes)
if (!matchedRoute) {
return new NotFoundResponse()
}
const response = await matchedRoute.handler(request, options)
if (request.method === HttpMethod.Head) {
return new HeadResponse(response.status, response.headers)
const start = performance.now()

try {
if (request.method !== HttpMethod.Get && request.method !== HttpMethod.Head) {
return new MethodNotAllowedResponse()
}
const matchedRoute = FindMatchingRoute.findMatchingRoute(request.path, Routes.routes)
if (!matchedRoute) {
return new NotFoundResponse()
}
const response = await matchedRoute.handler(request, options)
if (request.method === HttpMethod.Head) {
return new HeadResponse(response.status, response.headers)
}

// Add Server-Timing header
const duration = Math.round(performance.now() - start)
response.headers.set('Server-Timing', `total;dur=${duration}`)

return response
} catch (error) {
const response = new NotFoundResponse()
return response
}
return response
}
8 changes: 8 additions & 0 deletions packages/preview-process/test/WebViewProtocol.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ test('get - css file', async () => {
headers: {
'Content-Type': 'text/css',
'Cross-Origin-Resource-Policy': 'same-origin',
'Server-Timing': expect.any(String),
},
})
expect(response.body.toString()).toBe('a')
Expand All @@ -69,6 +70,7 @@ test('get - javascript file', async () => {
headers: {
'Content-Type': 'text/javascript',
'Cross-Origin-Resource-Policy': 'same-origin',
'Server-Timing': expect.any(String),
},
})
expect(response.body.toString()).toBe('console.log("test")')
Expand Down Expand Up @@ -102,6 +104,7 @@ test('get - preview injected', async () => {
headers: {
'Content-Type': 'text/javascript',
'Cross-Origin-Resource-Policy': 'same-origin',
'Server-Timing': expect.any(String),
},
})
expect(response.body).toBeDefined()
Expand All @@ -118,6 +121,7 @@ test('get - index.html', async () => {
'Content-Type': 'text/html',
'Cross-Origin-Resource-Policy': 'cross-origin',
'Cross-Origin-Embedder-Policy': 'require-corp',
'Server-Timing': expect.any(String),
},
})
expect(response.body.toString()).toBe('<h1>hello world</h1>')
Expand Down Expand Up @@ -162,6 +166,7 @@ test('get - png image file', async () => {
headers: {
'Content-Type': 'image/png',
'Cross-Origin-Resource-Policy': 'same-origin',
'Server-Timing': expect.any(String),
},
},
})
Expand All @@ -179,6 +184,7 @@ test('get - svg file', async () => {
headers: {
'Content-Type': 'image/svg+xml',
'Cross-Origin-Resource-Policy': 'same-origin',
'Server-Timing': expect.any(String),
},
},
})
Expand All @@ -196,6 +202,7 @@ test('get - json file', async () => {
headers: {
'Content-Type': 'application/json',
'Cross-Origin-Resource-Policy': 'same-origin',
'Server-Timing': expect.any(String),
},
},
})
Expand All @@ -213,6 +220,7 @@ test('get - unknown file type', async () => {
headers: {
'Content-Type': 'text/plain',
'Cross-Origin-Resource-Policy': 'same-origin',
'Server-Timing': expect.any(String),
},
},
})
Expand Down

0 comments on commit 0e16c40

Please sign in to comment.