Add check for invalid assetPrefix (#9759)

* Add check for invalid assetPrefix

* Update test/integration/invalid-config-values/test/index.test.js

Co-Authored-By: JJ Kasper <jj@jjsweb.site>
This commit is contained in:
Tim Neutkens 2019-12-16 16:07:03 +01:00 committed by GitHub
parent a32af59e93
commit 2ba056aa20
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 87 additions and 0 deletions

View file

@ -0,0 +1,17 @@
# Invalid assetPrefix
#### Why This Error Occurred
The value of `assetPrefix` in `next.config.js` is set to something that is not a `string`.
#### Possible Ways to Fix It
Ensure that `assetPrefix` is a `string`.
Example:
```js
module.exports = {
assetPrefix: '/',
}
```

View file

@ -100,6 +100,12 @@ function assignDefaults(userConfig: { [key: string]: any }) {
})
const result = { ...defaultConfig, ...userConfig }
if (typeof result.assetPrefix !== 'string') {
throw new Error(
`Specified assetPrefix is not a string, found type "${typeof result.assetPrefix}" https://err.sh/zeit/next.js/invalid-assetprefix`
)
}
if (result.experimental && result.experimental.css) {
// The new CSS support requires granular chunks be enabled.
result.experimental.granularChunks = true

View file

@ -0,0 +1 @@
export default () => 'hi'

View file

@ -0,0 +1,63 @@
/* eslint-env jest */
/* global jasmine */
import fs from 'fs-extra'
import { join } from 'path'
import { nextBuild } from 'next-test-utils'
const appDir = join(__dirname, '../')
const nextConfigPath = join(appDir, 'next.config.js')
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 2
const cleanUp = () => fs.remove(nextConfigPath)
describe('Handles valid/invalid assetPrefix', () => {
beforeAll(() => cleanUp())
afterAll(() => cleanUp())
it('should not error without usage of assetPrefix', async () => {
await fs.writeFile(
nextConfigPath,
`module.exports = {
}`
)
const { stderr } = await nextBuild(appDir, undefined, { stderr: true })
expect(stderr).not.toMatch(/Specified assetPrefix is not a string/)
})
it('should not error when assetPrefix is a string', async () => {
await fs.writeFile(
nextConfigPath,
`module.exports = {
assetPrefix: '/hello'
}`
)
const { stderr } = await nextBuild(appDir, undefined, { stderr: true })
expect(stderr).not.toMatch(/Specified assetPrefix is not a string/)
})
it('should error on wrong usage of assetPrefix', async () => {
await fs.writeFile(
nextConfigPath,
`module.exports = {
assetPrefix: null
}`
)
const { stderr } = await nextBuild(appDir, undefined, { stderr: true })
expect(stderr).toMatch(/Specified assetPrefix is not a string/)
})
it('should error on usage of assetPrefix with undefined as value', async () => {
await fs.writeFile(
nextConfigPath,
`module.exports = {
assetPrefix: undefined
}`
)
const { stderr } = await nextBuild(appDir, undefined, { stderr: true })
expect(stderr).toMatch(/Specified assetPrefix is not a string/)
})
})