Unverified Commit c88872ce authored by Matsumoto Toshi's avatar Matsumoto Toshi Committed by GitHub

fix: issue #18338 - don't add a trailing slash to external links (#18595)

## Description

Fixes #18338 - Fixed an issue with `trailingSlash: true` adding a slash to the end of an external link.

After
```
<h1 class="Home_title__3DjR7">Welcome to <a href="https://nextjs.org">Without slash</a></h1>
<h1 class="Home_title__3DjR7">Welcome to <a href="https://nextjs.org">With slash?</a></h1>
```

Before
```
<h1 class="Home_title__3DjR7">Welcome to <a href="https://nextjs.org">Without slash</a></h1>
<h1 class="Home_title__3DjR7">Welcome to <a href="https://nextjs.org/">With slash?</a></h1>
```
parent 6c282a3d
...@@ -197,6 +197,10 @@ export function resolveHref( ...@@ -197,6 +197,10 @@ export function resolveHref(
const base = new URL(currentPath, 'http://n') const base = new URL(currentPath, 'http://n')
const urlAsString = const urlAsString =
typeof href === 'string' ? href : formatWithValidation(href) typeof href === 'string' ? href : formatWithValidation(href)
// Return because it cannot be routed by the Next.js router
if (!isLocalURL(urlAsString)) {
return (resolveAs ? [urlAsString] : urlAsString) as string
}
try { try {
const finalUrl = new URL(urlAsString, base) const finalUrl = new URL(urlAsString, base)
finalUrl.pathname = normalizePathTrailingSlash(finalUrl.pathname) finalUrl.pathname = normalizePathTrailingSlash(finalUrl.pathname)
......
import Link from 'next/link'
import { useRouter } from 'next/router'
export async function getServerSideProps({ query }) {
return {
props: { href: decodeURI(query.href) || '/' },
}
}
export default function ExternalLinker({ href }) {
const router = useRouter()
const pushRoute = () => {
router.push(href)
}
return (
<div>
<Link href={href}>
<a id="link">link to {href}</a>
</Link>
<button id="route-pusher" onClick={pushRoute}>
push route {href}
</button>
</div>
)
}
...@@ -68,6 +68,17 @@ function testShouldResolve(expectations) { ...@@ -68,6 +68,17 @@ function testShouldResolve(expectations) {
) )
} }
function testExternalLinkShouldRewriteTo(expectations) {
it.each(expectations)(
'%s should have href %s',
async (linkPage, expectedHref) => {
const content = await renderViaHTTP(appPort, linkPage)
const $ = cheerio.load(content)
expect($('#link').attr('href')).toBe(expectedHref)
}
)
}
function testLinkShouldRewriteTo(expectations) { function testLinkShouldRewriteTo(expectations) {
it.each(expectations)( it.each(expectations)(
'%s should have href %s', '%s should have href %s',
...@@ -143,6 +154,17 @@ function testWithoutTrailingSlash() { ...@@ -143,6 +154,17 @@ function testWithoutTrailingSlash() {
['/linker?href=/catch-all/hello/', '/catch-all/hello'], ['/linker?href=/catch-all/hello/', '/catch-all/hello'],
['/linker?href=/catch-all/hello.world/', '/catch-all/hello.world'], ['/linker?href=/catch-all/hello.world/', '/catch-all/hello.world'],
]) ])
testExternalLinkShouldRewriteTo([
[
`/external-linker?href=${encodeURI('https://nextjs.org')}`,
'https://nextjs.org',
],
[
`/external-linker?href=${encodeURI('https://nextjs.org/')}`,
'https://nextjs.org/',
],
])
} }
function testWithTrailingSlash() { function testWithTrailingSlash() {
...@@ -173,6 +195,17 @@ function testWithTrailingSlash() { ...@@ -173,6 +195,17 @@ function testWithTrailingSlash() {
['/linker?href=/catch-all/hello/', '/catch-all/hello/'], ['/linker?href=/catch-all/hello/', '/catch-all/hello/'],
['/linker?href=/catch-all/hello.world/', '/catch-all/hello.world'], ['/linker?href=/catch-all/hello.world/', '/catch-all/hello.world'],
]) ])
testExternalLinkShouldRewriteTo([
[
`/external-linker?href=${encodeURI('https://nextjs.org')}`,
'https://nextjs.org',
],
[
`/external-linker?href=${encodeURI('https://nextjs.org/')}`,
'https://nextjs.org/',
],
])
} }
describe('Trailing slashes', () => { describe('Trailing slashes', () => {
......
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