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

Error when exporting to static directory #20969

Merged
merged 4 commits into from Jan 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
15 changes: 15 additions & 0 deletions errors/can-not-output-to-static.md
@@ -0,0 +1,15 @@
# Cannot output to /static

#### Why This Error Occurred

Either you set `distDir` to `static` in your `next.config.js` or during `next export` you tried to export to the `static` directory.

This is not allowed due to `static` being a special folder in Next.js used to serve static assets.

#### Possible Ways to Fix It

Use a different `distDir` or export to a different folder.

### Useful Links

- [Static file serving docs](https://nextjs.org/docs/basic-features/static-file-serving)
6 changes: 6 additions & 0 deletions packages/next/export/index.ts
Expand Up @@ -252,6 +252,12 @@ export default async function exportApp(
)
}

if (outDir === join(dir, 'static')) {
throw new Error(
`The 'static' directory is reserved in Next.js and can not be used as the export out directory. https://err.sh/vercel/next.js/can-not-output-to-static`
)
}

await recursiveDelete(join(outDir))
await promises.mkdir(join(outDir, '_next', buildId), { recursive: true })

Expand Down
Expand Up @@ -13,7 +13,7 @@ describe('Errors on output to public', () => {
await fs.writeFile(nextConfig, `module.exports = { distDir: 'public' }`)
const results = await nextBuild(appDir, [], { stdout: true, stderr: true })
expect(results.stdout + results.stderr).toMatch(
/The 'public' directory is reserved in Next.js and can not be set as/
/The 'public' directory is reserved in Next\.js and can not be set as/
)
await fs.remove(nextConfig)
})
Expand All @@ -31,7 +31,7 @@ describe('Errors on output to public', () => {
}
)
expect(results.stdout + results.stderr).toMatch(
/The 'public' directory is reserved in Next.js and can not be used as/
/The 'public' directory is reserved in Next\.js and can not be used as/
)
})
})
@@ -0,0 +1 @@
export default () => 'hi'
28 changes: 28 additions & 0 deletions test/integration/errors-on-output-to-static/test/index.test.js
@@ -0,0 +1,28 @@
/* eslint-env jest */

import path from 'path'
import fs from 'fs-extra'
import { nextBuild, nextExport } from 'next-test-utils'

jest.setTimeout(1000 * 60 * 1)
const appDir = path.join(__dirname, '..')
const nextConfig = path.join(appDir, 'next.config.js')

describe('Errors on output to static', () => {
it('Throws error when export out dir is static', async () => {
await fs.remove(nextConfig)
await nextBuild(appDir)
const outdir = path.join(appDir, 'static')
const results = await nextExport(
appDir,
{ outdir },
{
stdout: true,
stderr: true,
}
)
expect(results.stdout + results.stderr).toMatch(
/The 'static' directory is reserved in Next\.js and can not be used as/
)
})
})