Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
d3d35adc
Commit
d3d35adc
authored
Oct 08, 2018
by
Tim Neutkens
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Require pages directly instead of from manifest
parent
38d315f6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
42 additions
and
68 deletions
+42
-68
constants.js
packages/next-server/lib/constants.js
+1
-3
render.js
packages/next-server/server/render.js
+35
-17
require.js
packages/next-server/server/require.js
+1
-25
rendering.js
test/integration/basic/test/rendering.js
+2
-2
index.test.js
test/integration/production/test/index.test.js
+2
-2
require-page.test.js
test/isolated/require-page.test.js
+1
-19
No files found.
packages/next-server/lib/constants.js
View file @
d3d35adc
import
{
join
}
from
'path'
export
const
PHASE_EXPORT
=
'phase-export'
export
const
PHASE_EXPORT
=
'phase-export'
export
const
PHASE_PRODUCTION_BUILD
=
'phase-production-build'
export
const
PHASE_PRODUCTION_BUILD
=
'phase-production-build'
export
const
PHASE_PRODUCTION_SERVER
=
'phase-production-server'
export
const
PHASE_PRODUCTION_SERVER
=
'phase-production-server'
export
const
PHASE_DEVELOPMENT_SERVER
=
'phase-development-server'
export
const
PHASE_DEVELOPMENT_SERVER
=
'phase-development-server'
export
const
PAGES_MANIFEST
=
'pages-manifest.json'
export
const
PAGES_MANIFEST
=
'pages-manifest.json'
export
const
BUILD_MANIFEST
=
'build-manifest.json'
export
const
BUILD_MANIFEST
=
'build-manifest.json'
export
const
REACT_LOADABLE_MANIFEST
=
'react-loadable-manifest.json'
export
const
SERVER_DIRECTORY
=
'server'
export
const
SERVER_DIRECTORY
=
'server'
export
const
CONFIG_FILE
=
'next.config.js'
export
const
CONFIG_FILE
=
'next.config.js'
export
const
BUILD_ID_FILE
=
'BUILD_ID'
export
const
BUILD_ID_FILE
=
'BUILD_ID'
...
@@ -24,4 +22,4 @@ export const CLIENT_STATIC_FILES_RUNTIME_WEBPACK = `${CLIENT_STATIC_FILES_RUNTIM
...
@@ -24,4 +22,4 @@ export const CLIENT_STATIC_FILES_RUNTIME_WEBPACK = `${CLIENT_STATIC_FILES_RUNTIM
// matches static/<buildid>/pages/<page>.js
// matches static/<buildid>/pages/<page>.js
export
const
IS_BUNDLED_PAGE_REGEX
=
/^static
[/\\][^/\\]
+
[/\\]
pages.*
\.
js$/
export
const
IS_BUNDLED_PAGE_REGEX
=
/^static
[/\\][^/\\]
+
[/\\]
pages.*
\.
js$/
// matches static/<buildid>/pages/:page*.js
// matches static/<buildid>/pages/:page*.js
export
const
ROUTE_NAME_REGEX
=
/^static
[/\\][^/\\]
+
[/\\]
pages
[/\\](
.*
)\.
js$/
export
const
ROUTE_NAME_REGEX
=
/^static
[/\\][^/\\]
+
[/\\]
pages
[/\\](
.*
)\.
js$/
\ No newline at end of file
packages/next-server/server/render.js
View file @
d3d35adc
...
@@ -4,14 +4,18 @@ import { renderToString, renderToStaticMarkup } from 'react-dom/server'
...
@@ -4,14 +4,18 @@ import { renderToString, renderToStaticMarkup } from 'react-dom/server'
import
send
from
'send'
import
send
from
'send'
import
generateETag
from
'etag'
import
generateETag
from
'etag'
import
fresh
from
'fresh'
import
fresh
from
'fresh'
import
requirePage
,
{
normalizePagePath
}
from
'./require'
import
{
normalizePagePath
,
pageNotFoundError
}
from
'./require'
import
{
Router
}
from
'../lib/router'
import
{
Router
}
from
'../lib/router'
import
{
loadGetInitialProps
,
isResSent
}
from
'../lib/utils'
import
{
loadGetInitialProps
,
isResSent
}
from
'../lib/utils'
import
Head
,
{
defaultHead
}
from
'../lib/head'
import
Head
,
{
defaultHead
}
from
'../lib/head'
import
ErrorDebug
from
'../lib/error-debug'
import
ErrorDebug
from
'../lib/error-debug'
import
Loadable
from
'../lib/loadable'
import
Loadable
from
'../lib/loadable'
import
LoadableCapture
from
'../lib/loadable-capture'
import
LoadableCapture
from
'../lib/loadable-capture'
import
{
BUILD_MANIFEST
,
REACT_LOADABLE_MANIFEST
,
SERVER_DIRECTORY
,
CLIENT_STATIC_FILES_PATH
}
from
'next-server/constants'
import
{
promisify
}
from
'util'
import
fs
from
'fs'
import
{
BUILD_MANIFEST
,
SERVER_DIRECTORY
,
CLIENT_STATIC_FILES_PATH
}
from
'next-server/constants'
const
access
=
promisify
(
fs
.
access
)
// Based on https://github.com/jamiebuilds/react-loadable/pull/132
// Based on https://github.com/jamiebuilds/react-loadable/pull/132
function
getDynamicImportBundles
(
manifest
,
moduleIds
)
{
function
getDynamicImportBundles
(
manifest
,
moduleIds
)
{
...
@@ -47,12 +51,11 @@ export function renderErrorToHTML (err, req, res, pathname, query, opts = {}) {
...
@@ -47,12 +51,11 @@ export function renderErrorToHTML (err, req, res, pathname, query, opts = {}) {
return
doRender
(
req
,
res
,
pathname
,
query
,
{
...
opts
,
err
,
page
:
'/_error'
})
return
doRender
(
req
,
res
,
pathname
,
query
,
{
...
opts
,
err
,
page
:
'/_error'
})
}
}
function
getPageFiles
(
buildManifest
,
page
)
{
function
getPageFiles
(
buildManifest
,
normalizedPagePath
)
{
const
normalizedPage
=
normalizePagePath
(
page
)
const
files
=
buildManifest
.
pages
[
normalizedPagePath
]
const
files
=
buildManifest
.
pages
[
normalizedPage
]
if
(
!
files
)
{
if
(
!
files
)
{
console
.
warn
(
`Could not find files for
${
normalizedPage
}
in .next/build-manifest.json`
)
console
.
warn
(
`Could not find files for
${
normalizedPage
Path
}
in .next/build-manifest.json`
)
return
[]
return
[]
}
}
...
@@ -73,30 +76,45 @@ async function doRender (req, res, pathname, query, {
...
@@ -73,30 +76,45 @@ async function doRender (req, res, pathname, query, {
nextExport
nextExport
}
=
{})
{
}
=
{})
{
page
=
page
||
pathname
page
=
page
||
pathname
let
normalizedPagePath
try
{
normalizedPagePath
=
normalizePagePath
(
page
)
}
catch
(
err
)
{
console
.
error
(
err
)
throw
pageNotFoundError
(
page
)
}
// In dev mode we use on demand entries to compile the page before rendering
// In dev mode we use on demand entries to compile the page before rendering
if
(
hotReloader
)
{
if
(
hotReloader
)
{
await
hotReloader
.
ensurePage
(
page
)
await
hotReloader
.
ensurePage
(
page
)
}
}
const
pagePath
=
join
(
distDir
,
SERVER_DIRECTORY
,
CLIENT_STATIC_FILES_PATH
,
buildId
,
'pages'
,
normalizedPagePath
)
try
{
await
access
(
`
${
pagePath
}
.js`
,
(
fs
.
constants
||
fs
).
R_OK
)
}
catch
(
err
)
{
throw
pageNotFoundError
(
page
)
}
const
documentPath
=
join
(
distDir
,
SERVER_DIRECTORY
,
CLIENT_STATIC_FILES_PATH
,
buildId
,
'pages'
,
'_document'
)
const
documentPath
=
join
(
distDir
,
SERVER_DIRECTORY
,
CLIENT_STATIC_FILES_PATH
,
buildId
,
'pages'
,
'_document'
)
const
appPath
=
join
(
distDir
,
SERVER_DIRECTORY
,
CLIENT_STATIC_FILES_PATH
,
buildId
,
'pages'
,
'_app'
)
const
appPath
=
join
(
distDir
,
SERVER_DIRECTORY
,
CLIENT_STATIC_FILES_PATH
,
buildId
,
'pages'
,
'_app'
)
let
[
buildManifest
,
reactLoadableManifest
,
Component
,
Document
,
App
]
=
await
Promise
.
all
([
require
(
join
(
distDir
,
BUILD_MANIFEST
)),
require
(
join
(
distDir
,
REACT_LOADABLE_MANIFEST
)),
requirePage
(
page
,
{
distDir
}),
require
(
documentPath
),
require
(
appPath
)
])
let
Document
=
require
(
documentPath
)
let
App
=
require
(
appPath
)
let
Component
=
require
(
pagePath
)
Document
=
Document
.
default
||
Document
App
=
App
.
default
||
App
Component
=
Component
.
default
||
Component
Component
=
Component
.
default
||
Component
const
reactLoadableManifest
=
require
(
pagePath
+
'-loadable.json'
)
const
buildManifest
=
require
(
join
(
distDir
,
BUILD_MANIFEST
))
if
(
typeof
Component
!==
'function'
)
{
if
(
typeof
Component
!==
'function'
)
{
throw
new
Error
(
`The default export is not a React Component in page: "
${
pa
thnam
e
}
"`
)
throw
new
Error
(
`The default export is not a React Component in page: "
${
pa
g
e
}
"`
)
}
}
App
=
App
.
default
||
App
Document
=
Document
.
default
||
Document
const
asPath
=
req
.
url
const
asPath
=
req
.
url
const
ctx
=
{
err
,
req
,
res
,
pathname
,
query
,
asPath
}
const
ctx
=
{
err
,
req
,
res
,
pathname
,
query
,
asPath
}
const
router
=
new
Router
(
pathname
,
query
,
asPath
)
const
router
=
new
Router
(
pathname
,
query
,
asPath
)
...
@@ -104,7 +122,7 @@ async function doRender (req, res, pathname, query, {
...
@@ -104,7 +122,7 @@ async function doRender (req, res, pathname, query, {
const
devFiles
=
buildManifest
.
devFiles
const
devFiles
=
buildManifest
.
devFiles
const
files
=
[
const
files
=
[
...
new
Set
([
...
new
Set
([
...
getPageFiles
(
buildManifest
,
page
),
...
getPageFiles
(
buildManifest
,
normalizedPagePath
),
...
getPageFiles
(
buildManifest
,
'/_app'
),
...
getPageFiles
(
buildManifest
,
'/_app'
),
...
getPageFiles
(
buildManifest
,
'/_error'
)
...
getPageFiles
(
buildManifest
,
'/_error'
)
])
])
...
...
packages/next-server/server/require.js
View file @
d3d35adc
import
{
join
,
posix
}
from
'path'
import
{
posix
}
from
'path'
import
{
PAGES_MANIFEST
,
SERVER_DIRECTORY
}
from
'next-server/constants'
export
function
pageNotFoundError
(
page
)
{
export
function
pageNotFoundError
(
page
)
{
const
err
=
new
Error
(
`Cannot find module for page:
${
page
}
`
)
const
err
=
new
Error
(
`Cannot find module for page:
${
page
}
`
)
...
@@ -26,26 +25,3 @@ export function normalizePagePath (page) {
...
@@ -26,26 +25,3 @@ export function normalizePagePath (page) {
return
page
return
page
}
}
export
function
getPagePath
(
page
,
{
distDir
})
{
const
serverBuildPath
=
join
(
distDir
,
SERVER_DIRECTORY
)
const
pagesManifest
=
require
(
join
(
serverBuildPath
,
PAGES_MANIFEST
))
try
{
page
=
normalizePagePath
(
page
)
}
catch
(
err
)
{
console
.
error
(
err
)
throw
pageNotFoundError
(
page
)
}
if
(
!
pagesManifest
[
page
])
{
throw
pageNotFoundError
(
page
)
}
return
join
(
serverBuildPath
,
pagesManifest
[
page
])
}
export
default
async
function
requirePage
(
page
,
{
distDir
})
{
const
pagePath
=
getPagePath
(
page
,
{
distDir
})
return
require
(
pagePath
)
}
test/integration/basic/test/rendering.js
View file @
d3d35adc
/* global describe, test, it, expect */
/* global describe, test, it, expect */
import
cheerio
from
'cheerio'
import
cheerio
from
'cheerio'
import
{
BUILD_MANIFEST
,
REACT_LOADABLE_MANIFEST
}
from
'next-server/constants'
import
{
BUILD_MANIFEST
}
from
'next-server/constants'
import
{
join
}
from
'path'
import
{
join
}
from
'path'
export
default
function
({
app
},
suiteName
,
render
,
fetch
,
appPort
)
{
export
default
function
({
app
},
suiteName
,
render
,
fetch
,
appPort
)
{
...
@@ -173,7 +173,7 @@ export default function ({ app }, suiteName, render, fetch, appPort) {
...
@@ -173,7 +173,7 @@ export default function ({ app }, suiteName, render, fetch, appPort) {
await
fetch
(
'/dynamic/ssr'
)
await
fetch
(
'/dynamic/ssr'
)
const
buildManifest
=
require
(
join
(
'../.next'
,
BUILD_MANIFEST
))
const
buildManifest
=
require
(
join
(
'../.next'
,
BUILD_MANIFEST
))
const
reactLoadableManifest
=
require
(
join
(
'../.next'
,
REACT_LOADABLE_MANIFEST
))
const
reactLoadableManifest
=
require
(
join
(
'../.next'
,
'server'
,
'static'
,
buildId
,
'pages'
,
'dynamic'
,
'ssr-loadable.json'
))
const
resources
=
[]
const
resources
=
[]
// test a regular page
// test a regular page
...
...
test/integration/production/test/index.test.js
View file @
d3d35adc
...
@@ -16,7 +16,7 @@ import fetch from 'node-fetch'
...
@@ -16,7 +16,7 @@ import fetch from 'node-fetch'
import
dynamicImportTests
from
'./dynamic'
import
dynamicImportTests
from
'./dynamic'
import
processEnv
from
'./process-env'
import
processEnv
from
'./process-env'
import
security
from
'./security'
import
security
from
'./security'
import
{
BUILD_MANIFEST
,
REACT_LOADABLE_MANIFEST
}
from
'next-server/constants'
import
{
BUILD_MANIFEST
}
from
'next-server/constants'
const
appDir
=
join
(
__dirname
,
'../'
)
const
appDir
=
join
(
__dirname
,
'../'
)
let
appPort
let
appPort
...
@@ -79,7 +79,7 @@ describe('Production Usage', () => {
...
@@ -79,7 +79,7 @@ describe('Production Usage', () => {
it
(
'should set Cache-Control header'
,
async
()
=>
{
it
(
'should set Cache-Control header'
,
async
()
=>
{
const
buildId
=
readFileSync
(
join
(
__dirname
,
'../.next/BUILD_ID'
),
'utf8'
)
const
buildId
=
readFileSync
(
join
(
__dirname
,
'../.next/BUILD_ID'
),
'utf8'
)
const
buildManifest
=
require
(
join
(
'../.next'
,
BUILD_MANIFEST
))
const
buildManifest
=
require
(
join
(
'../.next'
,
BUILD_MANIFEST
))
const
reactLoadableManifest
=
require
(
join
(
'../.next'
,
REACT_LOADABLE_MANIFEST
))
const
reactLoadableManifest
=
require
(
join
(
'../.next'
,
'server'
,
'static'
,
buildId
,
'pages'
,
'dynamic'
,
'ssr-loadable.json'
))
const
url
=
`http://localhost:
${
appPort
}
/_next/`
const
url
=
`http://localhost:
${
appPort
}
/_next/`
const
resources
=
[]
const
resources
=
[]
...
...
test/isolated/require-page.test.js
View file @
d3d35adc
/* global describe, it, expect */
/* global describe, it, expect */
import
{
join
}
from
'path'
import
{
join
}
from
'path'
import
{
SERVER_DIRECTORY
,
CLIENT_STATIC_FILES_PATH
}
from
'next-server/constants'
import
requirePage
,
{
normalizePagePath
,
pageNotFoundError
}
from
'next-server/dist/server/require'
import
requirePage
,
{
getPagePath
,
normalizePagePath
,
pageNotFoundError
}
from
'next-server/dist/server/require'
const
sep
=
'/'
const
sep
=
'/'
const
distDir
=
join
(
__dirname
,
'_resolvedata'
)
const
distDir
=
join
(
__dirname
,
'_resolvedata'
)
const
pathToBundles
=
join
(
distDir
,
SERVER_DIRECTORY
,
CLIENT_STATIC_FILES_PATH
,
'development'
,
'pages'
)
describe
(
'pageNotFoundError'
,
()
=>
{
describe
(
'pageNotFoundError'
,
()
=>
{
it
(
'Should throw error with ENOENT code'
,
()
=>
{
it
(
'Should throw error with ENOENT code'
,
()
=>
{
...
@@ -40,22 +38,6 @@ describe('normalizePagePath', () => {
...
@@ -40,22 +38,6 @@ describe('normalizePagePath', () => {
})
})
})
})
describe
(
'getPagePath'
,
()
=>
{
it
(
'Should append /index to the / page'
,
()
=>
{
const
pagePath
=
getPagePath
(
'/'
,
{
distDir
})
expect
(
pagePath
).
toBe
(
join
(
pathToBundles
,
`
${
sep
}
index.js`
))
})
it
(
'Should prepend / when a page does not have it'
,
()
=>
{
const
pagePath
=
getPagePath
(
'_error'
,
{
distDir
})
expect
(
pagePath
).
toBe
(
join
(
pathToBundles
,
`
${
sep
}
_error.js`
))
})
it
(
'Should throw with paths containing ../'
,
()
=>
{
expect
(()
=>
getPagePath
(
'/../../package.json'
,
{
distDir
})).
toThrow
()
})
})
describe
(
'requirePage'
,
()
=>
{
describe
(
'requirePage'
,
()
=>
{
it
(
'Should require /index.js when using /'
,
async
()
=>
{
it
(
'Should require /index.js when using /'
,
async
()
=>
{
const
page
=
await
requirePage
(
'/'
,
{
distDir
})
const
page
=
await
requirePage
(
'/'
,
{
distDir
})
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment