Skip to content

Commit

Permalink
Make next export respect experimental.exportTrailingSlash (vercel#6752)
Browse files Browse the repository at this point in the history
* Add --no-subfolders argument to next export

* Use experimental.exportTrailingSlash instead of a cli flag

* Add experimental.exportTrailingSlash documentation

* Add tests for export with experimental.exportTrailingSlash

* Remove docs

* Remove comment
  • Loading branch information
martpie authored and timneutkens committed Apr 22, 2019
1 parent 87f60f5 commit be4026f
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 2 deletions.
4 changes: 3 additions & 1 deletion packages/next/export/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,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

if (nextConfig.target !== 'server') throw new Error('Cannot export when target is not server. https://err.sh/zeit/next.js/next-export-serverless')

Expand Down Expand Up @@ -139,7 +140,8 @@ export default async function (dir, options, configuration) {
outDir,
renderOpts,
serverRuntimeConfig,
concurrency
concurrency,
subFolders
})
worker.on('message', ({ type, payload }) => {
if (type === 'progress' && progress) {
Expand Down
6 changes: 5 additions & 1 deletion packages/next/export/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ process.on(
outDir,
renderOpts,
serverRuntimeConfig,
concurrency
concurrency,
subFolders
}) => {
const sema = new Sema(concurrency, { capacity: exportPaths.length })
try {
Expand All @@ -47,6 +48,9 @@ process.on(
})

let htmlFilename = `${path}${sep}index.html`

if (!subFolders) htmlFilename = `${path}.html`

const pageExt = extname(page)
const pathExt = extname(path)
// Make sure page isn't a folder with a dot in the name e.g. `v1.2`
Expand Down
5 changes: 5 additions & 0 deletions test/integration/export-subfolders/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
experimental: {
exportTrailingSlash: false
}
}
3 changes: 3 additions & 0 deletions test/integration/export-subfolders/pages/about.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default () => (
<p>I am an about page</p>
)
3 changes: 3 additions & 0 deletions test/integration/export-subfolders/pages/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default () => (
<p>I am a home page</p>
)
3 changes: 3 additions & 0 deletions test/integration/export-subfolders/pages/posts/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default () => (
<p>I am a list of posts</p>
)
3 changes: 3 additions & 0 deletions test/integration/export-subfolders/pages/posts/single.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default () => (
<p>I am a single post</p>
)
40 changes: 40 additions & 0 deletions test/integration/export-subfolders/test/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/* eslint-env jest */
/* global jasmine */
import fs from 'fs'
import { join } from 'path'
import cheerio from 'cheerio'
import { promisify } from 'util'
import {
nextBuild,
nextExport
} from 'next-test-utils'

jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 5
const readFile = promisify(fs.readFile)
const access = promisify(fs.access)
const appDir = join(__dirname, '../')
const outdir = join(appDir, 'out')

describe('Export experimental.exportTrailingSlash set to false', () => {
beforeAll(async () => {
await nextBuild(appDir)
await nextExport(appDir, { outdir })
})

it('should export pages as [filename].html instead of [filename]/index.html', async () => {
expect.assertions(6)

await expect(access(join(outdir, 'index.html'))).resolves.toBe(undefined)
await expect(access(join(outdir, 'about.html'))).resolves.toBe(undefined)
await expect(access(join(outdir, 'posts.html'))).resolves.toBe(undefined)
await expect(access(join(outdir, 'posts', 'single.html'))).resolves.toBe(undefined)

const html = await readFile(join(outdir, 'index.html'))
const $ = cheerio.load(html)
expect($('p').text()).toBe('I am a home page')

const htmlSingle = await readFile(join(outdir, 'posts', 'single.html'))
const $single = cheerio.load(htmlSingle)
expect($single('p').text()).toBe('I am a single post')
})
})

0 comments on commit be4026f

Please sign in to comment.