From feec26807701b9808cdfabdc7591978966cf97dc Mon Sep 17 00:00:00 2001 From: Tim Neutkens Date: Mon, 11 Jan 2021 11:19:17 +0100 Subject: [PATCH 1/3] Error when exporting to `static` directory --- errors/can-not-output-to-static.md | 15 ++++++++ packages/next/export/index.ts | 6 +++ .../test/index.test.js | 4 +- .../errors-on-output-to-static/pages/index.js | 1 + .../test/index.test.js | 37 +++++++++++++++++++ 5 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 errors/can-not-output-to-static.md create mode 100644 test/integration/errors-on-output-to-static/pages/index.js create mode 100644 test/integration/errors-on-output-to-static/test/index.test.js diff --git a/errors/can-not-output-to-static.md b/errors/can-not-output-to-static.md new file mode 100644 index 000000000000000..b949bc06ffb7a02 --- /dev/null +++ b/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) diff --git a/packages/next/export/index.ts b/packages/next/export/index.ts index fc611fccb91a5d5..c820e25c8904daf 100644 --- a/packages/next/export/index.ts +++ b/packages/next/export/index.ts @@ -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 }) diff --git a/test/integration/errors-on-output-to-public/test/index.test.js b/test/integration/errors-on-output-to-public/test/index.test.js index b094b29a74fda1f..be7e9b9bcd1f4c2 100644 --- a/test/integration/errors-on-output-to-public/test/index.test.js +++ b/test/integration/errors-on-output-to-public/test/index.test.js @@ -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) }) @@ -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/ ) }) }) diff --git a/test/integration/errors-on-output-to-static/pages/index.js b/test/integration/errors-on-output-to-static/pages/index.js new file mode 100644 index 000000000000000..0957a987fc2f227 --- /dev/null +++ b/test/integration/errors-on-output-to-static/pages/index.js @@ -0,0 +1 @@ +export default () => 'hi' diff --git a/test/integration/errors-on-output-to-static/test/index.test.js b/test/integration/errors-on-output-to-static/test/index.test.js new file mode 100644 index 000000000000000..d327a7aa17dd0ee --- /dev/null +++ b/test/integration/errors-on-output-to-static/test/index.test.js @@ -0,0 +1,37 @@ +/* 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 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 'static' 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 'static' directory is reserved in Next\.js and can not be used as/ + ) + }) +}) From 10ddb579475f599006a4fdb3f8f0cd54bea1d830 Mon Sep 17 00:00:00 2001 From: Tim Neutkens Date: Mon, 11 Jan 2021 11:45:34 +0100 Subject: [PATCH 2/3] Update `public` references --- .../errors-on-output-to-static/test/index.test.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test/integration/errors-on-output-to-static/test/index.test.js b/test/integration/errors-on-output-to-static/test/index.test.js index d327a7aa17dd0ee..83f6e80cce688f0 100644 --- a/test/integration/errors-on-output-to-static/test/index.test.js +++ b/test/integration/errors-on-output-to-static/test/index.test.js @@ -8,9 +8,9 @@ jest.setTimeout(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' }`) +describe('Errors on output to static', () => { + it('Throws error when `distDir` is set to static', async () => { + await fs.writeFile(nextConfig, `module.exports = { distDir: 'static' }`) const results = await nextBuild(appDir, [], { stdout: true, stderr: true }) expect(results.stdout + results.stderr).toMatch( /The 'static' directory is reserved in Next\.js and can not be set as/ @@ -18,10 +18,10 @@ describe('Errors on output to public', () => { await fs.remove(nextConfig) }) - it('Throws error when export out dir is public', async () => { + it('Throws error when export out dir is static', async () => { await fs.remove(nextConfig) await nextBuild(appDir) - const outdir = path.join(appDir, 'public') + const outdir = path.join(appDir, 'static') const results = await nextExport( appDir, { outdir }, From f860c3b5c24989b772a9b91510e9a5effedf7f08 Mon Sep 17 00:00:00 2001 From: Tim Neutkens Date: Mon, 11 Jan 2021 13:26:35 +0100 Subject: [PATCH 3/3] Remove extra test --- .../errors-on-output-to-static/test/index.test.js | 9 --------- 1 file changed, 9 deletions(-) diff --git a/test/integration/errors-on-output-to-static/test/index.test.js b/test/integration/errors-on-output-to-static/test/index.test.js index 83f6e80cce688f0..9ee69eba8d4101e 100644 --- a/test/integration/errors-on-output-to-static/test/index.test.js +++ b/test/integration/errors-on-output-to-static/test/index.test.js @@ -9,15 +9,6 @@ const appDir = path.join(__dirname, '..') const nextConfig = path.join(appDir, 'next.config.js') describe('Errors on output to static', () => { - it('Throws error when `distDir` is set to static', async () => { - await fs.writeFile(nextConfig, `module.exports = { distDir: 'static' }`) - const results = await nextBuild(appDir, [], { stdout: true, stderr: true }) - expect(results.stdout + results.stderr).toMatch( - /The 'static' directory is reserved in Next\.js and can not be set as/ - ) - await fs.remove(nextConfig) - }) - it('Throws error when export out dir is static', async () => { await fs.remove(nextConfig) await nextBuild(appDir)