diff --git a/packages/next/next-server/lib/router/router.ts b/packages/next/next-server/lib/router/router.ts index 2782c5cb317b9..7d6f93d47d27e 100644 --- a/packages/next/next-server/lib/router/router.ts +++ b/packages/next/next-server/lib/router/router.ts @@ -922,7 +922,9 @@ export default class Router implements BaseRouter { _resolveHref(parsedHref: UrlObject, pages: string[]) { const { pathname } = parsedHref - const cleanPathname = denormalizePagePath(delBasePath(pathname!)) + const cleanPathname = removePathTrailingSlash( + denormalizePagePath(delBasePath(pathname!)) + ) if (cleanPathname === '/404' || cleanPathname === '/_error') { return parsedHref diff --git a/test/integration/trailing-slashes-href-resolving/next.config.js b/test/integration/trailing-slashes-href-resolving/next.config.js new file mode 100644 index 0000000000000..ce3f975d0eac1 --- /dev/null +++ b/test/integration/trailing-slashes-href-resolving/next.config.js @@ -0,0 +1,3 @@ +module.exports = { + trailingSlash: true, +} diff --git a/test/integration/trailing-slashes-href-resolving/pages/404.js b/test/integration/trailing-slashes-href-resolving/pages/404.js new file mode 100644 index 0000000000000..e16cd81e3bee6 --- /dev/null +++ b/test/integration/trailing-slashes-href-resolving/pages/404.js @@ -0,0 +1,3 @@ +export default function NotFound() { + return
top level slug {router.query.slug}
+ > + ) +} diff --git a/test/integration/trailing-slashes-href-resolving/pages/another.js b/test/integration/trailing-slashes-href-resolving/pages/another.js new file mode 100644 index 0000000000000..decf00835a7ca --- /dev/null +++ b/test/integration/trailing-slashes-href-resolving/pages/another.js @@ -0,0 +1,7 @@ +export default function Page() { + return ( + <> +top level another
+ > + ) +} diff --git a/test/integration/trailing-slashes-href-resolving/pages/blog/[slug].js b/test/integration/trailing-slashes-href-resolving/pages/blog/[slug].js new file mode 100644 index 0000000000000..f67b5c66bf71b --- /dev/null +++ b/test/integration/trailing-slashes-href-resolving/pages/blog/[slug].js @@ -0,0 +1,11 @@ +import { useRouter } from 'next/router' + +export default function Page() { + const router = useRouter() + + return ( + <> +blog slug {router.query.slug}
+ > + ) +} diff --git a/test/integration/trailing-slashes-href-resolving/pages/blog/another.js b/test/integration/trailing-slashes-href-resolving/pages/blog/another.js new file mode 100644 index 0000000000000..04416504c581f --- /dev/null +++ b/test/integration/trailing-slashes-href-resolving/pages/blog/another.js @@ -0,0 +1,7 @@ +export default function Page() { + return ( + <> +blog another
+ > + ) +} diff --git a/test/integration/trailing-slashes-href-resolving/pages/catch-all/[...slug].js b/test/integration/trailing-slashes-href-resolving/pages/catch-all/[...slug].js new file mode 100644 index 0000000000000..328deb70ac3f4 --- /dev/null +++ b/test/integration/trailing-slashes-href-resolving/pages/catch-all/[...slug].js @@ -0,0 +1,11 @@ +import { useRouter } from 'next/router' + +export default function Page() { + const router = useRouter() + + return ( + <> +catch-all slug {router.query.slug?.join('/')}
+ > + ) +} diff --git a/test/integration/trailing-slashes-href-resolving/pages/catch-all/first.js b/test/integration/trailing-slashes-href-resolving/pages/catch-all/first.js new file mode 100644 index 0000000000000..ef828deb443e1 --- /dev/null +++ b/test/integration/trailing-slashes-href-resolving/pages/catch-all/first.js @@ -0,0 +1,7 @@ +export default function Page() { + return ( + <> +catch-all first
+ > + ) +} diff --git a/test/integration/trailing-slashes-href-resolving/pages/index.js b/test/integration/trailing-slashes-href-resolving/pages/index.js new file mode 100644 index 0000000000000..46e6e099364b3 --- /dev/null +++ b/test/integration/trailing-slashes-href-resolving/pages/index.js @@ -0,0 +1,32 @@ +import Link from 'next/link' + +export default function Index() { + return ( + <> + + to /blog/another/ + +