Unverified Commit 562e4283 authored by Forrest's avatar Forrest Committed by GitHub

Use finally to clean up seen requests (#36222)

* try-finally for router execute request references

* lint
Co-authored-by: 's avatarJJ Kasper <jj@jjsweb.site>
parent 2e00eb67
...@@ -146,7 +146,7 @@ export default class Router { ...@@ -146,7 +146,7 @@ export default class Router {
) )
} }
this.seenRequests.add(req) this.seenRequests.add(req)
try {
// memoize page check calls so we don't duplicate checks for pages // memoize page check calls so we don't duplicate checks for pages
const pageChecks: { [name: string]: Promise<boolean> } = {} const pageChecks: { [name: string]: Promise<boolean> } = {}
const memoizedPageChecker = async (p: string): Promise<boolean> => { const memoizedPageChecker = async (p: string): Promise<boolean> => {
...@@ -172,7 +172,12 @@ export default class Router { ...@@ -172,7 +172,12 @@ export default class Router {
if (fsParams) { if (fsParams) {
checkParsedUrl.pathname = fsPathname checkParsedUrl.pathname = fsPathname
const fsResult = await fsRoute.fn(req, res, fsParams, checkParsedUrl) const fsResult = await fsRoute.fn(
req,
res,
fsParams,
checkParsedUrl
)
if (fsResult.finished) { if (fsResult.finished) {
return true return true
...@@ -238,7 +243,12 @@ export default class Router { ...@@ -238,7 +243,12 @@ export default class Router {
name: 'page checker', name: 'page checker',
requireBasePath: false, requireBasePath: false,
match: route('/:path*'), match: route('/:path*'),
fn: async (checkerReq, checkerRes, params, parsedCheckerUrl) => { fn: async (
checkerReq,
checkerRes,
params,
parsedCheckerUrl
) => {
let { pathname } = parsedCheckerUrl let { pathname } = parsedCheckerUrl
pathname = removePathTrailingSlash(pathname || '/') pathname = removePathTrailingSlash(pathname || '/')
...@@ -298,7 +308,8 @@ export default class Router { ...@@ -298,7 +308,8 @@ export default class Router {
const originalPathname = currentPathname const originalPathname = currentPathname
const requireBasePath = testRoute.requireBasePath !== false const requireBasePath = testRoute.requireBasePath !== false
const isCustomRoute = customRouteTypes.has(testRoute.type) const isCustomRoute = customRouteTypes.has(testRoute.type)
const isPublicFolderCatchall = testRoute.name === 'public folder catchall' const isPublicFolderCatchall =
testRoute.name === 'public folder catchall'
const isMiddlewareCatchall = testRoute.name === 'middleware catchall' const isMiddlewareCatchall = testRoute.name === 'middleware catchall'
const keepBasePath = const keepBasePath =
isCustomRoute || isPublicFolderCatchall || isMiddlewareCatchall isCustomRoute || isPublicFolderCatchall || isMiddlewareCatchall
...@@ -337,7 +348,9 @@ export default class Router { ...@@ -337,7 +348,9 @@ export default class Router {
parsedUrl.query.__nextLocale && parsedUrl.query.__nextLocale &&
!localePathResult.detectedLocale !localePathResult.detectedLocale
) { ) {
currentPathname = `${activeBasePath}/${parsedUrl.query.__nextLocale}${ currentPathname = `${activeBasePath}/${
parsedUrl.query.__nextLocale
}${
currentPathnameNoBasePath === '/' ? '' : currentPathnameNoBasePath currentPathnameNoBasePath === '/' ? '' : currentPathnameNoBasePath
}` }`
} }
...@@ -381,7 +394,6 @@ export default class Router { ...@@ -381,7 +394,6 @@ export default class Router {
) { ) {
if (requireBasePath) { if (requireBasePath) {
// consider this a non-match so the 404 renders // consider this a non-match so the 404 renders
this.seenRequests.delete(req)
return false return false
} }
// page checker occurs before rewrites so we need to continue // page checker occurs before rewrites so we need to continue
...@@ -392,11 +404,15 @@ export default class Router { ...@@ -392,11 +404,15 @@ export default class Router {
parsedUrlUpdated.pathname = currentPathname parsedUrlUpdated.pathname = currentPathname
} }
const result = await testRoute.fn(req, res, newParams, parsedUrlUpdated) const result = await testRoute.fn(
req,
res,
newParams,
parsedUrlUpdated
)
// The response was handled // The response was handled
if (result.finished) { if (result.finished) {
this.seenRequests.delete(req)
return true return true
} }
...@@ -420,13 +436,14 @@ export default class Router { ...@@ -420,13 +436,14 @@ export default class Router {
// check filesystem // check filesystem
if (testRoute.check === true) { if (testRoute.check === true) {
if (await applyCheckTrue(parsedUrlUpdated)) { if (await applyCheckTrue(parsedUrlUpdated)) {
this.seenRequests.delete(req)
return true return true
} }
} }
} }
} }
this.seenRequests.delete(req)
return false return false
} finally {
this.seenRequests.delete(req)
}
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment