From 42ea5aa18c90457a372d6a93270c0bffc49da3b2 Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Wed, 16 Jun 2021 14:45:12 -0500 Subject: [PATCH] Fix long URLs causing 400s with dynamic routes/rewrites --- packages/next/lib/file-exists.ts | 2 +- test/integration/custom-routes/test/index.test.js | 11 +++++++++++ .../dynamic-routing/pages/dash/[hello-world].js | 4 +++- test/integration/dynamic-routing/test/index.test.js | 12 ++++++++++++ .../production/pages/invalid-param/[slug].js | 2 +- 5 files changed, 28 insertions(+), 3 deletions(-) diff --git a/packages/next/lib/file-exists.ts b/packages/next/lib/file-exists.ts index fc60cdcf8e30f..b3e49b1624a8b 100644 --- a/packages/next/lib/file-exists.ts +++ b/packages/next/lib/file-exists.ts @@ -16,7 +16,7 @@ export async function fileExists( } return true } catch (err) { - if (err.code === 'ENOENT') { + if (err.code === 'ENOENT' || err.code === 'ENAMETOOLONG') { return false } throw err diff --git a/test/integration/custom-routes/test/index.test.js b/test/integration/custom-routes/test/index.test.js index 9453a19eb5fd5..3fce6591dedb3 100644 --- a/test/integration/custom-routes/test/index.test.js +++ b/test/integration/custom-routes/test/index.test.js @@ -39,6 +39,17 @@ let appPort let app const runTests = (isDev = false) => { + it('should support long URLs for rewrites', async () => { + const res = await fetchViaHTTP( + appPort, + '/catchall-rewrite/a9btBxtHQALZ6cxfuj18X6OLGNSkJVzrOXz41HG4QwciZfn7ggRZzPx21dWqGiTBAqFRiWvVNm5ko2lpyso5jtVaXg88dC1jKfqI2qmIcdeyJat8xamrIh2LWnrYRrsBcoKfQU65KHod8DPANuzPS3fkVYWlmov05GQbc82HwR1exOvPVKUKb5gBRWiN0WOh7hN4QyezIuq3dJINAptFQ6m2bNGjYACBRk4MOSHdcQG58oq5Ch7luuqrl9EcbWSa' + ) + + const html = await res.text() + expect(res.status).toBe(200) + expect(html).toContain('/with-params') + }) + it('should resolveHref correctly navigating through history', async () => { const browser = await webdriver(appPort, '/') await browser.eval('window.beforeNav = 1') diff --git a/test/integration/dynamic-routing/pages/dash/[hello-world].js b/test/integration/dynamic-routing/pages/dash/[hello-world].js index 0957a987fc2f2..2468ab1dc7171 100644 --- a/test/integration/dynamic-routing/pages/dash/[hello-world].js +++ b/test/integration/dynamic-routing/pages/dash/[hello-world].js @@ -1 +1,3 @@ -export default () => 'hi' +export default function Page(props) { + return 'hi' +} diff --git a/test/integration/dynamic-routing/test/index.test.js b/test/integration/dynamic-routing/test/index.test.js index ac8f7842a0d60..754dc8e366f31 100644 --- a/test/integration/dynamic-routing/test/index.test.js +++ b/test/integration/dynamic-routing/test/index.test.js @@ -30,6 +30,18 @@ const appDir = join(__dirname, '../') const buildIdPath = join(appDir, '.next/BUILD_ID') function runTests(dev) { + it('should support long URLs for dynamic routes', async () => { + const res = await fetchViaHTTP( + appPort, + '/dash/a9btBxtHQALZ6cxfuj18X6OLGNSkJVzrOXz41HG4QwciZfn7ggRZzPx21dWqGiTBAqFRiWvVNm5ko2lpyso5jtVaXg88dC1jKfqI2qmIcdeyJat8xamrIh2LWnrYRrsBcoKfQU65KHod8DPANuzPS3fkVYWlmov05GQbc82HwR1exOvPVKUKb5gBRWiN0WOh7hN4QyezIuq3dJINAptFQ6m2bNGjYACBRk4MOSHdcQG58oq5Ch7luuqrl9EcbWSa' + ) + + const html = await res.text() + expect(res.status).toBe(200) + expect(html).toContain('hi') + expect(html).toContain('/dash/[hello-world]') + }) + it('should handle only query on dynamic route', async () => { const browser = await webdriver(appPort, '/post-1') diff --git a/test/integration/production/pages/invalid-param/[slug].js b/test/integration/production/pages/invalid-param/[slug].js index 9f287061d65fc..36b5e610a9331 100644 --- a/test/integration/production/pages/invalid-param/[slug].js +++ b/test/integration/production/pages/invalid-param/[slug].js @@ -1,7 +1,7 @@ import { useRouter } from 'next/router' export default function Page() { - return

hello {useRouter().query}

+ return

hello {useRouter().query.slug}

} export const getServerSideProps = () => {