Unverified Commit 40abd708 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Show better error when non-array is returned from custom-routes (#10670)

* Show better error when non-array is returned from custom-routes

* bump
Co-authored-by: 's avatarJoe Haddad <timer150@gmail.com>
parent 0ee18741
# Custom Routes must return an array
#### Why This Error Occurred
When defining custom routes an array wasn't returned from either `headers`, `rewrites`, or `redirects`.
#### Possible Ways to Fix It
Make sure to return an array that contains the routes.
**Before**
```js
// next.config.js
module.exports = {
experimental: {
async rewrites() {
return {
source: '/feedback',
destination: '/feedback/general',
}
},
},
}
```
**After**
```js
module.exports = {
experimental: {
async rewrites() {
return [
{
source: '/feedback',
destination: '/feedback/general',
},
]
},
},
}
```
...@@ -113,21 +113,24 @@ export default async function build(dir: string, conf = null): Promise<void> { ...@@ -113,21 +113,24 @@ export default async function build(dir: string, conf = null): Promise<void> {
const { target } = config const { target } = config
const buildId = await generateBuildId(config.generateBuildId, nanoid) const buildId = await generateBuildId(config.generateBuildId, nanoid)
const distDir = path.join(dir, config.distDir) const distDir = path.join(dir, config.distDir)
const headers: Header[] = []
const rewrites: Rewrite[] = [] const rewrites: Rewrite[] = []
const redirects: Redirect[] = [] const redirects: Redirect[] = []
const headers: Header[] = []
if (typeof config.experimental.redirects === 'function') { if (typeof config.experimental.redirects === 'function') {
redirects.push(...(await config.experimental.redirects())) const _redirects = await config.experimental.redirects()
checkCustomRoutes(redirects, 'redirect') checkCustomRoutes(_redirects, 'redirect')
redirects.push(..._redirects)
} }
if (typeof config.experimental.rewrites === 'function') { if (typeof config.experimental.rewrites === 'function') {
rewrites.push(...(await config.experimental.rewrites())) const _rewrites = await config.experimental.rewrites()
checkCustomRoutes(rewrites, 'rewrite') checkCustomRoutes(_rewrites, 'rewrite')
rewrites.push(..._rewrites)
} }
if (typeof config.experimental.headers === 'function') { if (typeof config.experimental.headers === 'function') {
headers.push(...(await config.experimental.headers())) const _headers = await config.experimental.headers()
checkCustomRoutes(headers, 'header') checkCustomRoutes(_headers, 'header')
headers.push(..._headers)
} }
if (ciEnvironment.isCI) { if (ciEnvironment.isCI) {
......
...@@ -78,6 +78,13 @@ export default function checkCustomRoutes( ...@@ -78,6 +78,13 @@ export default function checkCustomRoutes(
routes: Redirect[] | Header[] | Rewrite[], routes: Redirect[] | Header[] | Rewrite[],
type: RouteType type: RouteType
): void { ): void {
if (!Array.isArray(routes)) {
throw new Error(
`${type}s must return an array, received ${typeof routes}.\n` +
`See here for more info: https://err.sh/next.js/routes-must-be-array`
)
}
let numInvalidRoutes = 0 let numInvalidRoutes = 0
let hadInvalidStatus = false let hadInvalidStatus = false
......
...@@ -9,7 +9,7 @@ jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 2 ...@@ -9,7 +9,7 @@ jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 2
let appDir = join(__dirname, '..') let appDir = join(__dirname, '..')
const nextConfigPath = join(appDir, 'next.config.js') const nextConfigPath = join(appDir, 'next.config.js')
const writeConfig = async (routes = [], type = 'redirects') => { const writeConfig = async (routes, type = 'redirects') => {
await fs.writeFile( await fs.writeFile(
nextConfigPath, nextConfigPath,
` `
...@@ -322,6 +322,36 @@ const runTests = () => { ...@@ -322,6 +322,36 @@ const runTests = () => {
expect(stderr).toContain(`Reason: Unexpected MODIFIER at 10, expected END`) expect(stderr).toContain(`Reason: Unexpected MODIFIER at 10, expected END`)
expect(stderr).toContain(`/learning/?`) expect(stderr).toContain(`/learning/?`)
}) })
it('should show valid error when non-array is returned from rewrites', async () => {
await writeConfig(
{
source: '/feedback/(?!general)',
destination: '/feedback/general',
},
'rewrites'
)
const stderr = await getStderr()
expect(stderr).toContain(`rewrites must return an array, received object`)
})
it('should show valid error when non-array is returned from redirects', async () => {
await writeConfig(false, 'redirects')
const stderr = await getStderr()
expect(stderr).toContain(`redirects must return an array, received boolean`)
})
it('should show valid error when non-array is returned from headers', async () => {
await writeConfig(undefined, 'headers')
const stderr = await getStderr()
expect(stderr).toContain(`headers must return an array, received undefined`)
})
} }
describe('Errors on invalid custom routes', () => { describe('Errors on invalid custom routes', () => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment