Skip to content

Commit

Permalink
Update serve.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
emmron committed Nov 7, 2024
1 parent ec3e580 commit 186553b
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions packages/gatsby/src/commands/serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ const sanitizeUrl = (url: string): string => {
const createMatchPathMiddleware = (
matchPaths: Array<IMatchPath>,
options: IMatchPathMiddlewareOptions
) => {
): express.RequestHandler => {
// Cache commonly accessed paths
const pathCache = new Map<string, string | null>()
const { root, enableLogging = false } = options
Expand Down Expand Up @@ -117,13 +117,17 @@ const createMatchPathMiddleware = (
})

if (matchPath) {
// Cache the result
pathCache.set(originalUrl, matchPath.path)

if (enableLogging) {
const [seconds, nanoseconds] = process.hrtime(startTime!)
if (enableLogging && startTime) {
// Remove non-null assertion
const [seconds, nanoseconds] = process.hrtime(startTime)
const duration = seconds * 1000 + nanoseconds / 1e6
report.info(`Matched ${originalUrl} to ${matchPath.path} (${duration.toFixed(2)}ms)`)
report.info(
`Matched ${originalUrl} to ${matchPath.path} (${duration.toFixed(
2
)}ms)`
)
}

return res.sendFile(
Expand All @@ -139,10 +143,8 @@ const createMatchPathMiddleware = (
)
}

// Cache non-matches too
pathCache.set(originalUrl, null)
return next()

} catch (error) {
report.error(`Error processing ${originalUrl}: ${error.message}`)
res.status(500)
Expand Down Expand Up @@ -376,10 +378,12 @@ module.exports = async (program: IServeProgram): Promise<void> => {
}

const matchPaths = await readMatchPaths(program)
router.use(createMatchPathMiddleware(matchPaths, {
root,
enableLogging: process.env.NODE_ENV !== 'production'
}))
router.use(
createMatchPathMiddleware(matchPaths, {
root,
enableLogging: process.env.NODE_ENV !== `production`,
})
)

// TODO: Remove/merge with above same block
router.use((req, res, next) => {
Expand Down

0 comments on commit 186553b

Please sign in to comment.