Skip to content

Commit

Permalink
docs: "Can I customize the alternate links?"
Browse files Browse the repository at this point in the history
  • Loading branch information
amannn committed Dec 14, 2023
1 parent dbe1927 commit 4572f96
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ There are several ways you can contribute:
1. **Helping others by answering questions**: Developers often seek guidance by posting questions in [issues](https://github.com/amannn/next-intl/issues), [discussions](https://github.com/amannn/next-intl/discussions), and [on Stack Overflow](https://stackoverflow.com/search?q=%22next-intl%22). Your responses, based on your experience, are greatly appreciated and help foster a supportive community.
2. **Docs**: We focus on documentation as much as we do on the code itself. We strive to create helpful learning resources that efficiently communicate concepts and address common user issues. If you encounter areas that need clarification or have ideas to improve our documentation, we welcome and appreciate your contributions!
3. **Bug fixes**: Fixing a bug always starts with creating a regression test that reliably reproduces the broken behavior. This alone is a valuable contribution and can be submitted as a pull request. If you're up for the challenge, feel free to provide the bug fix yourself!
4. **New features**: We encourage proposing feature ideas as issues prior to starting development. This practice helps to avoid duplicated efforts and allows us to align on the direction before investing significant development time.
4. **New features**: We encourage proposing feature ideas as issues prior to starting development. This practice helps to avoid duplicated efforts and allows us to align on the direction before investing significant development time. Note that features for exotic use cases that can already be achieved with the current feature set are unlikely to get merged, as the maintainence of such features over time takes significant effort.

Open source work should be fun for everyone involved. Let's make sure it stays that way! Our communication style aims to be clear and friendly. We value empathy, respect, and understanding different perspectives.

Expand Down
32 changes: 30 additions & 2 deletions docs/pages/docs/routing/middleware.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,34 @@ export default createMiddleware({
});
```

<details>
<summary>Can I customize the alternate links?</summary>

The alternate links can either be turned off or on, depending on the `alternateLinks` option.

If you need to customize the alternate links, you can either turn them off and provide your own implementation, or if you only need to make minor adaptions, you can [compose the middleware](#composing-other-middlewares) and add your custom logic after the middleware has run:

```tsx filename="middleware.ts"
import createIntlMiddleware from 'next-intl/middleware';
import LinkHeader from 'http-link-header';
import {NextRequest} from 'next/server';

const handleI18nRouting = createIntlMiddleware(/* ... */);

export default async function middleware(request: NextRequest) {
const response = handleI18nRouting(request);

// Example: Remove the `x-default` entry
const link = LinkHeader.parse(response.headers.get('link'));
link.refs = link.refs.filter((entry) => entry.hreflang !== 'x-default');
response.headers.set('link', link.toString());

return response;
}
```

</details>

### Localizing pathnames

Many apps choose to localize pathnames, especially when search engine optimization is relevant, e.g.:
Expand Down Expand Up @@ -381,7 +409,7 @@ import {NextRequest} from 'next/server';

export default async function middleware(request: NextRequest) {
// Step 1: Use the incoming request (example)
const defaultLocale = request.headers.get('x-default-locale') || 'en';
const defaultLocale = request.headers.get('x-your-custom-locale') || 'en';

// Step 2: Create and call the next-intl middleware (example)
const handleI18nRouting = createIntlMiddleware({
Expand All @@ -391,7 +419,7 @@ export default async function middleware(request: NextRequest) {
const response = handleI18nRouting(request);

// Step 3: Alter the response (example)
response.headers.set('x-default-locale', defaultLocale);
response.headers.set('x-your-custom-locale', defaultLocale);

return response;
}
Expand Down

2 comments on commit 4572f96

@vercel
Copy link

@vercel vercel bot commented on 4572f96 Dec 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 4572f96 Dec 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

next-intl-docs – ./docs

next-intl-docs.vercel.app
next-intl-docs-git-main-next-intl.vercel.app
next-intl-docs-next-intl.vercel.app

Please sign in to comment.