Repository: honojs/hono (TypeScript)

Issue:
Same parameter name issue of TrieRouter
### What version of Hono are you using?

3.12.9

### What runtime/platform is your app running on?

any

### What steps can reproduce the bug?

```ts
import { Hono } from 'hono'
import { getRouterName } from 'hono/dev'
import { createMiddleware } from 'hono/factory'
import { TrieRouter } from 'hono/router/trie-router'

const middleware = createMiddleware(async (c, next) => {
  console.log('in middleware', c.req.param())
  await next()
})

const app = new Hono({
  router: new TrieRouter(),
})

app.use('/:id/:id2/comments', middleware)
app.get('/posts/:id/comments', (c) => {
  console.log('in handler', c.req.param())
  return c.text(c.req.param('id'))
})

async function test() {
  const res = await app.request('/posts/123/comments')
  console.log(res.status)
}

test()
```

### What is the expected behavior?

```txt
in middleware {
  id: "posts",
  id2: "123",
}
in handler {
  id: "123",
}
```

### What do you see instead?


```txt
in middleware {
  id: "123",
  id2: "123",
}
in handler {
  id: "123",
}
```

### Additional information

_No response_


Discussion:

Patch:
diff --git a/deno_dist/router/trie-router/node.ts b/deno_dist/router/trie-router/node.ts
index fd5b16bfb..d6d6d1fa5 100644
--- a/deno_dist/router/trie-router/node.ts
+++ b/deno_dist/router/trie-router/node.ts
@@ -119,7 +119,6 @@ export class Node<T> {
   search(method: string, path: string): [[T, Params][]] {
     const handlerSets: HandlerParamsSet<T>[] = []
     this.params = {}
-    const params: Record<string, string> = {}
 
     // eslint-disable-next-line @typescript-eslint/no-this-alias
     const curNode: Node<T> = this
@@ -151,6 +150,8 @@ export class Node<T> {
         for (let k = 0, len3 = node.patterns.length; k < len3; k++) {
           const pattern = node.patterns[k]
 
+          const params = { ...node.params }
+
           // Wildcard
           // '/hello/*/foo' => match /hello/bar/foo
           if (pattern === '*') {
@@ -184,10 +185,10 @@ export class Node<T> {
               if (isLast === true) {
                 handlerSets.push(...this.gHSets(child, method, params, node.params))
                 if (child.children['*']) {
-                  handlerSets.push(...this.gHSets(child.children['*'], method, node.params, params))
+                  handlerSets.push(...this.gHSets(child.children['*'], method, params, node.params))
                 }
               } else {
-                child.params = { ...params }
+                child.params = params
                 tempNodes.push(child)
               }
             }
diff --git a/src/router/trie-router/node.ts b/src/router/trie-router/node.ts
index 3f7bd9e2e..9a7a0fdbb 100644
--- a/src/router/trie-router/node.ts
+++ b/src/router/trie-router/node.ts
@@ -119,7 +119,6 @@ export class Node<T> {
   search(method: string, path: string): [[T, Params][]] {
     const handlerSets: HandlerParamsSet<T>[] = []
     this.params = {}
-    const params: Record<string, string> = {}
 
     // eslint-disable-next-line @typescript-eslint/no-this-alias
     const curNode: Node<T> = this
@@ -151,6 +150,8 @@ export class Node<T> {
         for (let k = 0, len3 = node.patterns.length; k < len3; k++) {
           const pattern = node.patterns[k]
 
+          const params = { ...node.params }
+
           // Wildcard
           // '/hello/*/foo' => match /hello/bar/foo
           if (pattern === '*') {
@@ -184,10 +185,10 @@ export class Node<T> {
               if (isLast === true) {
                 handlerSets.push(...this.gHSets(child, method, params, node.params))
                 if (child.children['*']) {
-                  handlerSets.push(...this.gHSets(child.children['*'], method, node.params, params))
+                  handlerSets.push(...this.gHSets(child.children['*'], method, params, node.params))
                 }
               } else {
-                child.params = { ...params }
+                child.params = params
                 tempNodes.push(child)
               }
             }


Test patch:
diff --git a/src/router/trie-router/node.test.ts b/src/router/trie-router/node.test.ts
index 2a8a54827..3c8d4e6f7 100644
--- a/src/router/trie-router/node.test.ts
+++ b/src/router/trie-router/node.test.ts
@@ -768,4 +768,19 @@ describe('The same name is used for path params', () => {
       expect(res[0][1]).toEqual({ b: 'about', a: 'me' })
     })
   })
+
+  describe('Complex with tails', () => {
+    const node = new Node()
+    node.insert('get', '/:id/:id2/comments', 'a')
+    node.insert('get', '/posts/:id/comments', 'b')
+    it('/posts/123/comments', () => {
+      const [res] = node.search('get', '/posts/123/comments')
+      expect(res).not.toBeNull()
+      expect(res.length).toBe(2)
+      expect(res[0][0]).toEqual('a')
+      expect(res[0][1]).toEqual({ id: 'posts', id2: '123' })
+      expect(res[1][0]).toEqual('b')
+      expect(res[1][1]).toEqual({ id: '123' })
+    })
+  })
 })