Commit a6ddaefe authored by JJ Kasper's avatar JJ Kasper Committed by Tim Neutkens

Add experimental exportTrailingSlash config (#6664)

By default when `next export`ing a Next.js application we will automatically append a `/` to all urls to be fully compatible with the directory structure being output.

However since most platforms support directory indexes it makes sense to change this default in the future.

This PR adds `exportTrailingSlash` as experimental flag. We'll try this out for a bit on nextjs.org / zeit.co/docs before introducing it as new option.

The default value is `true` as this is the current behavior in stable Next.js.

```
{
  experimental: {
    exportTrailingSlash: false
  }
}
```

⚠️ as with all experimental flags being added this is subject to breaking between canary/stable versions.
parent fbedce4d
......@@ -205,9 +205,11 @@ export default class Router implements IRouterInterface {
// Add the ending slash to the paths. So, we can serve the
// "<page>/index.html" directly for the SSR page.
// @ts-ignore this is temporarily global (attached to window)
if (__NEXT_DATA__.nextExport) {
as = Router._rewriteUrlForNextExport(as)
if (process.env.__NEXT_EXPORT_TRAILING_SLASH) {
// @ts-ignore this is temporarily global (attached to window)
if (__NEXT_DATA__.nextExport) {
as = Router._rewriteUrlForNextExport(as)
}
}
this.abortComponentLoad(as)
......
......@@ -28,7 +28,8 @@ const defaultConfig = {
(Number(process.env.CIRCLE_NODE_TOTAL) ||
(os.cpus() || { length: 1 }).length) - 1
),
profiling: false
profiling: false,
exportTrailingSlash: true
}
}
......
......@@ -244,6 +244,7 @@ export default function getBaseWebpackConfig (dir: string, {dev = false, isServe
...(dev && !isServer ? {
'process.env.__NEXT_DIST_DIR': JSON.stringify(distDir)
} : {}),
'process.env.__NEXT_EXPORT_TRAILING_SLASH': JSON.stringify(config.experimental.exportTrailingSlash)
}),
!isServer && new ReactLoadablePlugin({
filename: REACT_LOADABLE_MANIFEST
......
......@@ -14,7 +14,7 @@ function isLocal (href) {
(url.protocol === origin.protocol && url.host === origin.host)
}
function memoizedFormatUrl (formatUrl) {
function memoizedFormatUrl (formatFunc) {
let lastHref = null
let lastAs = null
let lastResult = null
......@@ -23,7 +23,7 @@ function memoizedFormatUrl (formatUrl) {
return lastResult
}
const result = formatUrl(href, as)
const result = formatFunc(href, as)
lastHref = href
lastAs = as
lastResult = result
......@@ -31,6 +31,12 @@ function memoizedFormatUrl (formatUrl) {
}
}
function formatUrl (url) {
return url && typeof url === 'object'
? formatWithValidation(url)
: url
}
class Link extends Component {
componentDidMount () {
this.prefetch()
......@@ -46,12 +52,8 @@ class Link extends Component {
// as per https://reactjs.org/blog/2018/06/07/you-probably-dont-need-derived-state.html
formatUrls = memoizedFormatUrl((href, asHref) => {
return {
href: href && typeof href === 'object'
? formatWithValidation(href)
: href,
as: asHref && typeof asHref === 'object'
? formatWithValidation(asHref)
: asHref
href: formatUrl(href),
as: formatUrl(asHref, true)
}
})
......@@ -136,12 +138,14 @@ class Link extends Component {
// Add the ending slash to the paths. So, we can serve the
// "<page>/index.html" directly.
if (
props.href &&
typeof __NEXT_DATA__ !== 'undefined' &&
__NEXT_DATA__.nextExport
) {
props.href = _Router._rewriteUrlForNextExport(props.href)
if (process.env.__NEXT_EXPORT_TRAILING_SLASH) {
if (
props.href &&
typeof __NEXT_DATA__ !== 'undefined' &&
__NEXT_DATA__.nextExport
) {
props.href = _Router._rewriteUrlForNextExport(props.href)
}
}
return React.cloneElement(child, props)
......
......@@ -9,6 +9,9 @@ module.exports = (phase) => {
serverRuntimeConfig: {
bar: 'bar'
},
experimental: {
// exportTrailingSlash: false
},
exportPathMap: function () {
return {
'/': { page: '/' },
......
......@@ -13,6 +13,24 @@ export default function (context) {
browser.close()
})
it('should add trailing slash on Link', async () => {
const browser = await webdriver(context.port, '/')
const link = await browser
.elementByCss('#about-via-link')
.getAttribute('href')
expect(link.substr(link.length - 1)).toBe('/')
})
it('should not add trailing slash on Link when disabled', async () => {
const browser = await webdriver(context.portNoTrailSlash, '/')
const link = await browser
.elementByCss('#about-via-link')
.getAttribute('href')
expect(link.substr(link.length - 1)).not.toBe('/')
})
it('should do navigations via Link', async () => {
const browser = await webdriver(context.port, '/')
const text = await browser
......
......@@ -64,12 +64,21 @@ describe('Static Export', () => {
})
beforeAll(async () => {
const outdir = join(appDir, 'out')
const outNoTrailSlash = join(appDir, 'outNoTrailSlash')
await nextBuild(appDir)
await nextExport(appDir, { outdir })
nextConfig.replace(`// exportTrailingSlash: false`, `exportTrailingSlash: false`)
await nextBuild(appDir)
await nextExport(appDir, { outdir: outNoTrailSlash })
context.server = await startStaticServer(join(appDir, 'out'))
context.port = context.server.address().port
context.serverNoTrailSlash = await startStaticServer(outNoTrailSlash)
context.portNoTrailSlash = context.serverNoTrailSlash.address().port
devContext.port = await findPort()
devContext.server = await launchApp(join(__dirname, '../'), devContext.port, true)
......@@ -82,7 +91,8 @@ describe('Static Export', () => {
afterAll(async () => {
await Promise.all([
stopApp(context.server),
killApp(devContext.server)
killApp(devContext.server),
stopApp(context.serverNoTrailSlash)
])
nextConfig.restore()
})
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment