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

Fix incorrect _document.js error when disableStaticImages: true #30768

Merged
merged 4 commits into from
Nov 2, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions errors/custom-document-image-import.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,19 @@ function MyApp({ Component, pageProps }) {
export default MyApp
```

If your application is not using `next/image`, you can disable the built-in loader with the following next.config.js:
styfle marked this conversation as resolved.
Show resolved Hide resolved

```js
module.exports = {
images: {
disableStaticImages: true,
},
}
```

### Useful Links

- [Custom `Document`](https://nextjs.org/docs/advanced-features/custom-document)
- [Custom `App`](https://nextjs.org/docs/advanced-features/custom-app)
- [Static File Serving](https://nextjs.org/docs/basic-features/static-file-serving)
- [Disable Static Image Imports](https://nextjs.org/docs/api-reference/next/image#disable-static-imports)
1 change: 1 addition & 0 deletions packages/next/build/webpack-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1682,6 +1682,7 @@ export default async function getBaseWebpackConfig(
productionBrowserSourceMaps: config.productionBrowserSourceMaps,
future: config.future,
experimental: config.experimental,
disableStaticImages: config.disableStaticImages,
})

// @ts-ignore Cache exists
Expand Down
7 changes: 6 additions & 1 deletion packages/next/build/webpack/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export async function build(
productionBrowserSourceMaps,
future,
experimental,
disableStaticImages,
}: {
rootDirectory: string
customAppFile: RegExp
Expand All @@ -31,6 +32,7 @@ export async function build(
productionBrowserSourceMaps: boolean
future: NextConfigComplete['future']
experimental: NextConfigComplete['experimental']
disableStaticImages: NextConfigComplete['disableStaticImages']
}
): Promise<webpack.Configuration> {
const ctx: ConfigurationContext = {
Expand All @@ -53,6 +55,9 @@ export async function build(
experimental,
}

const fn = pipe(base(ctx), css(ctx), images(ctx))
let fn = pipe(base(ctx), css(ctx))
if (!disableStaticImages) {
fn = pipe(fn, images(ctx))
}
return fn(config)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
/* replaceme */
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
/* eslint-env jest */
import { nextBuild } from 'next-test-utils'
import { nextBuild, File } from 'next-test-utils'
import { join } from 'path'

jest.setTimeout(1000 * 60 * 2)

const appDir = join(__dirname, '../')
const nextConfig = new File(join(appDir, 'next.config.js'))

describe('Invalid static image import in _document', () => {
it('Should fail to build', async () => {
const appDir = join(__dirname, '..')
afterAll(() => nextConfig.restore())

it('Should fail to build when no next.config.js', async () => {
const { code, stderr } = await nextBuild(appDir, [], {
stderr: true,
})
Expand All @@ -17,4 +21,16 @@ describe('Invalid static image import in _document', () => {
)
expect(stderr).toMatch(/Location:.*pages[\\/]_document\.js/)
})

it('Should build without error when disableStaticImages in next.config.js', async () => {
nextConfig.write(`
module.exports = {
images: {
disableStaticImages: true
}
}
`)
const { code } = await nextBuild(appDir, [], {})
expect(code).toBe(0)
})
})