-
-
Notifications
You must be signed in to change notification settings - Fork 251
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Return alternate language links as headers from middleware (#195)
- Loading branch information
Showing
13 changed files
with
173 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
{ | ||
"Index": { | ||
"title": "Inicio", | ||
"description": "Esta es la página de inicio.", | ||
"rich": "Este es un texto <important>importante</important>.", | ||
"globalDefaults": "<highlight>{globalString}</highlight>" | ||
}, | ||
"LocaleLayout": { | ||
"title": "Ejemplo next-intl", | ||
"description": "Este es un ejemplo de cómo usar next-intl en el directorio app." | ||
}, | ||
"Client": { | ||
"title": "Cliente", | ||
"description": "Esta página se hidrata en el lado del cliente." | ||
}, | ||
"Nested": { | ||
"title": "Anidado", | ||
"description": "Esta es una página anidada." | ||
}, | ||
"Navigation": { | ||
"home": "Inicio", | ||
"client": "Página del cliente", | ||
"nested": "Página anidada" | ||
}, | ||
"NotFound": { | ||
"title": "Esta página no se encontró (404)" | ||
}, | ||
"LocaleSwitcher": { | ||
"switchLocale": "Cambiar a {locale, select, de {Alemán} en {Inglés} other {Desconocido}}" | ||
}, | ||
"Counter": { | ||
"count": "Conteo actual:", | ||
"increment": "Incrementar" | ||
}, | ||
"ClientCounter": { | ||
"count": "Conteo actual: {count}", | ||
"increment": "Incrementar" | ||
}, | ||
"ApiRoute": { | ||
"hello": "¡Hola {name}!" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export default function getHost(requestHeaders: Headers) { | ||
return ( | ||
requestHeaders.get('x-forwarded-host') ?? | ||
requestHeaders.get('host') ?? | ||
undefined | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export {default} from './middleware'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
packages/next-intl/src/middleware/setAlternateLinksHeader.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import {NextRequest, NextResponse} from 'next/server'; | ||
import NextIntlMiddlewareConfig from './NextIntlMiddlewareConfig'; | ||
|
||
function getUnprefixedPathname( | ||
config: NextIntlMiddlewareConfig, | ||
request: NextRequest | ||
) { | ||
const url = new URL(request.url); | ||
if (!url.pathname.endsWith('/')) { | ||
url.pathname += '/'; | ||
} | ||
|
||
url.pathname = url.pathname.replace( | ||
new RegExp(`^/(${config.locales.join('|')})/`), | ||
'/' | ||
); | ||
|
||
// Remove trailing slash | ||
if (url.pathname !== '/') { | ||
url.pathname = url.pathname.slice(0, -1); | ||
} | ||
|
||
return url.toString(); | ||
} | ||
|
||
function getAlternateEntry(url: string, locale: string) { | ||
return `<${url}>; rel="alternate"; hreflang="${locale}"`; | ||
} | ||
|
||
/** | ||
* See https://developers.google.com/search/docs/specialty/international/localized-versions | ||
*/ | ||
export default function setAlternateLinksHeader( | ||
config: NextIntlMiddlewareConfig, | ||
request: NextRequest, | ||
response: NextResponse | ||
) { | ||
const unprefixedPathname = getUnprefixedPathname(config, request); | ||
|
||
const links = config.locales.flatMap((locale) => { | ||
function localizePathname(url: URL) { | ||
if (url.pathname === '/') { | ||
url.pathname = `/${locale}`; | ||
} else { | ||
url.pathname = `/${locale}${url.pathname}`; | ||
} | ||
return url; | ||
} | ||
|
||
let url; | ||
|
||
const domainConfigs = | ||
config.domains?.filter((cur) => cur.defaultLocale === locale) || []; | ||
|
||
if (domainConfigs.length > 1) { | ||
// Prio 1: Configured domain(s) | ||
return domainConfigs.map((domainConfig) => { | ||
url = new URL(unprefixedPathname); | ||
url.host = domainConfig.domain; | ||
return getAlternateEntry(url.toString(), locale); | ||
}); | ||
} else { | ||
// Prio 2: Prefixed route | ||
url = new URL(unprefixedPathname); | ||
localizePathname(url); | ||
} | ||
|
||
return getAlternateEntry(url.toString(), locale); | ||
}); | ||
|
||
links.push(getAlternateEntry(unprefixedPathname, 'x-default')); | ||
|
||
response.headers.set('Link', links.join(', ')); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
f55424a
There was a problem hiding this comment.
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 – ./
next-intl-amann.vercel.app
next-intl-docs.vercel.app
next-intl-git-feat-next-13-rsc-amann.vercel.app