Commit dac2f39a authored by Tim Neutkens's avatar Tim Neutkens Committed by Arunoda Susiripala

Add support for exportPathMap in development (#4094)

* Add support for exportPathMap in development

* Add comment about what it does
parent b393833a
...@@ -81,7 +81,6 @@ export default class Server { ...@@ -81,7 +81,6 @@ export default class Server {
}) })
this.setAssetPrefix(assetPrefix) this.setAssetPrefix(assetPrefix)
this.defineRoutes()
} }
getHotReloader (dir, options) { getHotReloader (dir, options) {
...@@ -119,6 +118,7 @@ export default class Server { ...@@ -119,6 +118,7 @@ export default class Server {
} }
async prepare () { async prepare () {
await this.defineRoutes()
if (this.hotReloader) { if (this.hotReloader) {
await this.hotReloader.start() await this.hotReloader.start()
} }
...@@ -139,7 +139,7 @@ export default class Server { ...@@ -139,7 +139,7 @@ export default class Server {
} }
} }
defineRoutes () { async defineRoutes () {
const routes = { const routes = {
'/_next-prefetcher.js': async (req, res, params) => { '/_next-prefetcher.js': async (req, res, params) => {
const p = join(__dirname, '../client/next-prefetcher-bundle.js') const p = join(__dirname, '../client/next-prefetcher-bundle.js')
...@@ -303,9 +303,22 @@ export default class Server { ...@@ -303,9 +303,22 @@ export default class Server {
} }
if (this.nextConfig.useFileSystemPublicRoutes) { 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) => { routes['/:path*'] = async (req, res, params, parsedUrl) => {
const { pathname, query } = parsedUrl const { pathname, query } = parsedUrl
await this.render(req, res, pathname, query) await this.render(req, res, pathname, query, parsedUrl)
} }
} }
......
/* 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()
})
})
}
...@@ -5,16 +5,22 @@ import { ...@@ -5,16 +5,22 @@ import {
nextBuild, nextBuild,
nextExport, nextExport,
startStaticServer, startStaticServer,
stopApp launchApp,
stopApp,
killApp,
findPort,
renderViaHTTP
} from 'next-test-utils' } from 'next-test-utils'
import ssr from './ssr' import ssr from './ssr'
import browser from './browser' import browser from './browser'
import dev from './dev'
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 5 jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 5
const appDir = join(__dirname, '../') const appDir = join(__dirname, '../')
const context = {} const context = {}
const devContext = {}
describe('Static Export', () => { describe('Static Export', () => {
beforeAll(async () => { beforeAll(async () => {
...@@ -24,9 +30,22 @@ describe('Static Export', () => { ...@@ -24,9 +30,22 @@ describe('Static Export', () => {
context.server = await startStaticServer(join(appDir, 'out')) context.server = await startStaticServer(join(appDir, 'out'))
context.port = context.server.address().port 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) ssr(context)
browser(context) browser(context)
dev(context)
}) })
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment