mirror of
https://github.com/terribleplan/next.js.git
synced 2024-01-19 02:48:18 +00:00
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.
This commit is contained in:
parent
d4aa2b0408
commit
311e4ca0ee
17
lib/link.js
17
lib/link.js
|
@ -1,3 +1,5 @@
|
||||||
|
/* 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 {
|
||||||
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 {
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
||||||
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 {
|
||||||
|
|
||||||
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)
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
/* 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 {
|
||||||
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 {
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
||||||
|
|
||||||
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 {
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
}} />}
|
}} />}
|
||||||
<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,17 +37,6 @@ export default async function (dir) {
|
||||||
join(outDir, '_next', buildId, 'page')
|
join(outDir, '_next', buildId, 'page')
|
||||||
)
|
)
|
||||||
|
|
||||||
// Start the rendering process
|
|
||||||
const renderOpts = {
|
|
||||||
dir,
|
|
||||||
buildStats,
|
|
||||||
buildId,
|
|
||||||
assetPrefix: config.assetPrefix.replace(/\/$/, ''),
|
|
||||||
dev: false,
|
|
||||||
staticMarkup: false,
|
|
||||||
hotReloader: null
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get the exportPathMap from the `next.config.js`
|
// Get the exportPathMap from the `next.config.js`
|
||||||
if (typeof config.exportPathMap !== 'function') {
|
if (typeof config.exportPathMap !== 'function') {
|
||||||
printAndExit(
|
printAndExit(
|
||||||
|
@ -59,6 +48,23 @@ export default async function (dir) {
|
||||||
const exportPathMap = await config.exportPathMap()
|
const exportPathMap = await config.exportPathMap()
|
||||||
const exportPaths = Object.keys(exportPathMap)
|
const exportPaths = Object.keys(exportPathMap)
|
||||||
|
|
||||||
|
// Start the rendering process
|
||||||
|
const renderOpts = {
|
||||||
|
dir,
|
||||||
|
buildStats,
|
||||||
|
buildId,
|
||||||
|
nextExport: true,
|
||||||
|
assetPrefix: config.assetPrefix.replace(/\/$/, ''),
|
||||||
|
dev: false,
|
||||||
|
staticMarkup: false,
|
||||||
|
hotReloader: null
|
||||||
|
}
|
||||||
|
|
||||||
|
// We need this for server rendering the Link component.
|
||||||
|
global.__NEXT_DATA__ = {
|
||||||
|
nextExport: true
|
||||||
|
}
|
||||||
|
|
||||||
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, {
|
||||||
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, {
|
||||||
buildId,
|
buildId,
|
||||||
buildStats,
|
buildStats,
|
||||||
assetPrefix,
|
assetPrefix,
|
||||||
|
nextExport,
|
||||||
err: (err) ? serializeError(dev, err) : null
|
err: (err) ? serializeError(dev, err) : null
|
||||||
},
|
},
|
||||||
dev,
|
dev,
|
||||||
|
|
Loading…
Reference in a new issue