diff --git a/packages/next/next-server/lib/router/router.ts b/packages/next/next-server/lib/router/router.ts index 2782c5cb317b94a87ea41e63a117334dfd2dbefc..7d6f93d47d27e8c6e73dafb750c07bf7b00878b8 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 0000000000000000000000000000000000000000..ce3f975d0eac1f958cc34cb1100b7a64069181ba --- /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 0000000000000000000000000000000000000000..e16cd81e3bee63766ca72085401a353a8eb02d10 --- /dev/null +++ b/test/integration/trailing-slashes-href-resolving/pages/404.js @@ -0,0 +1,3 @@ +export default function NotFound() { + return
404
+} diff --git a/test/integration/trailing-slashes-href-resolving/pages/[slug].js b/test/integration/trailing-slashes-href-resolving/pages/[slug].js new file mode 100644 index 0000000000000000000000000000000000000000..f873b7ab9104ba0be3d54cdf7177eed0b2e6adc8 --- /dev/null +++ b/test/integration/trailing-slashes-href-resolving/pages/[slug].js @@ -0,0 +1,11 @@ +import { useRouter } from 'next/router' + +export default function Page() { + const router = useRouter() + + 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 0000000000000000000000000000000000000000..decf00835a7ca0486ca3c84a0608229a15844f22 --- /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 0000000000000000000000000000000000000000..f67b5c66bf71bd8a2a3f0464abac9019a8dcf29c --- /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 0000000000000000000000000000000000000000..04416504c581f9e989e64bae45a627498d76062e --- /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 0000000000000000000000000000000000000000..328deb70ac3f4544eba61d07fd7ad4e64eaa3e7e --- /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 0000000000000000000000000000000000000000..ef828deb443e1c154035c985d78ebd156153f040 --- /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 0000000000000000000000000000000000000000..46e6e099364b37cc9b72cff55b05b8abb4a22947 --- /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/ + +
+ + to /blog/first-post/ + +
+ + to /catch-all/hello/world/ + +
+ + to /catch-all/first/ + +
+ + to /another/ + +
+ + to /top-level-slug/ + +
+ + ) +} diff --git a/test/integration/trailing-slashes-href-resolving/test/index.test.js b/test/integration/trailing-slashes-href-resolving/test/index.test.js new file mode 100644 index 0000000000000000000000000000000000000000..eb4811538b44fd0cf3ddd8a089a69a1298b54998 --- /dev/null +++ b/test/integration/trailing-slashes-href-resolving/test/index.test.js @@ -0,0 +1,98 @@ +/* eslint-env jest */ + +import { join } from 'path' +import webdriver from 'next-webdriver' +import { + findPort, + killApp, + launchApp, + nextBuild, + nextStart, +} from 'next-test-utils' + +jest.setTimeout(1000 * 60 * 2) + +let app +let appPort +const appDir = join(__dirname, '../') + +const runTests = () => { + it('should route to /blog/another/ correctly', async () => { + const browser = await webdriver(appPort, '/') + await browser.elementByCss('#to-blog-another').click() + + await browser.waitForElementByCss('#another') + expect(await browser.elementByCss('#another').text()).toBe('blog another') + }) + + it('should route to /blog/first-post/ correctly', async () => { + const browser = await webdriver(appPort, '/') + await browser.elementByCss('#to-blog-post').click() + + await browser.waitForElementByCss('#slug') + expect(await browser.elementByCss('#slug').text()).toBe( + 'blog slug first-post' + ) + }) + + it('should route to /catch-all/hello/world/ correctly', async () => { + const browser = await webdriver(appPort, '/') + await browser.elementByCss('#to-catch-all-item').click() + + await browser.waitForElementByCss('#slug') + expect(await browser.elementByCss('#slug').text()).toBe( + 'catch-all slug hello/world' + ) + }) + + it('should route to /catch-all/first/ correctly', async () => { + const browser = await webdriver(appPort, '/') + await browser.elementByCss('#to-catch-all-first').click() + + await browser.waitForElementByCss('#first') + expect(await browser.elementByCss('#first').text()).toBe('catch-all first') + }) + + it('should route to /another/ correctly', async () => { + const browser = await webdriver(appPort, '/') + await browser.elementByCss('#to-another').click() + + await browser.waitForElementByCss('#another') + expect(await browser.elementByCss('#another').text()).toBe( + 'top level another' + ) + }) + + it('should route to /top-level-slug/ correctly', async () => { + const browser = await webdriver(appPort, '/') + await browser.elementByCss('#to-slug').click() + + await browser.waitForElementByCss('#slug') + expect(await browser.elementByCss('#slug').text()).toBe( + 'top level slug top-level-slug' + ) + }) +} + +describe('href resolving trailing-slash', () => { + describe('dev mode', () => { + beforeAll(async () => { + appPort = await findPort() + app = await launchApp(appDir, appPort) + }) + afterAll(() => killApp(app)) + + runTests() + }) + + describe('production mode', () => { + beforeAll(async () => { + await nextBuild(appDir) + appPort = await findPort() + app = await nextStart(appDir, appPort) + }) + afterAll(() => killApp(app)) + + runTests() + }) +})