Skip to content

Commit

Permalink
Add check for invalid assetPrefix (#9759)
Browse files Browse the repository at this point in the history
* Add check for invalid assetPrefix

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

Co-Authored-By: JJ Kasper <jj@jjsweb.site>
  • Loading branch information
timneutkens and ijjk committed Dec 16, 2019
1 parent a32af59 commit 2ba056a
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 0 deletions.
17 changes: 17 additions & 0 deletions errors/invalid-assetprefix.md
Original file line number Diff line number Diff line change
@@ -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: '/',
}
```
6 changes: 6 additions & 0 deletions packages/next/next-server/server/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions test/integration/invalid-config-values/pages/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default () => 'hi'
63 changes: 63 additions & 0 deletions test/integration/invalid-config-values/test/index.test.js
Original file line number Diff line number Diff line change
@@ -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/)
})
})

0 comments on commit 2ba056a

Please sign in to comment.