Make exportTrailingSlash stable (#7746)

This commit is contained in:
Joe Haddad 2019-07-03 13:25:44 -04:00 committed by GitHub
parent e36d7b3226
commit b2a9670ca4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 22 additions and 18 deletions

View file

@ -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,

View file

@ -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.<br/>
> 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:

View file

@ -406,10 +406,7 @@ export default async function build(dir: string, conf = null): Promise<void> {
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$/)

View file

@ -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') }

View file

@ -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(

View file

@ -1,5 +0,0 @@
module.exports = {
experimental: {
exportTrailingSlash: false
}
}

View file

@ -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 })

View file

@ -9,9 +9,7 @@ module.exports = phase => {
serverRuntimeConfig: {
bar: 'bar'
},
experimental: {
exportTrailingSlash: true
},
exportTrailingSlash: true,
exportPathMap: function () {
return {
'/': { page: '/' },