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

Get rid of realPathname for consistant page file structure.

This commit is contained in:
Arunoda Susiripala 2017-05-10 20:01:42 -07:00
parent 98ed666ca6
commit 6f674b8c0a
5 changed files with 52 additions and 18 deletions

View file

@ -121,6 +121,7 @@
"react-dom": "15.5.3",
"recursive-copy": "^2.0.6",
"standard": "9.0.2",
"walk": "^2.3.9",
"wd": "1.2.0"
},
"peerDependencies": {

View file

@ -67,11 +67,13 @@ export class Head extends Component {
render () {
const { head, styles, __NEXT_DATA__ } = this.context._documentProps
const { realPathname, buildId, assetPrefix } = __NEXT_DATA__
const { pathname, buildId, assetPrefix, nextExport } = __NEXT_DATA__
const pagePathname = nextExport ? `${pathname}/index.js` : pathname
return <head>
<link rel='preload' href={`${assetPrefix}/_next/${buildId}/page${realPathname}`} as='script' />
<link rel='preload' href={`${assetPrefix}/_next/${buildId}/page/_error.js`} as='script' />
<link rel='preload' href={`${assetPrefix}/_next/${buildId}/page${pagePathname}`} as='script' />
<link rel='preload' href={`${assetPrefix}/_next/${buildId}/page/_error/index.js`} as='script' />
{this.getPreloadMainLinks()}
{(head || []).map((h, i) => React.cloneElement(h, { key: i }))}
{styles || null}
@ -133,7 +135,9 @@ export class NextScript extends Component {
render () {
const { staticMarkup, __NEXT_DATA__ } = this.context._documentProps
const { pathname, realPathname, buildId, assetPrefix } = __NEXT_DATA__
const { pathname, nextExport, buildId, assetPrefix } = __NEXT_DATA__
const pagePathname = nextExport ? `${pathname}/index.js` : pathname
return <div>
{staticMarkup ? null : <script dangerouslySetInnerHTML={{
@ -147,8 +151,9 @@ export class NextScript extends Component {
}
`
}} />}
<script async id={`__NEXT_PAGE__${pathname}`} type='text/javascript' src={`${assetPrefix}/_next/${buildId}/page${realPathname}`} />
<script async id={`__NEXT_PAGE__/_error`} type='text/javascript' src={`${assetPrefix}/_next/${buildId}/page/_error.js`} />
<script async id={`__NEXT_PAGE__${pathname}`} type='text/javascript' src={`${assetPrefix}/_next/${buildId}/page${pagePathname}`} />
<script async id={`__NEXT_PAGE__/_error`} type='text/javascript' src={`${assetPrefix}/_next/${buildId}/page/_error/index.js`} />
{staticMarkup ? null : this.getScripts()}
</div>
}

View file

@ -1,6 +1,7 @@
import del from 'del'
import cp from 'recursive-copy'
import mkdirp from 'mkdirp-then'
import walk from 'walk'
import { resolve, join, dirname, sep } from 'path'
import { existsSync, readFileSync, writeFileSync } from 'fs'
import getConfig from './config'
@ -32,10 +33,7 @@ export default async function (dir, options) {
join(outDir, '_next', buildStats['app.js'].hash, 'app.js')
)
await cp(
join(nextDir, 'bundles', 'pages'),
join(outDir, '_next', buildId, 'page')
)
await copyPages(nextDir, outDir, buildId)
// Get the exportPathMap from the `next.config.js`
if (typeof config.exportPathMap !== 'function') {
@ -84,3 +82,31 @@ export default async function (dir, options) {
writeFileSync(htmlFilepath, html, 'utf8')
}
}
function copyPages (nextDir, outDir, buildId) {
// TODO: do some proper error handling
return new Promise((resolve, reject) => {
const nextBundlesDir = join(nextDir, 'bundles', 'pages')
const walker = walk.walk(nextBundlesDir, { followLinks: false })
walker.on('file', (root, stat, next) => {
const filename = stat.name
const fullFilePath = `${root}${sep}${filename}`
const relativeFilePath = fullFilePath.replace(nextBundlesDir, '')
let destFilePath = null
if (/index\.js$/.test(filename)) {
destFilePath = join(outDir, '_next', buildId, 'page', relativeFilePath)
} else {
const newRelativeFilePath = relativeFilePath.replace(/\.js/, `${sep}index.js`)
destFilePath = join(outDir, '_next', buildId, 'page', newRelativeFilePath)
}
cp(fullFilePath, destFilePath)
.then(next)
.catch(reject)
})
walker.on('end', resolve)
})
}

View file

@ -49,13 +49,6 @@ async function doRender (req, res, pathname, query, {
const dist = getConfig(dir).distDir
// realPathname is pretty useful in the document.js
// In there, we include a proper valid resolved path.
// That helps us to server that JSON page directly from a static server.
// Basically, this is a requirment for next-export
const pageRealPath = await resolvePath(join(dir, dist, 'dist', 'pages', page))
const realPathname = pageRealPath.replace(join(dir, dist, 'dist', 'pages'), '')
let [Component, Document] = await Promise.all([
requireModule(join(dir, dist, 'dist', 'pages', page)),
requireModule(join(dir, dist, 'dist', 'pages', '_document'))
@ -102,7 +95,6 @@ async function doRender (req, res, pathname, query, {
__NEXT_DATA__: {
props,
pathname,
realPathname,
query,
buildId,
buildStats,

View file

@ -2327,6 +2327,10 @@ foreach@^2.0.5:
version "2.0.5"
resolved "https://registry.yarnpkg.com/foreach/-/foreach-2.0.5.tgz#0bee005018aeb260d0a3af3ae658dd0136ec1b99"
foreachasync@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/foreachasync/-/foreachasync-3.0.0.tgz#5502987dc8714be3392097f32e0071c9dee07cf6"
foreground-child@^1.3.3, foreground-child@^1.5.3:
version "1.5.6"
resolved "https://registry.yarnpkg.com/foreground-child/-/foreground-child-1.5.6.tgz#4fd71ad2dfde96789b980a5c0a295937cb2f5ce9"
@ -5234,6 +5238,12 @@ vm-browserify@0.0.4:
dependencies:
indexof "0.0.1"
walk@^2.3.9:
version "2.3.9"
resolved "https://registry.yarnpkg.com/walk/-/walk-2.3.9.tgz#31b4db6678f2ae01c39ea9fb8725a9031e558a7b"
dependencies:
foreachasync "^3.0.0"
walkdir@^0.0.11:
version "0.0.11"
resolved "https://registry.yarnpkg.com/walkdir/-/walkdir-0.0.11.tgz#a16d025eb931bd03b52f308caed0f40fcebe9532"