Commit a3eb7324 authored by JJ Kasper's avatar JJ Kasper Committed by Joe Haddad

Update to return JSON if data request (#9106)

* Update to return json if data request

* Update sending data

* Add test using serverless bundle directly

* Don't early return if fromExport
parent 117d0235
......@@ -165,7 +165,19 @@ const nextServerlessLoader: loader.Loader = function() {
`
: `const nowParams = null;`
}
const result = await renderToHTML(req, res, "${page}", Object.assign({}, unstable_getStaticProps ? {} : parsedUrl.query, nowParams ? nowParams : params, sprData ? { _nextSprData: '1' } : {}), renderOpts)
let result = await renderToHTML(req, res, "${page}", Object.assign({}, unstable_getStaticProps ? {} : parsedUrl.query, nowParams ? nowParams : params, sprData ? { _nextSprData: '1' } : {}), renderOpts)
if (sprData && !fromExport) {
const payload = JSON.stringify(renderOpts.sprData)
res.setHeader('Content-Type', 'application/json')
res.setHeader('Content-Length', Buffer.byteLength(payload))
res.setHeader(
'Cache-Control',
\`s-maxage=\${renderOpts.revalidate}, stale-while-revalidate\`
)
res.end(payload)
return null
}
if (fromExport) return { html: result, renderOpts }
return result
......@@ -190,7 +202,9 @@ const nextServerlessLoader: loader.Loader = function() {
export async function render (req, res) {
try {
const html = await renderReqToHTML(req, res)
sendHTML(req, res, html, {generateEtags: ${generateEtags}})
if (html) {
sendHTML(req, res, html, {generateEtags: ${generateEtags}})
}
} catch(err) {
console.error(err)
res.statusCode = 500
......
const http = require('http')
const url = require('url')
const server = http.createServer((req, res) => {
let { pathname } = url.parse(req.url)
if (pathname.startsWith('/_next/data')) {
pathname = pathname
.replace(`/_next/data/${process.env.BUILD_ID}/`, '/')
.replace(/\.json$/, '')
}
console.log('serving', pathname)
require(`./.next/serverless/pages${pathname}`).render(req, res)
})
server.listen(process.env.PORT, () => {
console.log('ready on', process.env.PORT)
})
......@@ -12,7 +12,8 @@ import {
nextBuild,
nextStart,
nextExport,
startStaticServer
startStaticServer,
initNextServerScript
} from 'next-test-utils'
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 2
......@@ -24,6 +25,23 @@ let buildId
let distPagesDir
let exportDir
const startServer = async (optEnv = {}) => {
const scriptPath = join(appDir, 'server.js')
const env = Object.assign(
{},
{ ...process.env },
{ PORT: `${appPort}` },
optEnv
)
return initNextServerScript(
scriptPath,
/ready on/i,
env,
/ReferenceError: options is not defined/
)
}
const expectedManifestRoutes = () => ({
'/': {
dataRoute: `/_next/data/${buildId}/index.json`,
......@@ -273,10 +291,10 @@ const runTests = (dev = false) => {
})
it('should handle revalidating HTML correctly', async () => {
const route = '/blog/post-1/comment-1'
const route = '/blog/post-2/comment-2'
const initialHtml = await renderViaHTTP(appPort, route)
expect(initialHtml).toMatch(/Post:.*?post-1/)
expect(initialHtml).toMatch(/Comment:.*?comment-1/)
expect(initialHtml).toMatch(/Post:.*?post-2/)
expect(initialHtml).toMatch(/Comment:.*?comment-2/)
let newHtml = await renderViaHTTP(appPort, route)
expect(newHtml).toBe(initialHtml)
......@@ -287,8 +305,8 @@ const runTests = (dev = false) => {
await waitFor(2 * 1000)
newHtml = await renderViaHTTP(appPort, route)
expect(newHtml === initialHtml).toBe(false)
expect(newHtml).toMatch(/Post:.*?post-1/)
expect(newHtml).toMatch(/Comment:.*?comment-1/)
expect(newHtml).toMatch(/Post:.*?post-2/)
expect(newHtml).toMatch(/Comment:.*?comment-2/)
})
it('should handle revalidating JSON correctly', async () => {
......@@ -333,12 +351,26 @@ describe('SPR Prerender', () => {
)
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
app = nextStart(appDir, appPort)
distPagesDir = join(appDir, '.next/serverless/pages')
buildId = await fs.readFile(join(appDir, '.next/BUILD_ID'), 'utf8')
})
afterAll(() => killApp(app))
it('renders data correctly', async () => {
const port = await findPort()
const server = await startServer({
BUILD_ID: buildId,
PORT: port
})
const data = await renderViaHTTP(
port,
`/_next/data/${buildId}/index.json`
)
await killApp(server)
expect(JSON.parse(data).pageProps.world).toBe('world')
})
runTests()
})
......
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