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

De-experimentalize the public/ folder #8661

Merged
merged 11 commits into from
Oct 6, 2019
Prev Previous commit
Next Next commit
Error when public is set as an output dir
  • Loading branch information
ijjk committed Sep 6, 2019

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit 22bca9e9990e60bc99574205f53b12c2c0c69fb8
15 changes: 15 additions & 0 deletions errors/can-not-output-to-public.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Can't Override Next Props

#### Why This Error Occurred

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

This is not allowed due to `public` 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#static-file-serving-eg-images)
7 changes: 7 additions & 0 deletions packages/next/export/index.js
Original file line number Diff line number Diff line change
@@ -76,6 +76,13 @@ export default async function (dir, options, configuration) {

// Initialize the output directory
const outDir = options.outdir

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

await recursiveDelete(join(outDir))
await mkdirp(join(outDir, '_next', buildId))

6 changes: 6 additions & 0 deletions packages/next/next-server/server/config.ts
Original file line number Diff line number Diff line change
@@ -70,6 +70,12 @@ function assignDefaults(userConfig: { [key: string]: any }) {
experimentalWarning()
}

if (key === 'distDir' && userConfig[key] === 'public') {
throw new Error(
`The 'public' directory is reserved in Next.js and can not be set as the 'distDir'. https://err.sh/zeit/next.js/can-not-output-to-public`
)
}

const maybeObject = userConfig[key]
if (!!maybeObject && maybeObject.constructor === Object) {
userConfig[key] = {
1 change: 1 addition & 0 deletions test/integration/errors-on-output-to-public/pages/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default () => 'hi'
37 changes: 37 additions & 0 deletions test/integration/errors-on-output-to-public/test/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* eslint-env jest */
/* global jasmine */
import path from 'path'
import fs from 'fs-extra'
import { nextBuild, nextExport } from 'next-test-utils'

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

describe('Errors on output to public', () => {
it('Throws error when `distDir` is set to public', async () => {
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/
)
await fs.remove(nextConfig)
})

it('Throws error when export out dir is public', async () => {
await fs.remove(nextConfig)
await nextBuild(appDir)
const outdir = path.join(appDir, 'public')
const results = await nextExport(
appDir,
{ outdir },
{
stdout: true,
stderr: true
}
)
expect(results.stdout + results.stderr).toMatch(
/The 'public' directory is reserved in Next.js and can not be used as/
)
})
})
4 changes: 2 additions & 2 deletions test/lib/next-test-utils.js
Original file line number Diff line number Diff line change
@@ -180,8 +180,8 @@ export function nextBuild (dir, args = [], opts = {}) {
return runNextCommand(['build', dir, ...args], opts)
}

export function nextExport (dir, { outdir }) {
return runNextCommand(['export', dir, '--outdir', outdir])
export function nextExport (dir, { outdir }, opts = {}) {
return runNextCommand(['export', dir, '--outdir', outdir], opts)
}

export function nextStart (dir, port, opts = {}) {