Transformation adds spurious slash to end of url with an anchor selector

I am trying to redirect links going to one directory with anchor ids (#some-entry), and my transformation rule works great, except there is an unwanted slash added to the end of the url, which breaks the syntax and results in a 404 error. Here’s the rule:

For example, a request for “/toc/help” comes back with “/glossary#help/” and I can’t see where that is coming from.

I also tried a worker with the same exact results. Here’s the code:

const redirectHttpCode = 301

addEventListener(‘fetch’, event => {
event.respondWith(handleRequest(event.request))
})

/**

  • Respond to the request
  • @param {Request} request
    */
    async function handleRequest(request) {
    const url = new URL(request.url)
    const { pathname } = url
    const pathParts = pathname.split(‘/’)

if (pathParts[1] === ‘toc’) {
url.pathname = /glossary#+ pathParts[2]
return Response.redirect(url.toString(), redirectHttpCode)
}

return fetch(request) // by default proxy request as usual
}

Any help will received with gratitude. I’ve two days and can’t find anyone reporting this here or on the web.