Skip to content

Commit 2ba056a

Browse files
timneutkensijjk
andauthored
Add check for invalid assetPrefix (vercel#9759)
* Add check for invalid assetPrefix * Update test/integration/invalid-config-values/test/index.test.js Co-Authored-By: JJ Kasper <[email protected]>
1 parent a32af59 commit 2ba056a

File tree

4 files changed

+87
-0
lines changed

4 files changed

+87
-0
lines changed

errors/invalid-assetprefix.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Invalid assetPrefix
2+
3+
#### Why This Error Occurred
4+
5+
The value of `assetPrefix` in `next.config.js` is set to something that is not a `string`.
6+
7+
#### Possible Ways to Fix It
8+
9+
Ensure that `assetPrefix` is a `string`.
10+
11+
Example:
12+
13+
```js
14+
module.exports = {
15+
assetPrefix: '/',
16+
}
17+
```

packages/next/next-server/server/config.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,12 @@ function assignDefaults(userConfig: { [key: string]: any }) {
100100
})
101101

102102
const result = { ...defaultConfig, ...userConfig }
103+
104+
if (typeof result.assetPrefix !== 'string') {
105+
throw new Error(
106+
`Specified assetPrefix is not a string, found type "${typeof result.assetPrefix}" https://err.sh/zeit/next.js/invalid-assetprefix`
107+
)
108+
}
103109
if (result.experimental && result.experimental.css) {
104110
// The new CSS support requires granular chunks be enabled.
105111
result.experimental.granularChunks = true
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default () => 'hi'
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/* eslint-env jest */
2+
/* global jasmine */
3+
import fs from 'fs-extra'
4+
import { join } from 'path'
5+
import { nextBuild } from 'next-test-utils'
6+
7+
const appDir = join(__dirname, '../')
8+
const nextConfigPath = join(appDir, 'next.config.js')
9+
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 2
10+
11+
const cleanUp = () => fs.remove(nextConfigPath)
12+
13+
describe('Handles valid/invalid assetPrefix', () => {
14+
beforeAll(() => cleanUp())
15+
afterAll(() => cleanUp())
16+
17+
it('should not error without usage of assetPrefix', async () => {
18+
await fs.writeFile(
19+
nextConfigPath,
20+
`module.exports = {
21+
}`
22+
)
23+
24+
const { stderr } = await nextBuild(appDir, undefined, { stderr: true })
25+
expect(stderr).not.toMatch(/Specified assetPrefix is not a string/)
26+
})
27+
28+
it('should not error when assetPrefix is a string', async () => {
29+
await fs.writeFile(
30+
nextConfigPath,
31+
`module.exports = {
32+
assetPrefix: '/hello'
33+
}`
34+
)
35+
36+
const { stderr } = await nextBuild(appDir, undefined, { stderr: true })
37+
expect(stderr).not.toMatch(/Specified assetPrefix is not a string/)
38+
})
39+
40+
it('should error on wrong usage of assetPrefix', async () => {
41+
await fs.writeFile(
42+
nextConfigPath,
43+
`module.exports = {
44+
assetPrefix: null
45+
}`
46+
)
47+
48+
const { stderr } = await nextBuild(appDir, undefined, { stderr: true })
49+
expect(stderr).toMatch(/Specified assetPrefix is not a string/)
50+
})
51+
52+
it('should error on usage of assetPrefix with undefined as value', async () => {
53+
await fs.writeFile(
54+
nextConfigPath,
55+
`module.exports = {
56+
assetPrefix: undefined
57+
}`
58+
)
59+
60+
const { stderr } = await nextBuild(appDir, undefined, { stderr: true })
61+
expect(stderr).toMatch(/Specified assetPrefix is not a string/)
62+
})
63+
})

0 commit comments

Comments
 (0)