Unverified Commit 9c69c838 authored by Zack Tanner's avatar Zack Tanner Committed by GitHub

remove locale from app link (#73052)

This prop is a no-op in app router. This removes the code that handled
it for the App Router link, and adds a warning for apps that are
attempting to use it in an app router context.
parent 8ea2830b
...@@ -13,6 +13,7 @@ import { PrefetchKind } from '../components/router-reducer/router-reducer-types' ...@@ -13,6 +13,7 @@ import { PrefetchKind } from '../components/router-reducer/router-reducer-types'
import { useMergedRef } from '../use-merged-ref' import { useMergedRef } from '../use-merged-ref'
import { isAbsoluteUrl } from '../../shared/lib/utils' import { isAbsoluteUrl } from '../../shared/lib/utils'
import { addBasePath } from '../add-base-path' import { addBasePath } from '../add-base-path'
import { warnOnce } from '../../shared/lib/utils/warn-once'
type Url = string | UrlObject type Url = string | UrlObject
type RequiredKeys<T> = { type RequiredKeys<T> = {
...@@ -78,6 +79,7 @@ type InternalLinkProps = { ...@@ -78,6 +79,7 @@ type InternalLinkProps = {
/** /**
* The active locale is automatically prepended. `locale` allows for providing a different locale. * The active locale is automatically prepended. `locale` allows for providing a different locale.
* When `false` `href` has to include the locale as the default behavior is disabled. * When `false` `href` has to include the locale as the default behavior is disabled.
* Note: This is only available in the Pages Router.
*/ */
locale?: string | false locale?: string | false
/** /**
...@@ -108,7 +110,7 @@ type InternalLinkProps = { ...@@ -108,7 +110,7 @@ type InternalLinkProps = {
// eslint-disable-next-line @typescript-eslint/no-unused-vars // eslint-disable-next-line @typescript-eslint/no-unused-vars
export type LinkProps<RouteInferType = any> = InternalLinkProps export type LinkProps<RouteInferType = any> = InternalLinkProps
type LinkPropsRequired = RequiredKeys<LinkProps> type LinkPropsRequired = RequiredKeys<LinkProps>
type LinkPropsOptional = OptionalKeys<InternalLinkProps> type LinkPropsOptional = OptionalKeys<Omit<InternalLinkProps, 'locale'>>
function prefetch( function prefetch(
router: AppRouterInstance, router: AppRouterInstance,
...@@ -157,8 +159,7 @@ function linkClicked( ...@@ -157,8 +159,7 @@ function linkClicked(
as: string, as: string,
replace?: boolean, replace?: boolean,
shallow?: boolean, shallow?: boolean,
scroll?: boolean, scroll?: boolean
locale?: string | false
): void { ): void {
const { nodeName } = e.currentTarget const { nodeName } = e.currentTarget
...@@ -178,7 +179,6 @@ function linkClicked( ...@@ -178,7 +179,6 @@ function linkClicked(
if ('beforePopState' in router) { if ('beforePopState' in router) {
router[replace ? 'replace' : 'push'](href, as, { router[replace ? 'replace' : 'push'](href, as, {
shallow, shallow,
locale,
scroll: routerScroll, scroll: routerScroll,
}) })
} else { } else {
...@@ -225,7 +225,6 @@ const Link = React.forwardRef<HTMLAnchorElement, LinkPropsReal>( ...@@ -225,7 +225,6 @@ const Link = React.forwardRef<HTMLAnchorElement, LinkPropsReal>(
replace, replace,
shallow, shallow,
scroll, scroll,
locale,
onClick, onClick,
onMouseEnter: onMouseEnterProp, onMouseEnter: onMouseEnterProp,
onTouchStart: onTouchStartProp, onTouchStart: onTouchStartProp,
...@@ -302,7 +301,6 @@ const Link = React.forwardRef<HTMLAnchorElement, LinkPropsReal>( ...@@ -302,7 +301,6 @@ const Link = React.forwardRef<HTMLAnchorElement, LinkPropsReal>(
shallow: true, shallow: true,
passHref: true, passHref: true,
prefetch: true, prefetch: true,
locale: true,
onClick: true, onClick: true,
onMouseEnter: true, onMouseEnter: true,
onTouchStart: true, onTouchStart: true,
...@@ -322,14 +320,6 @@ const Link = React.forwardRef<HTMLAnchorElement, LinkPropsReal>( ...@@ -322,14 +320,6 @@ const Link = React.forwardRef<HTMLAnchorElement, LinkPropsReal>(
actual: valType, actual: valType,
}) })
} }
} else if (key === 'locale') {
if (props[key] && valType !== 'string') {
throw createPropError({
key,
expected: '`string`',
actual: valType,
})
}
} else if ( } else if (
key === 'onClick' || key === 'onClick' ||
key === 'onMouseEnter' || key === 'onMouseEnter' ||
...@@ -366,6 +356,11 @@ const Link = React.forwardRef<HTMLAnchorElement, LinkPropsReal>( ...@@ -366,6 +356,11 @@ const Link = React.forwardRef<HTMLAnchorElement, LinkPropsReal>(
} }
if (process.env.NODE_ENV !== 'production') { if (process.env.NODE_ENV !== 'production') {
if (props.locale) {
warnOnce(
'The `locale` prop is not supported in `next/link` while using the `app` router. Read more about app router internalization: https://nextjs.org/docs/app/building-your-application/routing/internationalization'
)
}
if (!asProp) { if (!asProp) {
let href: string | undefined let href: string | undefined
if (typeof hrefProp === 'string') { if (typeof hrefProp === 'string') {
...@@ -488,7 +483,7 @@ const Link = React.forwardRef<HTMLAnchorElement, LinkPropsReal>( ...@@ -488,7 +483,7 @@ const Link = React.forwardRef<HTMLAnchorElement, LinkPropsReal>(
prefetch(router, href, { prefetch(router, href, {
kind: appPrefetchKind, kind: appPrefetchKind,
}) })
}, [as, href, isVisible, locale, prefetchEnabled, router, appPrefetchKind]) }, [as, href, isVisible, prefetchEnabled, router, appPrefetchKind])
const childProps: { const childProps: {
onTouchStart?: React.TouchEventHandler<HTMLAnchorElement> onTouchStart?: React.TouchEventHandler<HTMLAnchorElement>
...@@ -527,7 +522,7 @@ const Link = React.forwardRef<HTMLAnchorElement, LinkPropsReal>( ...@@ -527,7 +522,7 @@ const Link = React.forwardRef<HTMLAnchorElement, LinkPropsReal>(
return return
} }
linkClicked(e, router, href, as, replace, shallow, scroll, locale) linkClicked(e, router, href, as, replace, shallow, scroll)
}, },
onMouseEnter(e) { onMouseEnter(e) {
if (!legacyBehavior && typeof onMouseEnterProp === 'function') { if (!legacyBehavior && typeof onMouseEnterProp === 'function') {
...@@ -585,7 +580,7 @@ const Link = React.forwardRef<HTMLAnchorElement, LinkPropsReal>( ...@@ -585,7 +580,7 @@ const Link = React.forwardRef<HTMLAnchorElement, LinkPropsReal>(
// If child is an <a> tag and doesn't have a href attribute, or if the 'passHref' property is // If child is an <a> tag and doesn't have a href attribute, or if the 'passHref' property is
// defined, we specify the current 'href', so that repetition is not needed by the user. // defined, we specify the current 'href', so that repetition is not needed by the user.
// If the url is absolute, we can bypass the logic to prepend the domain and locale. // If the url is absolute, we can bypass the logic to prepend the basePath.
if (isAbsoluteUrl(as)) { if (isAbsoluteUrl(as)) {
childProps.href = as childProps.href = as
} else if ( } else if (
......
...@@ -83,6 +83,7 @@ type InternalLinkProps = { ...@@ -83,6 +83,7 @@ type InternalLinkProps = {
/** /**
* The active locale is automatically prepended. `locale` allows for providing a different locale. * The active locale is automatically prepended. `locale` allows for providing a different locale.
* When `false` `href` has to include the locale as the default behavior is disabled. * When `false` `href` has to include the locale as the default behavior is disabled.
* Note: This is only available in the Pages Router.
*/ */
locale?: string | false locale?: string | false
/** /**
......
import Link from 'next/link'
export default function Page() {
return (
<>
<Link href="/about" locale="en">
Link 1
</Link>
<Link href="/about" locale="fr">
Link 2
</Link>
</>
)
}
...@@ -974,4 +974,27 @@ describe('app dir - navigation', () => { ...@@ -974,4 +974,27 @@ describe('app dir - navigation', () => {
) )
}) })
}) })
if (isNextDev) {
describe('locale warnings', () => {
it('should warn about using the `locale` prop with `next/link` in app router', async () => {
const browser = await next.browser('/locale-app')
const logs = await browser.log()
expect(logs).toContainEqual(
expect.objectContaining({
message: expect.stringContaining(
'The `locale` prop is not supported in `next/link` while using the `app` router.'
),
source: 'warning',
})
)
})
it('should have no warnings in pages router', async () => {
const browser = await next.browser('/locale-pages')
const logs = await browser.log()
expect(logs.filter((log) => log.source === 'warning')).toHaveLength(0)
})
})
}
}) })
import Link from 'next/link'
export default function Page() {
return (
<>
<Link href="/about" locale="en">
Link 1
</Link>
<Link href="/about" locale="fr">
Link 2
</Link>
</>
)
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment