Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow static exports without trailing slashes #3283

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

@timneutkens timneutkens Jun 2, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, I was thinking this morning that this might actually be compiled at build time instead of providing it at runtime and having __NEXT_DATA__ used here.

So what you could do is add DefinePlugin with process.env.NEXT_EXPORT_TRAILING_SLASHES which holds the value of config.exportTrailingSlashes. So that it's compiled at build time.

I'm also not sure how you're passing the config to the client right now, so it wouldn't work client-side right 🤔 (with the current solution)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implemented in #4524

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: nextConfig.exportTrailingSlashes
}

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