1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00

Add support for exportPathMap in development (#4094)

* Add support for exportPathMap in development

* Add comment about what it does
This commit is contained in:
Tim Neutkens 2018-04-05 03:48:11 +02:00 committed by Arunoda Susiripala
parent b393833aea
commit dac2f39a25
3 changed files with 61 additions and 5 deletions

View file

@ -81,7 +81,6 @@ export default class Server {
})
this.setAssetPrefix(assetPrefix)
this.defineRoutes()
}
getHotReloader (dir, options) {
@ -119,6 +118,7 @@ export default class Server {
}
async prepare () {
await this.defineRoutes()
if (this.hotReloader) {
await this.hotReloader.start()
}
@ -139,7 +139,7 @@ export default class Server {
}
}
defineRoutes () {
async defineRoutes () {
const routes = {
'/_next-prefetcher.js': async (req, res, params) => {
const p = join(__dirname, '../client/next-prefetcher-bundle.js')
@ -303,9 +303,22 @@ export default class Server {
}
if (this.nextConfig.useFileSystemPublicRoutes) {
// Makes `next export` exportPathMap work in development mode.
// So that the user doesn't have to define a custom server reading the exportPathMap
if (this.dev && this.nextConfig.exportPathMap) {
console.log('Defining routes from exportPathMap')
const exportPathMap = await this.nextConfig.exportPathMap({}) // In development we can't give a default path mapping
for (const path in exportPathMap) {
const options = exportPathMap[path]
routes[path] = async (req, res, params, parsedUrl) => {
await this.render(req, res, options.page, options.query, parsedUrl)
}
}
}
routes['/:path*'] = async (req, res, params, parsedUrl) => {
const { pathname, query } = parsedUrl
await this.render(req, res, pathname, query)
await this.render(req, res, pathname, query, parsedUrl)
}
}

View file

@ -0,0 +1,24 @@
/* global describe, it, expect */
import webdriver from 'next-webdriver'
export default function (context) {
describe('Render in development mode', () => {
it('should render the home page', async () => {
const browser = await webdriver(context.port, '/')
const text = await browser
.elementByCss('#home-page p').text()
expect(text).toBe('This is the home page')
browser.close()
})
it('should render pages only existent in exportPathMap page', async () => {
const browser = await webdriver(context.port, '/dynamic/one')
const text = await browser
.elementByCss('#dynamic-page p').text()
expect(text).toBe('next export is nice')
browser.close()
})
})
}

View file

@ -5,16 +5,22 @@ import {
nextBuild,
nextExport,
startStaticServer,
stopApp
launchApp,
stopApp,
killApp,
findPort,
renderViaHTTP
} from 'next-test-utils'
import ssr from './ssr'
import browser from './browser'
import dev from './dev'
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 5
const appDir = join(__dirname, '../')
const context = {}
const devContext = {}
describe('Static Export', () => {
beforeAll(async () => {
@ -24,9 +30,22 @@ describe('Static Export', () => {
context.server = await startStaticServer(join(appDir, 'out'))
context.port = context.server.address().port
devContext.appPort = await findPort()
devContext.server = await launchApp(join(__dirname, '../'), devContext.appPort, true)
// pre-build all pages at the start
await Promise.all([
renderViaHTTP(devContext.appPort, '/'),
renderViaHTTP(devContext.appPort, '/dynamic/one')
])
})
afterAll(() => {
stopApp(context.server)
killApp(devContext.server)
})
afterAll(() => stopApp(context.server))
ssr(context)
browser(context)
dev(context)
})