Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure non-matching API routes can be rewritten (#33966)
This ensures non-matching API routes can be rewritten with i18n configured as currently we bail and render the 404 page when a locale prefixed API route is requested. ## Bug - [x] Related issues linked using `fixes #number` - [x] Integration tests added - [ ] Errors have helpful link attached, see `contributing.md` x-ref: [slack thread](https://vercel.slack.com/archives/CGU8HUTUH/p1643930049224689) closes: #28921
- Loading branch information
Showing
3 changed files
with
83 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { createNext } from 'e2e-utils' | ||
import { fetchViaHTTP } from 'next-test-utils' | ||
import { NextInstance } from 'test/lib/next-modes/base' | ||
|
||
describe('i18n API support', () => { | ||
let next: NextInstance | ||
|
||
beforeAll(async () => { | ||
next = await createNext({ | ||
files: { | ||
'pages/api/hello.js': ` | ||
export default function handler(req, res) { | ||
res.end('hello world') | ||
} | ||
`, | ||
'pages/api/blog/[slug].js': ` | ||
export default function handler(req, res) { | ||
res.end('blog/[slug]') | ||
} | ||
`, | ||
}, | ||
nextConfig: { | ||
i18n: { | ||
locales: ['en', 'fr'], | ||
defaultLocale: 'en', | ||
}, | ||
async rewrites() { | ||
return { | ||
beforeFiles: [], | ||
afterFiles: [], | ||
fallback: [ | ||
{ | ||
source: '/api/:path*', | ||
destination: 'https://example.vercel.sh/', | ||
}, | ||
], | ||
} | ||
}, | ||
}, | ||
dependencies: {}, | ||
}) | ||
}) | ||
afterAll(() => next.destroy()) | ||
|
||
it('should respond to normal API request', async () => { | ||
const res = await fetchViaHTTP(next.url, '/api/hello') | ||
expect(res.status).toBe(200) | ||
expect(await res.text()).toBe('hello world') | ||
}) | ||
|
||
it('should respond to normal dynamic API request', async () => { | ||
const res = await fetchViaHTTP(next.url, '/api/blog/first') | ||
expect(res.status).toBe(200) | ||
expect(await res.text()).toBe('blog/[slug]') | ||
}) | ||
|
||
it('should fallback rewrite non-matching API request', async () => { | ||
const paths = [ | ||
'/fr/api/hello', | ||
'/en/api/blog/first', | ||
'/en/api/non-existent', | ||
'/api/non-existent', | ||
] | ||
|
||
for (const path of paths) { | ||
const res = await fetchViaHTTP(next.url, path) | ||
expect(res.status).toBe(200) | ||
expect(await res.text()).toContain('Example Domain') | ||
} | ||
}) | ||
}) |