1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00

Fix Runtime Config in next export (#6258)

This commit is contained in:
Connor Davis 2019-02-11 19:28:47 -06:00 committed by GitHub
parent 33b9ebc783
commit bd249180c6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 30 additions and 10 deletions

View file

@ -7,7 +7,6 @@ import { resolve, join } from 'path'
import { existsSync, readFileSync } from 'fs' import { existsSync, readFileSync } from 'fs'
import loadConfig from 'next-server/next-config' import loadConfig from 'next-server/next-config'
import { PHASE_EXPORT, SERVER_DIRECTORY, PAGES_MANIFEST, CONFIG_FILE, BUILD_ID_FILE, CLIENT_STATIC_FILES_PATH } from 'next-server/constants' import { PHASE_EXPORT, SERVER_DIRECTORY, PAGES_MANIFEST, CONFIG_FILE, BUILD_ID_FILE, CLIENT_STATIC_FILES_PATH } from 'next-server/constants'
import * as envConfig from 'next-server/config'
import createProgress from 'tty-aware-progress' import createProgress from 'tty-aware-progress'
export default async function (dir, options, configuration) { export default async function (dir, options, configuration) {
@ -98,11 +97,6 @@ export default async function (dir, options, configuration) {
renderOpts.runtimeConfig = publicRuntimeConfig renderOpts.runtimeConfig = publicRuntimeConfig
} }
envConfig.setConfig({
serverRuntimeConfig,
publicRuntimeConfig
})
// We need this for server rendering the Link component. // We need this for server rendering the Link component.
global.__NEXT_DATA__ = { global.__NEXT_DATA__ = {
nextExport: true nextExport: true
@ -138,6 +132,7 @@ export default async function (dir, options, configuration) {
exportPathMap: chunk.pathMap, exportPathMap: chunk.pathMap,
outDir, outDir,
renderOpts, renderOpts,
serverRuntimeConfig,
concurrency concurrency
}) })
worker.on('message', ({ type, payload }) => { worker.on('message', ({ type, payload }) => {

View file

@ -8,6 +8,7 @@ const { renderToHTML } = require('next-server/dist/server/render')
const { writeFile } = require('fs') const { writeFile } = require('fs')
const Sema = require('async-sema') const Sema = require('async-sema')
const {loadComponents} = require('next-server/dist/server/load-components') const {loadComponents} = require('next-server/dist/server/load-components')
const envConfig = require('next-server/config')
process.on( process.on(
'message', 'message',
@ -18,6 +19,7 @@ process.on(
exportPathMap, exportPathMap,
outDir, outDir,
renderOpts, renderOpts,
serverRuntimeConfig,
concurrency concurrency
}) => { }) => {
const sema = new Sema(concurrency, { capacity: exportPaths.length }) const sema = new Sema(concurrency, { capacity: exportPaths.length })
@ -27,6 +29,10 @@ process.on(
const { page, query = {} } = exportPathMap[path] const { page, query = {} } = exportPathMap[path]
const req = { url: path } const req = { url: path }
const res = {} const res = {}
envConfig.setConfig({
serverRuntimeConfig,
publicRuntimeConfig: renderOpts.runtimeConfig
})
let htmlFilename = `${path}${sep}index.html` let htmlFilename = `${path}${sep}index.html`
if (extname(path) !== '') { if (extname(path) !== '') {

View file

@ -3,6 +3,12 @@ const {PHASE_DEVELOPMENT_SERVER} = require('next-server/constants')
module.exports = (phase) => { module.exports = (phase) => {
return { return {
distDir: phase === PHASE_DEVELOPMENT_SERVER ? '.next-dev' : '.next', distDir: phase === PHASE_DEVELOPMENT_SERVER ? '.next-dev' : '.next',
publicRuntimeConfig: {
foo: 'foo'
},
serverRuntimeConfig: {
bar: 'bar'
},
exportPathMap: function () { exportPathMap: function () {
return { return {
'/': { page: '/' }, '/': { page: '/' },

View file

@ -1,12 +1,20 @@
import Link from 'next/link' import Link from 'next/link'
import getConfig from 'next/config'
const { publicRuntimeConfig, serverRuntimeConfig } = getConfig()
export default () => ( const About = ({ bar }) => (
<div id='about-page'> <div id='about-page'>
<div> <div>
<Link href='/'> <Link href='/'>
<a>Go Back</a> <a>Go Back</a>
</Link> </Link>
</div> </div>
<p>This is the About page</p> <p>{`This is the About page ${publicRuntimeConfig.foo}${bar || ''}`}</p>
</div> </div>
) )
About.getInitialProps = async (ctx) => {
return { bar: serverRuntimeConfig.bar }
}
export default About

View file

@ -20,7 +20,7 @@ export default function (context) {
.waitForElementByCss('#about-page') .waitForElementByCss('#about-page')
.elementByCss('#about-page p').text() .elementByCss('#about-page p').text()
expect(text).toBe('This is the About page') expect(text).toBe('This is the About page foo')
browser.close() browser.close()
}) })
@ -31,7 +31,7 @@ export default function (context) {
.waitForElementByCss('#about-page') .waitForElementByCss('#about-page')
.elementByCss('#about-page p').text() .elementByCss('#about-page p').text()
expect(text).toBe('This is the About page') expect(text).toBe('This is the About page foo')
browser.close() browser.close()
}) })

View file

@ -9,6 +9,11 @@ export default function (context) {
expect(html).toMatch(/This is the home page/) expect(html).toMatch(/This is the home page/)
}) })
it('should render the about page', async () => {
const html = await renderViaHTTP(context.port, '/about')
expect(html).toMatch(/This is the About page foobar/)
})
it('should render links correctly', async () => { it('should render links correctly', async () => {
const html = await renderViaHTTP(context.port, '/') const html = await renderViaHTTP(context.port, '/')
const $ = cheerio.load(html) const $ = cheerio.load(html)