Skip to content

Commit 66b224a

Browse files
committed
server: render .json urls
1 parent 22cfcf5 commit 66b224a

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

server/build.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import moduleAlias from 'babel-plugin-module-alias'
1313
const babelRuntimePath = require.resolve('babel-runtime/package')
1414
.replace(/[\\\/]package\.json$/, '');
1515

16-
export function transpile (path, { root = process.cwd() } = {}) {
16+
export function transpile (path) {
1717
return new Promise((resolve, reject) => {
1818
transformFile(path, {
1919
presets: [preset2015, presetReact],
@@ -38,7 +38,7 @@ export function transpile (path, { root = process.cwd() } = {}) {
3838
})
3939
}
4040

41-
export function bundle (path, { root = process.cwd() } = {}) {
41+
export function bundle (path) {
4242
const fs = new MemoryFS()
4343

4444
const compiler = webpack({

server/index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ export default class Server {
6666
}
6767

6868
async renderJSON (req, res, path) {
69+
const { root } = this
6970
let json
7071
try {
7172
json = await renderJSON(path, { root })
@@ -75,9 +76,11 @@ export default class Server {
7576
}
7677
throw err
7778
}
79+
80+
const data = JSON.stringify(json)
7881
res.setHeader('Content-Type', 'application/json')
79-
res.setHeader('Content-Length', Buffer.byteLength(json))
80-
res.end(json)
82+
res.setHeader('Content-Length', Buffer.byteLength(data))
83+
res.end(data)
8184
}
8285

8386
async render404 (req, res) {

server/render.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,8 @@ export async function render (path, req, res, { root = process.cwd() } = {}) {
3939
return '<!DOCTYPE html>' + renderToStaticMarkup(doc)
4040
}
4141

42-
export async function renderJSON (path) {
42+
export async function renderJSON (path, { root = process.cwd() }) {
43+
const bundlePath = resolve(root, '.next', '.next', 'pages', (path || 'index') + '.js')
44+
const component = await fs.readFile(bundlePath, 'utf8')
45+
return { component }
4346
}

0 commit comments

Comments
 (0)