From b2a9670ca46e4ed7bf80e12ccc88f997b6b27766 Mon Sep 17 00:00:00 2001 From: Joe Haddad Date: Wed, 3 Jul 2019 13:25:44 -0400 Subject: [PATCH] Make `exportTrailingSlash` stable (#7746) --- packages/next-server/server/config.ts | 2 +- packages/next/README.md | 18 ++++++++++++++++-- packages/next/build/index.ts | 5 +---- packages/next/build/webpack-config.ts | 2 +- packages/next/export/index.js | 2 +- .../export-subfolders/next.config.js | 5 ----- .../export-subfolders/test/index.test.js | 2 +- test/integration/export/next.config.js | 4 +--- 8 files changed, 22 insertions(+), 18 deletions(-) delete mode 100644 test/integration/export-subfolders/next.config.js diff --git a/packages/next-server/server/config.ts b/packages/next-server/server/config.ts index 797aa5a8fe..2f89519ecc 100644 --- a/packages/next-server/server/config.ts +++ b/packages/next-server/server/config.ts @@ -24,6 +24,7 @@ const defaultConfig: { [key: string]: any } = { amp: { canonicalBase: '', }, + exportTrailingSlash: false, experimental: { cpus: Math.max( 1, @@ -31,7 +32,6 @@ const defaultConfig: { [key: string]: any } = { (os.cpus() || { length: 1 }).length) - 1 ), ampBindInitData: false, - exportTrailingSlash: false, terserLoader: false, profiling: false, flyingShuttle: false, diff --git a/packages/next/README.md b/packages/next/README.md index e89bbd6029..a694de0f92 100644 --- a/packages/next/README.md +++ b/packages/next/README.md @@ -817,6 +817,7 @@ Router.events.on('routeChangeError', (err, url) => { > **Note**: Using router events in `getInitialProps` is discouraged as it may result in unexpected behavior.
> Router events should be registered when a component mounts (`useEffect` or `componentDidMount`/`componentWillUnmount`) or imperatively when an event happens. +> > ```js > useEffect(() => { > const handleRouteChange = url => { @@ -2211,7 +2212,7 @@ Any AMP errors will cause `next export` to exit with status code `1` because the ### TypeScript Support -AMP currently doesn't have built-in types for Typescript, but it's in their roadmap ([#13791](https://github.com/ampproject/amphtml/issues/13791)). As a workaround you can manually add the types to `amp.d.ts` like [here](https://stackoverflow.com/a/50601125). +AMP currently doesn't have built-in types for TypeScript, but it's in their roadmap ([#13791](https://github.com/ampproject/amphtml/issues/13791)). As a workaround you can manually add the types to `amp.d.ts` like [here](https://stackoverflow.com/a/50601125). ## Static HTML export @@ -2265,7 +2266,20 @@ module.exports = { } ``` -> Note that if the path ends with a directory, it will be exported as `/dir-name/index.html`, but if it ends with an extension, it will be exported as the specified filename, e.g. `/readme.md` above. If you use a file extension other than `.html`, you may need to set the `Content-Type` header to `text/html` when serving this content. +The pages will be exported as html files, i.e. `/about` will become `/about.html`. + +It is possible to configure Next.js to export pages as `index.html` files and require trailing slashes, i.e. `/about` becomes `/about/index.html` and is routable via `/about/`. +This was the default behavior prior to Next.js 9. +You can use the following `next.config.js` to switch back to this behavior: + +```js +// next.config.js +module.exports = { + exportTrailingSlash: true, +} +``` + +> **Note**: If the export path is a filename (e.g. `/readme.md`) and is different than `.html`, you may need to set the `Content-Type` header to `text/html` when serving this content. The second argument is an `object` with: diff --git a/packages/next/build/index.ts b/packages/next/build/index.ts index 0ebfbaebea..e9070e2eae 100644 --- a/packages/next/build/index.ts +++ b/packages/next/build/index.ts @@ -406,10 +406,7 @@ export default async function build(dir: string, conf = null): Promise { const exportConfig = { ...config, exportPathMap: (defaultMap: any) => defaultMap, - experimental: { - ...config.experimental, - exportTrailingSlash: false, - }, + exportTrailingSlash: false, } await exportApp(dir, exportOptions, exportConfig) const toMove = await recursiveReadDir(exportOptions.outdir, /.*\.html$/) diff --git a/packages/next/build/webpack-config.ts b/packages/next/build/webpack-config.ts index 070c3f923f..c58841aca4 100644 --- a/packages/next/build/webpack-config.ts +++ b/packages/next/build/webpack-config.ts @@ -441,7 +441,7 @@ export default async function getBaseWebpackConfig( } : {}), 'process.env.__NEXT_EXPORT_TRAILING_SLASH': JSON.stringify( - config.experimental.exportTrailingSlash + config.exportTrailingSlash ), ...(isServer ? { 'typeof window': JSON.stringify('undefined') } diff --git a/packages/next/export/index.js b/packages/next/export/index.js index e110ec55a4..58ec0e6256 100644 --- a/packages/next/export/index.js +++ b/packages/next/export/index.js @@ -32,7 +32,7 @@ export default async function (dir, options, configuration) { const concurrency = options.concurrency || 10 const threads = options.threads || Math.max(cpus().length - 1, 1) const distDir = join(dir, nextConfig.distDir) - const subFolders = nextConfig.experimental.exportTrailingSlash + const subFolders = nextConfig.exportTrailingSlash if (!options.buildExport && nextConfig.target !== 'server') { throw new Error( diff --git a/test/integration/export-subfolders/next.config.js b/test/integration/export-subfolders/next.config.js deleted file mode 100644 index 5a644ded01..0000000000 --- a/test/integration/export-subfolders/next.config.js +++ /dev/null @@ -1,5 +0,0 @@ -module.exports = { - experimental: { - exportTrailingSlash: false - } -} diff --git a/test/integration/export-subfolders/test/index.test.js b/test/integration/export-subfolders/test/index.test.js index 762c2033ec..d0427f1697 100644 --- a/test/integration/export-subfolders/test/index.test.js +++ b/test/integration/export-subfolders/test/index.test.js @@ -12,7 +12,7 @@ const access = promisify(fs.access) const appDir = join(__dirname, '../') const outdir = join(appDir, 'out') -describe('Export experimental.exportTrailingSlash set to false', () => { +describe('Export config#exportTrailingSlash set to false', () => { beforeAll(async () => { await nextBuild(appDir) await nextExport(appDir, { outdir }) diff --git a/test/integration/export/next.config.js b/test/integration/export/next.config.js index dd06cd4a02..911fa06286 100644 --- a/test/integration/export/next.config.js +++ b/test/integration/export/next.config.js @@ -9,9 +9,7 @@ module.exports = phase => { serverRuntimeConfig: { bar: 'bar' }, - experimental: { - exportTrailingSlash: true - }, + exportTrailingSlash: true, exportPathMap: function () { return { '/': { page: '/' },