mirror of
https://github.com/terribleplan/next.js.git
synced 2024-01-19 02:48:18 +00:00
Move getPageFiles and convert to ts (#5841)
* Move getPageFiles and convert to ts # Conflicts: # packages/next-server/server/render.js * Fix unit tests
This commit is contained in:
parent
8ca5749ff9
commit
8873242b0b
|
@ -18,7 +18,8 @@
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "taskr",
|
"build": "taskr",
|
||||||
"release": "taskr release",
|
"release": "taskr release",
|
||||||
"prepublish": "npm run release"
|
"prepublish": "npm run release",
|
||||||
|
"typescript": "tsc --noEmit"
|
||||||
},
|
},
|
||||||
"taskr": {
|
"taskr": {
|
||||||
"requires": [
|
"requires": [
|
||||||
|
|
19
packages/next-server/server/get-page-files.ts
Normal file
19
packages/next-server/server/get-page-files.ts
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
import {normalizePagePath} from './require'
|
||||||
|
|
||||||
|
type BuildManifest = {
|
||||||
|
pages: {
|
||||||
|
[page: string]: string[]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getPageFiles (buildManifest: BuildManifest, page: string): string[] {
|
||||||
|
const normalizedPage = normalizePagePath(page)
|
||||||
|
const files = buildManifest.pages[normalizedPage]
|
||||||
|
|
||||||
|
if (!files) {
|
||||||
|
console.warn(`Could not find files for ${normalizedPage} in .next/build-manifest.json`)
|
||||||
|
return []
|
||||||
|
}
|
||||||
|
|
||||||
|
return files
|
||||||
|
}
|
|
@ -3,7 +3,7 @@ import React from 'react'
|
||||||
import { renderToString, renderToStaticMarkup } from 'react-dom/server'
|
import { renderToString, renderToStaticMarkup } from 'react-dom/server'
|
||||||
import generateETag from 'etag'
|
import generateETag from 'etag'
|
||||||
import fresh from 'fresh'
|
import fresh from 'fresh'
|
||||||
import requirePage, {normalizePagePath} from './require'
|
import {requirePage} from './require'
|
||||||
import Router from '../lib/router/router'
|
import Router from '../lib/router/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'
|
||||||
|
@ -11,6 +11,7 @@ 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 { BUILD_MANIFEST, REACT_LOADABLE_MANIFEST, SERVER_DIRECTORY, CLIENT_STATIC_FILES_PATH } from 'next-server/constants'
|
||||||
import {getDynamicImportBundles} from './get-dynamic-import-bundles'
|
import {getDynamicImportBundles} from './get-dynamic-import-bundles'
|
||||||
|
import {getPageFiles} from './get-page-files'
|
||||||
|
|
||||||
export function renderToHTML (req, res, pathname, query, opts) {
|
export function renderToHTML (req, res, pathname, query, opts) {
|
||||||
return doRender(req, res, pathname, query, opts)
|
return doRender(req, res, pathname, query, opts)
|
||||||
|
@ -21,18 +22,6 @@ export function renderErrorToHTML (err, req, res, _pathname, query, opts = {}) {
|
||||||
return doRender(req, res, '/_error', query, { ...opts, err })
|
return doRender(req, res, '/_error', query, { ...opts, err })
|
||||||
}
|
}
|
||||||
|
|
||||||
function getPageFiles (buildManifest, page) {
|
|
||||||
const normalizedPage = normalizePagePath(page)
|
|
||||||
const files = buildManifest.pages[normalizedPage]
|
|
||||||
|
|
||||||
if (!files) {
|
|
||||||
console.warn(`Could not find files for ${normalizedPage} in .next/build-manifest.json`)
|
|
||||||
return []
|
|
||||||
}
|
|
||||||
|
|
||||||
return files
|
|
||||||
}
|
|
||||||
|
|
||||||
async function doRender (req, res, pathname, query, {
|
async function doRender (req, res, pathname, query, {
|
||||||
err,
|
err,
|
||||||
buildId,
|
buildId,
|
||||||
|
@ -48,7 +37,7 @@ async function doRender (req, res, pathname, query, {
|
||||||
let [buildManifest, reactLoadableManifest, Component, Document, App] = await Promise.all([
|
let [buildManifest, reactLoadableManifest, Component, Document, App] = await Promise.all([
|
||||||
require(join(distDir, BUILD_MANIFEST)),
|
require(join(distDir, BUILD_MANIFEST)),
|
||||||
require(join(distDir, REACT_LOADABLE_MANIFEST)),
|
require(join(distDir, REACT_LOADABLE_MANIFEST)),
|
||||||
requirePage(pathname, {distDir}),
|
requirePage(pathname, distDir),
|
||||||
require(documentPath),
|
require(documentPath),
|
||||||
require(appPath)
|
require(appPath)
|
||||||
])
|
])
|
||||||
|
|
|
@ -1,13 +1,13 @@
|
||||||
import {join, posix} from 'path'
|
import {join, posix} from 'path'
|
||||||
import {PAGES_MANIFEST, SERVER_DIRECTORY} from 'next-server/constants'
|
import {PAGES_MANIFEST, SERVER_DIRECTORY} from 'next-server/constants'
|
||||||
|
|
||||||
export function pageNotFoundError (page) {
|
export function pageNotFoundError (page: string): Error {
|
||||||
const err = new Error(`Cannot find module for page: ${page}`)
|
const err: any = new Error(`Cannot find module for page: ${page}`)
|
||||||
err.code = 'ENOENT'
|
err.code = 'ENOENT'
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
export function normalizePagePath (page) {
|
export function normalizePagePath (page: string): string {
|
||||||
// If the page is `/` we need to append `/index`, otherwise the returned directory root will be bundles instead of pages
|
// If the page is `/` we need to append `/index`, otherwise the returned directory root will be bundles instead of pages
|
||||||
if (page === '/') {
|
if (page === '/') {
|
||||||
page = '/index'
|
page = '/index'
|
||||||
|
@ -27,7 +27,7 @@ export function normalizePagePath (page) {
|
||||||
return page
|
return page
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getPagePath (page, {distDir}) {
|
export function getPagePath (page: string, distDir: string): string {
|
||||||
const serverBuildPath = join(distDir, SERVER_DIRECTORY)
|
const serverBuildPath = join(distDir, SERVER_DIRECTORY)
|
||||||
const pagesManifest = require(join(serverBuildPath, PAGES_MANIFEST))
|
const pagesManifest = require(join(serverBuildPath, PAGES_MANIFEST))
|
||||||
|
|
||||||
|
@ -45,7 +45,7 @@ export function getPagePath (page, {distDir}) {
|
||||||
return join(serverBuildPath, pagesManifest[page])
|
return join(serverBuildPath, pagesManifest[page])
|
||||||
}
|
}
|
||||||
|
|
||||||
export default async function requirePage (page, {distDir}) {
|
export async function requirePage (page: string, distDir: string): Promise<any> {
|
||||||
const pagePath = getPagePath(page, {distDir})
|
const pagePath = getPagePath(page, distDir)
|
||||||
return require(pagePath)
|
return require(pagePath)
|
||||||
}
|
}
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
import { join } from 'path'
|
import { join } from 'path'
|
||||||
import {SERVER_DIRECTORY, CLIENT_STATIC_FILES_PATH} from 'next-server/constants'
|
import {SERVER_DIRECTORY, CLIENT_STATIC_FILES_PATH} from 'next-server/constants'
|
||||||
import requirePage, {getPagePath, 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')
|
||||||
|
@ -42,39 +42,39 @@ describe('normalizePagePath', () => {
|
||||||
|
|
||||||
describe('getPagePath', () => {
|
describe('getPagePath', () => {
|
||||||
it('Should append /index to the / page', () => {
|
it('Should append /index to the / page', () => {
|
||||||
const pagePath = getPagePath('/', {distDir})
|
const pagePath = getPagePath('/', distDir)
|
||||||
expect(pagePath).toBe(join(pathToBundles, `${sep}index.js`))
|
expect(pagePath).toBe(join(pathToBundles, `${sep}index.js`))
|
||||||
})
|
})
|
||||||
|
|
||||||
it('Should prepend / when a page does not have it', () => {
|
it('Should prepend / when a page does not have it', () => {
|
||||||
const pagePath = getPagePath('_error', {distDir})
|
const pagePath = getPagePath('_error', distDir)
|
||||||
expect(pagePath).toBe(join(pathToBundles, `${sep}_error.js`))
|
expect(pagePath).toBe(join(pathToBundles, `${sep}_error.js`))
|
||||||
})
|
})
|
||||||
|
|
||||||
it('Should throw with paths containing ../', () => {
|
it('Should throw with paths containing ../', () => {
|
||||||
expect(() => getPagePath('/../../package.json', {distDir})).toThrow()
|
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)
|
||||||
expect(page.test).toBe('hello')
|
expect(page.test).toBe('hello')
|
||||||
})
|
})
|
||||||
|
|
||||||
it('Should require /index.js when using /index', async () => {
|
it('Should require /index.js when using /index', async () => {
|
||||||
const page = await requirePage('/index', {distDir})
|
const page = await requirePage('/index', distDir)
|
||||||
expect(page.test).toBe('hello')
|
expect(page.test).toBe('hello')
|
||||||
})
|
})
|
||||||
|
|
||||||
it('Should require /world.js when using /world', async () => {
|
it('Should require /world.js when using /world', async () => {
|
||||||
const page = await requirePage('/world', {distDir})
|
const page = await requirePage('/world', distDir)
|
||||||
expect(page.test).toBe('world')
|
expect(page.test).toBe('world')
|
||||||
})
|
})
|
||||||
|
|
||||||
it('Should throw when using /../../test.js', async () => {
|
it('Should throw when using /../../test.js', async () => {
|
||||||
try {
|
try {
|
||||||
await requirePage('/../../test', {distDir})
|
await requirePage('/../../test', distDir)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
expect(err.code).toBe('ENOENT')
|
expect(err.code).toBe('ENOENT')
|
||||||
}
|
}
|
||||||
|
@ -82,7 +82,7 @@ describe('requirePage', () => {
|
||||||
|
|
||||||
it('Should throw when using non existent pages like /non-existent.js', async () => {
|
it('Should throw when using non existent pages like /non-existent.js', async () => {
|
||||||
try {
|
try {
|
||||||
await requirePage('/non-existent', {distDir})
|
await requirePage('/non-existent', distDir)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
expect(err.code).toBe('ENOENT')
|
expect(err.code).toBe('ENOENT')
|
||||||
}
|
}
|
||||||
|
@ -90,7 +90,7 @@ describe('requirePage', () => {
|
||||||
|
|
||||||
it('Should bubble up errors in the child component', async () => {
|
it('Should bubble up errors in the child component', async () => {
|
||||||
try {
|
try {
|
||||||
await requirePage('/non-existent-child', {distDir})
|
await requirePage('/non-existent-child', distDir)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
expect(err.code).toBe('MODULE_NOT_FOUND')
|
expect(err.code).toBe('MODULE_NOT_FOUND')
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue