Skip to content

Commit

Permalink
Merge pull request #4536 from Shopify/improve-user-logs
Browse files Browse the repository at this point in the history
[Themes] Dev Server - Improve user logging for render requests
  • Loading branch information
EvilGenius13 authored Oct 4, 2024
2 parents c525193 + 0b587df commit a825c91
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/big-hairs-carry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/cli': minor
---

Improve user logging for render requests
27 changes: 23 additions & 4 deletions packages/theme/src/cli/utilities/theme-environment/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@ import {getProxyStorefrontHeaders, patchRenderingResponse} from './proxy.js'
import {getInMemoryTemplates, injectHotReloadScript} from './hot-reload/server.js'
import {render} from './storefront-renderer.js'
import {getExtensionInMemoryTemplates} from '../theme-ext-environment/theme-ext-server.js'
import {defineEventHandler, getCookie, setResponseHeader, setResponseStatus, type H3Error} from 'h3'
import {timestampDateFormat} from '../../constants.js'
import {defineEventHandler, getCookie, H3Event, setResponseHeader, setResponseStatus, type H3Error} from 'h3'
import {renderError, renderFatalError} from '@shopify/cli-kit/node/ui'
import {outputInfo} from '@shopify/cli-kit/node/output'
import {outputContent, outputInfo, outputToken} from '@shopify/cli-kit/node/output'
import {AbortError} from '@shopify/cli-kit/node/error'
import type {Response} from '@shopify/cli-kit/node/http'
import type {Theme} from '@shopify/cli-kit/node/themes/types'
import type {DevServerContext} from './types.js'

const CHARACTER_TRUNCATION_LIMIT = 80

export function getHtmlHandler(theme: Theme, ctx: DevServerContext) {
return defineEventHandler((event) => {
outputInfo(`${event.method} ${event.path}`)

const [browserPathname = '/', browserSearch = ''] = event.path.split('?')

return render(ctx.session, {
Expand All @@ -27,6 +28,8 @@ export function getHtmlHandler(theme: Theme, ctx: DevServerContext) {
replaceTemplates: getInMemoryTemplates(ctx, browserPathname, getCookie(event, 'localization')?.toLowerCase()),
})
.then(async (response) => {
logRequestLine(event, response)

let html = await patchRenderingResponse(ctx, event, response)

html = prettifySyntaxErrors(html)
Expand Down Expand Up @@ -67,6 +70,22 @@ export function getHtmlHandler(theme: Theme, ctx: DevServerContext) {
})
}

function logRequestLine(event: H3Event, response: Response) {
const truncatedPath =
event.path.length > CHARACTER_TRUNCATION_LIMIT
? `${event.path.substring(0, CHARACTER_TRUNCATION_LIMIT)}...`
: event.path
const serverTiming = response.headers.get('server-timing')
const requestDuration = serverTiming?.match(/cfRequestDuration;dur=([\d.]+)/)?.[1]
const durationString = requestDuration ? `${Math.round(Number(requestDuration))}ms` : ''

outputInfo(
outputContent`• ${timestampDateFormat.format(new Date())} Request ${outputToken.raw('»')} ${outputToken.gray(
`${event.method} ${truncatedPath} ${durationString}`,
)}`,
)
}

export function prettifySyntaxErrors(html: string) {
return html.replace(/Liquid(?: syntax)? error \([^\n]+(?:\n|<)/g, getErrorSection)
}
Expand Down

0 comments on commit a825c91

Please sign in to comment.