Skip to content

Commit

Permalink
docs(trailing-slash): add Trailing Slash Middleware (honojs#284)
Browse files Browse the repository at this point in the history
  • Loading branch information
yusukebe authored Mar 31, 2024
1 parent c01e8df commit 4d5733d
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,12 @@ const sidebars = (): DefaultTheme.SidebarItem[] => [
{ text: 'ETag', link: '/middleware/builtin/etag' },
{ text: 'JSX Renderer', link: '/middleware/builtin/jsx-renderer' },
{ text: 'JWT', link: '/middleware/builtin/jwt' },
{ text: 'Timing', link: '/middleware/builtin/timing' },
{ text: 'Logger', link: '/middleware/builtin/logger' },
{ text: 'Method Override', link: '/middleware/builtin/method-override' },
{ text: 'Pretty JSON', link: '/middleware/builtin/pretty-json' },
{ text: 'Secure Headers', link: '/middleware/builtin/secure-headers' },
{ text: 'Timing', link: '/middleware/builtin/timing' },
{ text: 'Trailing Slash', link: '/middleware/builtin/trailing-slash' },
{ text: '3rd-party Middleware', link: '/middleware/third-party' },
],
},
Expand Down
51 changes: 51 additions & 0 deletions middleware/builtin/trailing-slash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Tailing Slash Middleware

This middleware resolves the handling of Trailing Slashes in GET requests.

`appendTrailingSlash` redirects to the URL to which it added the Tailing Slash if the content was not found. Also, `trimTrailingSlash` will remove the Tailing Slash.

## Import

::: code-group

```ts [npm]
import { Hono } from 'hono'
import { appendTrailingSlash, trimTrailingSlash } from 'hono/trailing-slash'
```

```ts [Deno]
import { Hono } from 'https://deno.land/x/hono/mod.ts'
import { appendTrailingSlash, trimTrailingSlash } from 'https://deno.land/x/hono/middleware.ts'
```

:::

## Usage

Example of redirecting a GET request to `/about/me` to `/about/me/`.

```ts
import { Hono } from 'hono'
import { appendTrailingSlash } from 'hono/trailing-slash'

const app = new Hono({ strict: true })

app.use(appendTrailingSlash())
app.get('/about/me/', (c) => c.text('With Trailing Slash'))
```

Example of redirecting a GET request to `/about/me/` to `/about/me`.

```ts
import { Hono } from 'hono'
import { trimTrailingSlash } from 'hono/trailing-slash'

const app = new Hono({ strict: true })

app.use(trimTrailingSlash())
app.get('/about/me', (c) => c.text('Without Trailing Slash'))
```

## Note

It will be enabled when the request method is `GET` and the response status is `404`.

0 comments on commit 4d5733d

Please sign in to comment.