From bd249180c67e96e609bff92fe3e751015a94861f Mon Sep 17 00:00:00 2001 From: Connor Davis Date: Mon, 11 Feb 2019 19:28:47 -0600 Subject: [PATCH] Fix Runtime Config in `next export` (#6258) --- packages/next/export/index.js | 7 +------ packages/next/export/worker.js | 6 ++++++ test/integration/export/next.config.js | 6 ++++++ test/integration/export/pages/about.js | 12 ++++++++++-- test/integration/export/test/browser.js | 4 ++-- test/integration/export/test/ssr.js | 5 +++++ 6 files changed, 30 insertions(+), 10 deletions(-) diff --git a/packages/next/export/index.js b/packages/next/export/index.js index abcb0024..cb980229 100644 --- a/packages/next/export/index.js +++ b/packages/next/export/index.js @@ -7,7 +7,6 @@ import { resolve, join } from 'path' import { existsSync, readFileSync } from 'fs' 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 * as envConfig from 'next-server/config' import createProgress from 'tty-aware-progress' export default async function (dir, options, configuration) { @@ -98,11 +97,6 @@ export default async function (dir, options, configuration) { renderOpts.runtimeConfig = publicRuntimeConfig } - envConfig.setConfig({ - serverRuntimeConfig, - publicRuntimeConfig - }) - // We need this for server rendering the Link component. global.__NEXT_DATA__ = { nextExport: true @@ -138,6 +132,7 @@ export default async function (dir, options, configuration) { exportPathMap: chunk.pathMap, outDir, renderOpts, + serverRuntimeConfig, concurrency }) worker.on('message', ({ type, payload }) => { diff --git a/packages/next/export/worker.js b/packages/next/export/worker.js index 50ad33d1..059fc771 100644 --- a/packages/next/export/worker.js +++ b/packages/next/export/worker.js @@ -8,6 +8,7 @@ const { renderToHTML } = require('next-server/dist/server/render') const { writeFile } = require('fs') const Sema = require('async-sema') const {loadComponents} = require('next-server/dist/server/load-components') +const envConfig = require('next-server/config') process.on( 'message', @@ -18,6 +19,7 @@ process.on( exportPathMap, outDir, renderOpts, + serverRuntimeConfig, concurrency }) => { const sema = new Sema(concurrency, { capacity: exportPaths.length }) @@ -27,6 +29,10 @@ process.on( const { page, query = {} } = exportPathMap[path] const req = { url: path } const res = {} + envConfig.setConfig({ + serverRuntimeConfig, + publicRuntimeConfig: renderOpts.runtimeConfig + }) let htmlFilename = `${path}${sep}index.html` if (extname(path) !== '') { diff --git a/test/integration/export/next.config.js b/test/integration/export/next.config.js index ac101b11..b741a4b5 100644 --- a/test/integration/export/next.config.js +++ b/test/integration/export/next.config.js @@ -3,6 +3,12 @@ const {PHASE_DEVELOPMENT_SERVER} = require('next-server/constants') module.exports = (phase) => { return { distDir: phase === PHASE_DEVELOPMENT_SERVER ? '.next-dev' : '.next', + publicRuntimeConfig: { + foo: 'foo' + }, + serverRuntimeConfig: { + bar: 'bar' + }, exportPathMap: function () { return { '/': { page: '/' }, diff --git a/test/integration/export/pages/about.js b/test/integration/export/pages/about.js index 7ecf1d59..269e520a 100644 --- a/test/integration/export/pages/about.js +++ b/test/integration/export/pages/about.js @@ -1,12 +1,20 @@ import Link from 'next/link' +import getConfig from 'next/config' +const { publicRuntimeConfig, serverRuntimeConfig } = getConfig() -export default () => ( +const About = ({ bar }) => (
Go Back
-

This is the About page

+

{`This is the About page ${publicRuntimeConfig.foo}${bar || ''}`}

) + +About.getInitialProps = async (ctx) => { + return { bar: serverRuntimeConfig.bar } +} + +export default About diff --git a/test/integration/export/test/browser.js b/test/integration/export/test/browser.js index e594f5c3..b11f2499 100644 --- a/test/integration/export/test/browser.js +++ b/test/integration/export/test/browser.js @@ -20,7 +20,7 @@ export default function (context) { .waitForElementByCss('#about-page') .elementByCss('#about-page p').text() - expect(text).toBe('This is the About page') + expect(text).toBe('This is the About page foo') browser.close() }) @@ -31,7 +31,7 @@ export default function (context) { .waitForElementByCss('#about-page') .elementByCss('#about-page p').text() - expect(text).toBe('This is the About page') + expect(text).toBe('This is the About page foo') browser.close() }) diff --git a/test/integration/export/test/ssr.js b/test/integration/export/test/ssr.js index d9c88aa2..84c30d4b 100644 --- a/test/integration/export/test/ssr.js +++ b/test/integration/export/test/ssr.js @@ -9,6 +9,11 @@ export default function (context) { 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 () => { const html = await renderViaHTTP(context.port, '/') const $ = cheerio.load(html)