Skip to content

Commit

Permalink
Allow static exporting without trailing slashes (vercel#2944)
Browse files Browse the repository at this point in the history
  • Loading branch information
gkaemmer committed May 30, 2018
1 parent 0f9ea55 commit d62d9d1
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 6 deletions.
9 changes: 6 additions & 3 deletions lib/router/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* global window */
/* global window, __NEXT_DATA__ */
import _Router from './router'
import { execOnce } from '../utils'

Expand Down Expand Up @@ -106,8 +106,11 @@ export function _rewriteUrlForNextExport (url) {
path = path.replace(/\/$/, '')

let newPath = path
// Append a trailing slash if this path does not have an extension
if (!/\.[^/]+\/?$/.test(path)) {

// By default, append a trailing slash if this path does not have an extension
const exportTrailingSlashes = __NEXT_DATA__ &&
__NEXT_DATA__.nextExportTrailingSlashes
if (exportTrailingSlashes && !/\.[^/]+\/?$/.test(path)) {
newPath = `${path}/`
}

Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -1441,7 +1441,7 @@ 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.
> 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. Links to directory pages (`dir-name/index.html`) will contain a trailing slash (`dir-name/`) unless you set `exportTrailingSlashes: false` in `next.config.js`.
In that, you specify what are the pages you need to export as static HTML.

Expand Down
3 changes: 2 additions & 1 deletion server/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ const defaultConfig = {
useFileSystemPublicRoutes: true,
generateBuildId: () => uuid.v4(),
generateEtags: true,
pageExtensions: ['jsx', 'js']
pageExtensions: ['jsx', 'js'],
exportTrailingSlashes: true
}

export default function getConfig (phase: string, dir: string, customConfig?: ?Object) {
Expand Down
3 changes: 2 additions & 1 deletion server/export.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ export default async function (dir, options, configuration) {

// We need this for server rendering the Link component.
global.__NEXT_DATA__ = {
nextExport: true
nextExport: true,
nextExportTrailingSlashes: config.exportTrailingSlashes
}

const exportPathMap = await nextConfig.exportPathMap(defaultPathMap)
Expand Down

0 comments on commit d62d9d1

Please sign in to comment.