1
0
mirror of synced 2025-12-21 10:57:10 -05:00
Files
docs/middleware/trailing-slashes.js

26 lines
734 B
JavaScript

import { cacheControlFactory } from './cache-control.js'
const cacheControl = cacheControlFactory(60 * 60)
export default function trailingSlashes(req, res, next) {
if (req.method === 'GET' || req.method === 'HEAD' || req.method === 'OPTIONS') {
const split = req.url.split('?')
let pathname = split.shift()
if (pathname !== '/' && pathname.endsWith('/')) {
while (pathname.endsWith('/')) {
pathname = pathname.slice(0, pathname.length - 1)
}
let url = pathname
if (split.length) {
url += `?${split.join('?')}`
}
// So it can be cached in the CDN
res.removeHeader('set-cookie')
cacheControl(res)
return res.redirect(301, url)
}
}
next()
}