Skip to content

Commit 7de8580

Browse files
authored
Merge pull request #2314 from SalesforceCommerceCloud/bugfix/server-timing
Fix server timing header delimiter
2 parents 5076f8f + 58f951d commit 7de8580

File tree

2 files changed

+147
-1
lines changed

2 files changed

+147
-1
lines changed

packages/pwa-kit-react-sdk/src/ssr/universal/components/with-react-query/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export const withReactQuery = (Wrapped, options = {}) => {
7777
await Promise.all(
7878
queries.map((q, i) => {
7979
// always include the index to avoid duplicate entries
80-
const displayName = q.meta?.displayName ? `${q.meta?.displayName}:${i}` : `${i}`
80+
const displayName = q.meta?.displayName ? `${q.meta?.displayName}-${i}` : `${i}`
8181
res.__performanceTimer.mark(
8282
`${PERFORMANCE_MARKS.reactQueryUseQuery}.${displayName}`,
8383
'start'

packages/pwa-kit-react-sdk/src/ssr/universal/components/with-react-query/index.test.js

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ import {withReactQuery} from './index'
88
import {render, screen} from '@testing-library/react'
99
import React from 'react'
1010
import logger from '../../../../utils/logger-instance'
11+
import {PERFORMANCE_MARKS} from '../../../../utils/performance'
12+
13+
jest.mock('@tanstack/react-query', () => ({
14+
...jest.requireActual('@tanstack/react-query'),
15+
dehydrate: jest.fn().mockReturnValue({})
16+
}))
1117

1218
jest.mock('../../../../utils/logger-instance', () => {
1319
return {
@@ -91,4 +97,144 @@ describe('withReactQuery', function () {
9197
expect(withReactQuery({}).getHOCsInUse()).toHaveLength(1)
9298
expect(withReactQuery({getHOCsInUse: () => ['xyz']}).getHOCsInUse()).toHaveLength(2)
9399
})
100+
101+
test('Performance markers use hyphen as delimiter for displayName', async () => {
102+
const mockPerformanceTimer = {
103+
mark: jest.fn()
104+
}
105+
106+
const mockQueryMeta = {
107+
displayName: 'TestQuery'
108+
}
109+
110+
const mockQueryCache = {
111+
getAll: jest.fn().mockReturnValue([
112+
{
113+
options: {enabled: true},
114+
meta: mockQueryMeta,
115+
queryHash: 'test-hash',
116+
fetch: jest.fn().mockResolvedValue({})
117+
}
118+
])
119+
}
120+
121+
const mockQueryClient = {
122+
getQueryCache: jest.fn().mockReturnValue(mockQueryCache)
123+
}
124+
125+
const res = {
126+
locals: {
127+
__queryClient: mockQueryClient
128+
},
129+
__performanceTimer: mockPerformanceTimer
130+
}
131+
132+
const Component = withReactQuery({})
133+
134+
await Component.doInitAppState({
135+
res,
136+
appJSX: <div>Test</div>
137+
})
138+
139+
expect(mockPerformanceTimer.mark).toHaveBeenCalledWith(
140+
`${PERFORMANCE_MARKS.reactQueryUseQuery}.TestQuery-0`,
141+
'start'
142+
)
143+
144+
expect(mockPerformanceTimer.mark).toHaveBeenCalledWith(
145+
`${PERFORMANCE_MARKS.reactQueryUseQuery}.TestQuery-0`,
146+
'end',
147+
expect.objectContaining({
148+
detail: 'test-hash'
149+
})
150+
)
151+
})
152+
153+
test('Performance markers use index as displayName when meta.displayName is not available', async () => {
154+
const mockPerformanceTimer = {
155+
mark: jest.fn()
156+
}
157+
158+
const mockQueryCache = {
159+
getAll: jest.fn().mockReturnValue([
160+
{
161+
options: {enabled: true},
162+
meta: {},
163+
queryHash: 'test-hash',
164+
fetch: jest.fn().mockResolvedValue({})
165+
}
166+
])
167+
}
168+
169+
const mockQueryClient = {
170+
getQueryCache: jest.fn().mockReturnValue(mockQueryCache)
171+
}
172+
173+
const res = {
174+
locals: {
175+
__queryClient: mockQueryClient
176+
},
177+
__performanceTimer: mockPerformanceTimer
178+
}
179+
180+
const Component = withReactQuery({})
181+
182+
await Component.doInitAppState({
183+
res,
184+
appJSX: <div>Test</div>
185+
})
186+
187+
// Verify the performance marker uses just the index when no displayName is available
188+
expect(mockPerformanceTimer.mark).toHaveBeenCalledWith(
189+
`${PERFORMANCE_MARKS.reactQueryUseQuery}.0`,
190+
'start'
191+
)
192+
193+
expect(mockPerformanceTimer.mark).toHaveBeenCalledWith(
194+
`${PERFORMANCE_MARKS.reactQueryUseQuery}.0`,
195+
'end',
196+
expect.objectContaining({
197+
detail: 'test-hash'
198+
})
199+
)
200+
})
201+
202+
test('Performance markers for reactQueryPrerender are set correctly', async () => {
203+
const mockPerformanceTimer = {
204+
mark: jest.fn()
205+
}
206+
207+
const mockQueryCache = {
208+
getAll: jest.fn().mockReturnValue([])
209+
}
210+
211+
const mockQueryClient = {
212+
getQueryCache: jest.fn().mockReturnValue(mockQueryCache)
213+
}
214+
215+
const res = {
216+
locals: {
217+
__queryClient: mockQueryClient
218+
},
219+
__performanceTimer: mockPerformanceTimer
220+
}
221+
222+
const Component = withReactQuery({})
223+
224+
await Component.doInitAppState({
225+
res,
226+
appJSX: <div>Test</div>
227+
})
228+
229+
// Verify the performance markers for reactQueryPrerender are set correctly
230+
expect(mockPerformanceTimer.mark).toHaveBeenCalledWith(
231+
PERFORMANCE_MARKS.reactQueryPrerender,
232+
'start'
233+
)
234+
235+
expect(mockPerformanceTimer.mark).toHaveBeenCalledWith(
236+
PERFORMANCE_MARKS.reactQueryPrerender,
237+
'end'
238+
)
239+
})
94240
})

0 commit comments

Comments
 (0)