Commit d3d35adc authored by Tim Neutkens's avatar Tim Neutkens

Require pages directly instead of from manifest

parent 38d315f6
import {join} from 'path'
export const PHASE_EXPORT = 'phase-export'
export const PHASE_PRODUCTION_BUILD = 'phase-production-build'
export const PHASE_PRODUCTION_SERVER = 'phase-production-server'
export const PHASE_DEVELOPMENT_SERVER = 'phase-development-server'
export const PAGES_MANIFEST = 'pages-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 CONFIG_FILE = 'next.config.js'
export const BUILD_ID_FILE = 'BUILD_ID'
......@@ -24,4 +22,4 @@ export const CLIENT_STATIC_FILES_RUNTIME_WEBPACK = `${CLIENT_STATIC_FILES_RUNTIM
// matches static/<buildid>/pages/<page>.js
export const IS_BUNDLED_PAGE_REGEX = /^static[/\\][^/\\]+[/\\]pages.*\.js$/
// matches static/<buildid>/pages/:page*.js
export const ROUTE_NAME_REGEX = /^static[/\\][^/\\]+[/\\]pages[/\\](.*)\.js$/
\ No newline at end of file
export const ROUTE_NAME_REGEX = /^static[/\\][^/\\]+[/\\]pages[/\\](.*)\.js$/
......@@ -4,14 +4,18 @@ import { renderToString, renderToStaticMarkup } from 'react-dom/server'
import send from 'send'
import generateETag from 'etag'
import fresh from 'fresh'
import requirePage, {normalizePagePath} from './require'
import {normalizePagePath, pageNotFoundError} from './require'
import { Router } from '../lib/router'
import { loadGetInitialProps, isResSent } from '../lib/utils'
import Head, { defaultHead } from '../lib/head'
import ErrorDebug from '../lib/error-debug'
import Loadable from '../lib/loadable'
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
function getDynamicImportBundles (manifest, moduleIds) {
......@@ -47,12 +51,11 @@ export function renderErrorToHTML (err, req, res, pathname, query, opts = {}) {
return doRender(req, res, pathname, query, { ...opts, err, page: '/_error' })
}
function getPageFiles (buildManifest, page) {
const normalizedPage = normalizePagePath(page)
const files = buildManifest.pages[normalizedPage]
function getPageFiles (buildManifest, normalizedPagePath) {
const files = buildManifest.pages[normalizedPagePath]
if (!files) {
console.warn(`Could not find files for ${normalizedPage} in .next/build-manifest.json`)
console.warn(`Could not find files for ${normalizedPagePath} in .next/build-manifest.json`)
return []
}
......@@ -73,30 +76,45 @@ async function doRender (req, res, pathname, query, {
nextExport
} = {}) {
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
if (hotReloader) {
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 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
const reactLoadableManifest = require(pagePath + '-loadable.json')
const buildManifest = require(join(distDir, BUILD_MANIFEST))
if (typeof Component !== 'function') {
throw new Error(`The default export is not a React Component in page: "${pathname}"`)
throw new Error(`The default export is not a React Component in page: "${page}"`)
}
App = App.default || App
Document = Document.default || Document
const asPath = req.url
const ctx = { err, req, res, pathname, query, asPath }
const router = new Router(pathname, query, asPath)
......@@ -104,7 +122,7 @@ async function doRender (req, res, pathname, query, {
const devFiles = buildManifest.devFiles
const files = [
...new Set([
...getPageFiles(buildManifest, page),
...getPageFiles(buildManifest, normalizedPagePath),
...getPageFiles(buildManifest, '/_app'),
...getPageFiles(buildManifest, '/_error')
])
......
import {join, posix} from 'path'
import {PAGES_MANIFEST, SERVER_DIRECTORY} from 'next-server/constants'
import {posix} from 'path'
export function pageNotFoundError (page) {
const err = new Error(`Cannot find module for page: ${page}`)
......@@ -26,26 +25,3 @@ export function normalizePagePath (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)
}
/* global describe, test, it, expect */
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'
export default function ({ app }, suiteName, render, fetch, appPort) {
......@@ -173,7 +173,7 @@ export default function ({ app }, suiteName, render, fetch, appPort) {
await fetch('/dynamic/ssr')
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 = []
// test a regular page
......
......@@ -16,7 +16,7 @@ import fetch from 'node-fetch'
import dynamicImportTests from './dynamic'
import processEnv from './process-env'
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, '../')
let appPort
......@@ -79,7 +79,7 @@ describe('Production Usage', () => {
it('should set Cache-Control header', async () => {
const buildId = readFileSync(join(__dirname, '../.next/BUILD_ID'), 'utf8')
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 resources = []
......
/* global describe, it, expect */
import { join } from 'path'
import {SERVER_DIRECTORY, CLIENT_STATIC_FILES_PATH} from 'next-server/constants'
import requirePage, {getPagePath, normalizePagePath, pageNotFoundError} from 'next-server/dist/server/require'
import requirePage, {normalizePagePath, pageNotFoundError} from 'next-server/dist/server/require'
const sep = '/'
const distDir = join(__dirname, '_resolvedata')
const pathToBundles = join(distDir, SERVER_DIRECTORY, CLIENT_STATIC_FILES_PATH, 'development', 'pages')
describe('pageNotFoundError', () => {
it('Should throw error with ENOENT code', () => {
......@@ -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', () => {
it('Should require /index.js when using /', async () => {
const page = await requirePage('/', {distDir})
......
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