mirror of
https://github.com/terribleplan/next.js.git
synced 2024-01-19 02:48:18 +00:00
server: render .json urls
This commit is contained in:
parent
22cfcf5357
commit
66b224ad26
|
@ -13,7 +13,7 @@ import moduleAlias from 'babel-plugin-module-alias'
|
||||||
const babelRuntimePath = require.resolve('babel-runtime/package')
|
const babelRuntimePath = require.resolve('babel-runtime/package')
|
||||||
.replace(/[\\\/]package\.json$/, '');
|
.replace(/[\\\/]package\.json$/, '');
|
||||||
|
|
||||||
export function transpile (path, { root = process.cwd() } = {}) {
|
export function transpile (path) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
transformFile(path, {
|
transformFile(path, {
|
||||||
presets: [preset2015, presetReact],
|
presets: [preset2015, presetReact],
|
||||||
|
@ -38,7 +38,7 @@ export function transpile (path, { root = process.cwd() } = {}) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
export function bundle (path, { root = process.cwd() } = {}) {
|
export function bundle (path) {
|
||||||
const fs = new MemoryFS()
|
const fs = new MemoryFS()
|
||||||
|
|
||||||
const compiler = webpack({
|
const compiler = webpack({
|
||||||
|
|
|
@ -66,6 +66,7 @@ export default class Server {
|
||||||
}
|
}
|
||||||
|
|
||||||
async renderJSON (req, res, path) {
|
async renderJSON (req, res, path) {
|
||||||
|
const { root } = this
|
||||||
let json
|
let json
|
||||||
try {
|
try {
|
||||||
json = await renderJSON(path, { root })
|
json = await renderJSON(path, { root })
|
||||||
|
@ -75,9 +76,11 @@ export default class Server {
|
||||||
}
|
}
|
||||||
throw err
|
throw err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const data = JSON.stringify(json)
|
||||||
res.setHeader('Content-Type', 'application/json')
|
res.setHeader('Content-Type', 'application/json')
|
||||||
res.setHeader('Content-Length', Buffer.byteLength(json))
|
res.setHeader('Content-Length', Buffer.byteLength(data))
|
||||||
res.end(json)
|
res.end(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
async render404 (req, res) {
|
async render404 (req, res) {
|
||||||
|
|
|
@ -39,5 +39,8 @@ export async function render (path, req, res, { root = process.cwd() } = {}) {
|
||||||
return '<!DOCTYPE html>' + renderToStaticMarkup(doc)
|
return '<!DOCTYPE html>' + renderToStaticMarkup(doc)
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function renderJSON (path) {
|
export async function renderJSON (path, { root = process.cwd() }) {
|
||||||
|
const bundlePath = resolve(root, '.next', '.next', 'pages', (path || 'index') + '.js')
|
||||||
|
const component = await fs.readFile(bundlePath, 'utf8')
|
||||||
|
return { component }
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue