Commit 311e4ca0 authored by Arunoda Susiripala's avatar Arunoda Susiripala

Make sure the router is aware of the nextExport

Based on the we can change the routing to do SSR always.
Also make sure pageLoader don't download the page via
client side twice.
parent d4aa2b04
/* global __NEXT_DATA__ */
import { resolve, format, parse } from 'url' import { resolve, format, parse } from 'url'
import React, { Component, Children } from 'react' import React, { Component, Children } from 'react'
import PropTypes from 'prop-types' import PropTypes from 'prop-types'
...@@ -38,6 +40,10 @@ export default class Link extends Component { ...@@ -38,6 +40,10 @@ export default class Link extends Component {
return return
} }
if (__NEXT_DATA__.nextExport) {
return
}
let { href, as } = this let { href, as } = this
if (!isLocal(href)) { if (!isLocal(href)) {
...@@ -73,6 +79,7 @@ export default class Link extends Component { ...@@ -73,6 +79,7 @@ export default class Link extends Component {
} }
prefetch () { prefetch () {
if (__NEXT_DATA__.nextExport) return
if (!this.props.prefetch) return if (!this.props.prefetch) return
if (typeof window === 'undefined') return if (typeof window === 'undefined') return
...@@ -122,6 +129,16 @@ export default class Link extends Component { ...@@ -122,6 +129,16 @@ export default class Link extends Component {
props.href = as || href props.href = as || href
} }
// Add the ending slash to the paths. So, we can serve the
// "<page>/index.html" directly.
const endsWithSlash = /\/$/.test(props.href)
if (
__NEXT_DATA__.nextExport &&
!endsWithSlash
) {
props.href = `${props.href}/`
}
return React.cloneElement(child, props) return React.cloneElement(child, props)
} }
} }
......
...@@ -47,6 +47,12 @@ export default class PageLoader { ...@@ -47,6 +47,12 @@ export default class PageLoader {
this.registerEvents.on(route, fire) this.registerEvents.on(route, fire)
// If the page is loading via SSR, we need to wait for it
// rather downloading it again.
if (document.getElementById(`__NEXT_PAGE__${route}`)) {
return
}
// Load the script if not asked to load yet. // Load the script if not asked to load yet.
if (!this.loadingRoutes[route]) { if (!this.loadingRoutes[route]) {
this.loadScript(route) this.loadScript(route)
......
/* global __NEXT_DATA__ */
import { parse, format } from 'url' import { parse, format } from 'url'
import mitt from 'mitt' import mitt from 'mitt'
import shallowEquals from '../shallow-equals' import shallowEquals from '../shallow-equals'
...@@ -120,6 +121,16 @@ export default class Router { ...@@ -120,6 +121,16 @@ export default class Router {
const url = typeof _url === 'object' ? format(_url) : _url const url = typeof _url === 'object' ? format(_url) : _url
const as = typeof _as === 'object' ? format(_as) : _as const as = typeof _as === 'object' ? format(_as) : _as
// We need to load the page via SSR everytime if we
// are in the nextExport mode.
if (__NEXT_DATA__.nextExport) {
// Add the ending slash to the paths. So, we can serve the
// "<page>/index.html" directly.
const endsWithSlash = /\/$/.test(as)
window.location.href = endsWithSlash ? as : `${as}/`
return
}
this.abortComponentLoad(as) this.abortComponentLoad(as)
const { pathname, query } = parse(url, true) const { pathname, query } = parse(url, true)
...@@ -267,6 +278,7 @@ export default class Router { ...@@ -267,6 +278,7 @@ export default class Router {
} }
async prefetch (url) { async prefetch (url) {
if (__NEXT_DATA__.nextExport) return
// We don't add support for prefetch in the development mode. // We don't add support for prefetch in the development mode.
// If we do that, our on-demand-entries optimization won't performs better // If we do that, our on-demand-entries optimization won't performs better
if (process.env.NODE_ENV === 'development') return if (process.env.NODE_ENV === 'development') return
......
...@@ -133,7 +133,7 @@ export class NextScript extends Component { ...@@ -133,7 +133,7 @@ export class NextScript extends Component {
render () { render () {
const { staticMarkup, __NEXT_DATA__ } = this.context._documentProps const { staticMarkup, __NEXT_DATA__ } = this.context._documentProps
const { realPathname, buildId, assetPrefix } = __NEXT_DATA__ const { pathname, realPathname, buildId, assetPrefix } = __NEXT_DATA__
return <div> return <div>
{staticMarkup ? null : <script dangerouslySetInnerHTML={{ {staticMarkup ? null : <script dangerouslySetInnerHTML={{
...@@ -147,8 +147,8 @@ export class NextScript extends Component { ...@@ -147,8 +147,8 @@ export class NextScript extends Component {
} }
` `
}} />} }} />}
<script async type='text/javascript' src={`${assetPrefix}/_next/${buildId}/page${realPathname}`} /> <script async id={`__NEXT_PAGE__${pathname}`} type='text/javascript' src={`${assetPrefix}/_next/${buildId}/page${realPathname}`} />
<script async type='text/javascript' src={`${assetPrefix}/_next/${buildId}/page/_error.js`} /> <script async id={`__NEXT_PAGE__/_error`} type='text/javascript' src={`${assetPrefix}/_next/${buildId}/page/_error.js`} />
{staticMarkup ? null : this.getScripts()} {staticMarkup ? null : this.getScripts()}
</div> </div>
} }
......
...@@ -37,28 +37,34 @@ export default async function (dir) { ...@@ -37,28 +37,34 @@ export default async function (dir) {
join(outDir, '_next', buildId, 'page') join(outDir, '_next', buildId, 'page')
) )
// Get the exportPathMap from the `next.config.js`
if (typeof config.exportPathMap !== 'function') {
printAndExit(
'> Could not found "exportPathMap" function inside "next.config.js"\n' +
'> "next export" uses that function build html pages.'
)
}
const exportPathMap = await config.exportPathMap()
const exportPaths = Object.keys(exportPathMap)
// Start the rendering process // Start the rendering process
const renderOpts = { const renderOpts = {
dir, dir,
buildStats, buildStats,
buildId, buildId,
nextExport: true,
assetPrefix: config.assetPrefix.replace(/\/$/, ''), assetPrefix: config.assetPrefix.replace(/\/$/, ''),
dev: false, dev: false,
staticMarkup: false, staticMarkup: false,
hotReloader: null hotReloader: null
} }
// Get the exportPathMap from the `next.config.js` // We need this for server rendering the Link component.
if (typeof config.exportPathMap !== 'function') { global.__NEXT_DATA__ = {
printAndExit( nextExport: true
'> Could not found "exportPathMap" function inside "next.config.js"\n' +
'> "next export" uses that function build html pages.'
)
} }
const exportPathMap = await config.exportPathMap()
const exportPaths = Object.keys(exportPathMap)
for (const path of exportPaths) { for (const path of exportPaths) {
const { page, query } = exportPathMap[path] const { page, query } = exportPathMap[path]
const req = { url: path } const req = { url: path }
......
...@@ -40,7 +40,8 @@ async function doRender (req, res, pathname, query, { ...@@ -40,7 +40,8 @@ async function doRender (req, res, pathname, query, {
assetPrefix, assetPrefix,
dir = process.cwd(), dir = process.cwd(),
dev = false, dev = false,
staticMarkup = false staticMarkup = false,
nextExport = false
} = {}) { } = {}) {
page = page || pathname page = page || pathname
...@@ -106,6 +107,7 @@ async function doRender (req, res, pathname, query, { ...@@ -106,6 +107,7 @@ async function doRender (req, res, pathname, query, {
buildId, buildId,
buildStats, buildStats,
assetPrefix, assetPrefix,
nextExport,
err: (err) ? serializeError(dev, err) : null err: (err) ? serializeError(dev, err) : null
}, },
dev, dev,
......
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