index.ts
This file
Path
src/middleware/trailing-slash/index.ts
158 lines 路 4.3 KiB 路 TypeScript
Last changed by
Not known.
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159/** * @module * Trailing Slash Middleware for Hono. */
import type { MiddlewareHandler } from '../../types'
type TrimTrailingSlashOptions = { /** * If `true`, the middleware will always redirect requests with a trailing slash * before executing handlers. * This is useful for routes with wildcards (`*`). * If `false` (default), it will only redirect when the route is not found (404). * @default false */ alwaysRedirect?: boolean}
/** * Trailing Slash Middleware for Hono. * * @see {@link https://hono.dev/docs/middleware/builtin/trailing-slash} * * @param {TrimTrailingSlashOptions} options - The options for the middleware. * @returns {MiddlewareHandler} The middleware handler function. * * @example * ```ts * const app = new Hono() * * app.use(trimTrailingSlash()) * app.get('/about/me/', (c) => c.text('With Trailing Slash')) * ``` * * @example * ```ts * // With alwaysRedirect option for wildcard routes * const app = new Hono() * * app.use(trimTrailingSlash({ alwaysRedirect: true })) * app.get('/my-path/*', (c) => c.text('Wildcard route')) * ``` */export const trimTrailingSlash = (options?: TrimTrailingSlashOptions): MiddlewareHandler => { return async function trimTrailingSlash(c, next) { if (options?.alwaysRedirect) { if ( (c.req.method === 'GET' || c.req.method === 'HEAD') && c.req.path !== '/' && c.req.path.at(-1) === '/' ) { const url = new URL(c.req.url) url.pathname = url.pathname.substring(0, url.pathname.length - 1)
return c.redirect(url.toString(), 301) } }
await next()
if ( !options?.alwaysRedirect && c.res.status === 404 && (c.req.method === 'GET' || c.req.method === 'HEAD') && c.req.path !== '/' && c.req.path.at(-1) === '/' ) { const url = new URL(c.req.url) url.pathname = url.pathname.substring(0, url.pathname.length - 1)
c.res = c.redirect(url.toString(), 301) } }}
type AppendTrailingSlashOptions = { /** * If `true`, the middleware will always redirect requests without a trailing slash * before executing handlers. * This is useful for routes with wildcards (`*`). * If `false` (default), it will only redirect when the route is not found (404). * @default false */ alwaysRedirect?: boolean /** * A function that determines whether to skip the redirect for a given path. * If the function returns `true`, the redirect will be skipped. * @param path - The request path * @returns `true` to skip the redirect * * @example * ```ts * // Skip redirect for paths with file extensions * app.use(appendTrailingSlash({ * alwaysRedirect: true, * skip: (path) => /\.\w+$/.test(path), * })) * ``` */ skip?: (path: string) => boolean}
/** * Append trailing slash middleware for Hono. * Append a trailing slash to the URL if it doesn't have one. For example, `/path/to/page` will be redirected to `/path/to/page/`. * * @see {@link https://hono.dev/docs/middleware/builtin/trailing-slash} * * @param {AppendTrailingSlashOptions} options - The options for the middleware. * @returns {MiddlewareHandler} The middleware handler function. * * @example * ```ts * const app = new Hono() * * app.use(appendTrailingSlash()) * ``` * * @example * ```ts * // With alwaysRedirect option for wildcard routes * const app = new Hono() * * app.use(appendTrailingSlash({ alwaysRedirect: true })) * app.get('/my-path/*', (c) => c.text('Wildcard route')) * ``` */export const appendTrailingSlash = (options?: AppendTrailingSlashOptions): MiddlewareHandler => { return async function appendTrailingSlash(c, next) { if (options?.alwaysRedirect) { if ( (c.req.method === 'GET' || c.req.method === 'HEAD') && c.req.path.at(-1) !== '/' && !options.skip?.(c.req.path) ) { const url = new URL(c.req.url) url.pathname += '/'
return c.redirect(url.toString(), 301) } }
await next()
if ( !options?.alwaysRedirect && c.res.status === 404 && (c.req.method === 'GET' || c.req.method === 'HEAD') && c.req.path.at(-1) !== '/' && !options?.skip?.(c.req.path) ) { const url = new URL(c.req.url) url.pathname += '/'
c.res = c.redirect(url.toString(), 301) } }}