Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: properly handle trailingSlash: true and rewrites #43641

Merged
merged 2 commits into from Dec 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion packages/next/shared/lib/router/adapters.tsx
Expand Up @@ -117,7 +117,14 @@ export function PathnameContextProviderAdapter({
// any query strings), so it should have that stripped. Read more about the
// `asPath` option over at:
// https://nextjs.org/docs/api-reference/next/router#router-object
const url = new URL(router.asPath, 'http://f')
let url: URL
try {
url = new URL(router.asPath, 'http://f')
} catch (_) {
// fallback to / for invalid asPath values e.g. //
return '/'
}

return url.pathname
}, [router.asPath, router.isFallback, router.isReady, router.pathname])

Expand Down
6 changes: 6 additions & 0 deletions test/e2e/trailingslash-with-rewrite/app/next.config.js
@@ -0,0 +1,6 @@
module.exports = {
trailingSlash: true,
async rewrites() {
return [{ source: '/country/', destination: '/' }]
},
}
7 changes: 7 additions & 0 deletions test/e2e/trailingslash-with-rewrite/app/pages/index.js
@@ -0,0 +1,7 @@
export default function Home() {
return <p>Welcome home</p>
}

export async function getStaticProps() {
return { props: {} }
}
25 changes: 25 additions & 0 deletions test/e2e/trailingslash-with-rewrite/index.test.ts
@@ -0,0 +1,25 @@
import { join } from 'path'
import { createNext, FileRef } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base'
import { fetchViaHTTP } from 'next-test-utils'

describe('trailingSlash:true with rewrites and getStaticProps', () => {
let next: NextInstance

if ((global as any).isNextDeploy) {
it('should skip for deploy mode for now', () => {})
return
}

beforeAll(async () => {
next = await createNext({
files: new FileRef(join(__dirname, './app')),
})
})
afterAll(() => next.destroy())

it('should work', async () => {
const res = await fetchViaHTTP(next.url, '/country')
expect(await res.text()).toContain('Welcome home')
})
})