feat(trailing-slash): add `skip` option (#4862)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Yusuke Wada · committed by GitHub · parent 1aa32fb · Browse files · 2 files·+80 added−2 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+80 added−2 removed
- tests:+57 added−0 removed
- other:+23 added−2 removed
Provenance
- Commit
f23e97b- Tree
0262ca3- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
1aa32fb
2 files changed+80 added−2 removed
modified:src/middleware/trailing-slash/index.test.ts+57 added
253 unmodified lines254 expect(loc.searchParams.get('exampleParam')).toBe('1')255 })256 })257 258+ describe('appendTrailingSlash middleware with skip option', () => {259+ const hasExtension = (path: string): boolean => /\.\w+$/.test(path.split('/').at(-1) ?? '')260+ const app = new Hono({ strict: true })261+ app.use('*', appendTrailingSlash({ skip: hasExtension }))262+263+ app.get('/page/', (c) => c.text('ok'))264+265+ it('should not redirect paths with file extensions', async () => {266+ const resp = await app.request('/foo.html')267+268+ expect(resp.status).toBe(404)269+ })270+271+ it('should redirect paths without file extensions', async () => {272+ const resp = await app.request('/page')273+274+ expect(resp.status).toBe(301)275+ const loc = new URL(resp.headers.get('location')!)276+ expect(loc.pathname).toBe('/page/')277+ })278+ })279+280+ describe('appendTrailingSlash middleware with alwaysRedirect and skip options', () => {281+ const hasExtension = (path: string): boolean => /\.\w+$/.test(path.split('/').at(-1) ?? '')282+ const app = new Hono()283+ app.use('*', appendTrailingSlash({ alwaysRedirect: true, skip: hasExtension }))284+285+ app.get('/', (c) => c.text('ok'))286+ app.get('/my-path/*', (c) => c.text('wildcard'))287+288+ it('should redirect paths without extensions', async () => {289+ const resp = await app.request('/my-path/something')290+ const loc = new URL(resp.headers.get('location')!)291+292+ expect(resp.status).toBe(301)293+ expect(loc.pathname).toBe('/my-path/something/')294+ })295+296+ it('should not redirect paths with file extensions', async () => {297+ const resp = await app.request('/my-path/style.css')298+299+ expect(resp.status).toBe(200)300+ })301+302+ it('should not redirect paths with extensions like .html', async () => {303+ const resp = await app.request('/my-path/page.html')304+305+ expect(resp.status).toBe(200)306+ })307+308+ it('should handle HEAD request for paths with extensions', async () => {309+ const resp = await app.request('/my-path/script.js', { method: 'HEAD' })310+311+ expect(resp.status).toBe(200)312+ })313+ })314+315 describe('appendTrailingSlash middleware with alwaysRedirect option', () => {316 const app = new Hono()317 app.use('*', appendTrailingSlash({ alwaysRedirect: true }))318 63 unmodified linesmodified:src/middleware/trailing-slash/index.ts+23 added-2 removed
80 unmodified lines8182838485868788899091929394959697989910010110210310422 unmodified lines1271281291301151311321331341351361371381395 unmodified lines1451461471481291491501511521531544 unmodified lines80 unmodified lines * 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.22 unmodified lines */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) !== '/') { 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)5 unmodified lines if ( !options?.alwaysRedirect && c.res.status === 404 && (c.req.method === 'GET' || c.req.method === 'HEAD') && c.req.path.at(-1) !== '/' c.req.path.at(-1) !== '/' && !options?.skip?.(c.req.path) ) { const url = new URL(c.req.url) url.pathname += '/'
4 unmodified lines