Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add check for invalid assetPrefix #9759

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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('Serverless runtime configs', () => {
timneutkens marked this conversation as resolved.
Show resolved Hide resolved
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/)
})
})