Skip to content

Commit c2e821a

Browse files
committed
perf(router): rely on scoped query diagnostics
1 parent c1e7baa commit c2e821a

2 files changed

Lines changed: 20 additions & 4 deletions

File tree

storage/framework/core/router/src/stacks-router.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ function resolveDefaultsPath(rel: string): string {
207207

208208
import { runWithRequest } from './request-context'
209209
import { isApiRequest, JSON_CONTENT_TYPE } from './api-shape'
210-
import { clearTrackedQueries, createErrorResponse, createMiddlewareErrorResponse } from './error-handler'
210+
import { createErrorResponse, createMiddlewareErrorResponse } from './error-handler'
211211
import { applySecurityHeaders, applySecurityHeadersToRecord } from './security-headers'
212212
import { isCursorPaginator, isPaginator, isSimplePaginator } from '@stacksjs/pagination'
213213

@@ -1908,9 +1908,6 @@ function createMiddlewareHandler(routeKey: string, handler: StacksHandler): Rout
19081908
const baseResult = wrappedBase(enhancedReq)
19091909
let response = baseResult instanceof Response ? baseResult : await baseResult
19101910

1911-
// Clear tracked queries after each request to prevent accumulation
1912-
clearTrackedQueries()
1913-
19141911
// CSRF cookie seeding — on safe-method responses (GET/HEAD/OPTIONS),
19151912
// attach a fresh `X-CSRF-Token` cookie when none is present so SPAs
19161913
// and forms have a usable token to echo on the next unsafe request.

storage/framework/core/router/tests/request-context.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,25 @@ describe('Request Context - request proxy', () => {
139139
})
140140

141141
describe('Request Context - clearing', () => {
142+
test('fresh request scopes start with fresh query tracking', () => {
143+
const previousAppEnv = process.env.APP_ENV
144+
process.env.APP_ENV = 'development'
145+
146+
runWithRequest(makeFakeRequest(), () => {
147+
trackQuery('SELECT * FROM users WHERE id = 1')
148+
expect(getQueryShapeCounts().get('SELECT * FROM USERS WHERE ID = ?')).toBe(1)
149+
})
150+
151+
runWithRequest(makeFakeRequest(), () => {
152+
expect(getQueryShapeCounts().size).toBe(0)
153+
})
154+
155+
if (previousAppEnv === undefined)
156+
delete process.env.APP_ENV
157+
else
158+
process.env.APP_ENV = previousAppEnv
159+
})
160+
142161
test('query cleanup in an unused request does not clear the fallback scope', () => {
143162
clearCurrentRequest()
144163
const previousAppEnv = process.env.APP_ENV

0 commit comments

Comments
 (0)