Skip to content

Commit

Permalink
Add support for exportPathMap in development (#4094)
Browse files Browse the repository at this point in the history
* Add support for exportPathMap in development

* Add comment about what it does
  • Loading branch information
timneutkens authored and arunoda committed Apr 5, 2018
1 parent b393833 commit dac2f39
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 5 deletions.
19 changes: 16 additions & 3 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ export default class Server {
})

this.setAssetPrefix(assetPrefix)
this.defineRoutes()
}

getHotReloader (dir, options) {
Expand Down Expand Up @@ -119,6 +118,7 @@ export default class Server {
}

async prepare () {
await this.defineRoutes()
if (this.hotReloader) {
await this.hotReloader.start()
}
Expand All @@ -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')
Expand Down Expand Up @@ -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)
}
}

Expand Down
24 changes: 24 additions & 0 deletions test/integration/static/test/dev.js
Original file line number Diff line number Diff line change
@@ -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()
})
})
}
23 changes: 21 additions & 2 deletions test/integration/static/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand All @@ -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)
})

0 comments on commit dac2f39

Please sign in to comment.