From b8d075ef0e68b8407644b17813c510b4e30fd9ff Mon Sep 17 00:00:00 2001 From: Tim Neutkens Date: Tue, 31 Mar 2020 17:53:50 +0200 Subject: [PATCH] Update environment support (#11524) * Remove req.env * Rename NEXT_APP_ to NEXT_PUBLIC_ * Remove key exposing env config * Update tests * Update NEXT_APP_ test to NEXT_PUBLIC_ --- packages/next/build/webpack-config.ts | 2 +- .../webpack/loaders/next-serverless-loader.ts | 1 - packages/next/export/index.ts | 3 +- packages/next/lib/load-env-config.ts | 4 +- packages/next/next-server/server/api-utils.ts | 5 - .../next/next-server/server/next-server.ts | 7 +- packages/next/next-server/server/render.tsx | 8 - packages/next/next-server/server/utils.ts | 26 --- packages/next/types/index.d.ts | 4 - .../env-config-errors/app/package.json | 3 - .../env-config-errors/app/pages/api/hello.js | 5 - .../env-config-errors/app/pages/index.js | 14 -- .../env-config-errors/app/pages/ssp.js | 13 -- .../env-config-errors/test/index.test.js | 150 ------------------ test/integration/env-config/app/.env | 2 +- .../env-config/app/pages/api/all.js | 46 +++--- .../env-config/app/pages/global.js | 2 +- .../integration/env-config/app/pages/index.js | 50 +++--- .../env-config/app/pages/some-ssg.js | 50 +++--- .../env-config/app/pages/some-ssp.js | 50 +++--- test/integration/env-config/app/pages/ssp.js | 49 +++--- .../integration/env-config/test/index.test.js | 2 +- 22 files changed, 146 insertions(+), 350 deletions(-) delete mode 100644 test/integration/env-config-errors/app/package.json delete mode 100644 test/integration/env-config-errors/app/pages/api/hello.js delete mode 100644 test/integration/env-config-errors/app/pages/index.js delete mode 100644 test/integration/env-config-errors/app/pages/ssp.js delete mode 100644 test/integration/env-config-errors/test/index.test.js diff --git a/packages/next/build/webpack-config.ts b/packages/next/build/webpack-config.ts index 0c6e46c343..4f9b720298 100644 --- a/packages/next/build/webpack-config.ts +++ b/packages/next/build/webpack-config.ts @@ -716,7 +716,7 @@ export default async function getBaseWebpackConfig( ...(config.experimental.pageEnv ? Object.keys(process.env).reduce( (prev: { [key: string]: string }, key: string) => { - if (key.startsWith('NEXT_APP_')) { + if (key.startsWith('NEXT_PUBLIC_')) { prev[key] = process.env[key]! } return prev diff --git a/packages/next/build/webpack/loaders/next-serverless-loader.ts b/packages/next/build/webpack/loaders/next-serverless-loader.ts index 2c7b682ffe..820b0fd432 100644 --- a/packages/next/build/webpack/loaders/next-serverless-loader.ts +++ b/packages/next/build/webpack/loaders/next-serverless-loader.ts @@ -181,7 +181,6 @@ const nextServerlessLoader: loader.Loader = function() { Object.assign({}, parsedUrl.query, params ), resolver, ${encodedPreviewProps}, - process.env, onError ) } catch (err) { diff --git a/packages/next/export/index.ts b/packages/next/export/index.ts index db4e666f82..c202c2d50e 100644 --- a/packages/next/export/index.ts +++ b/packages/next/export/index.ts @@ -226,12 +226,13 @@ export default async function( } } + loadEnvConfig(dir) + // Start the rendering process const renderOpts = { dir, buildId, nextExport: true, - env: loadEnvConfig(dir), assetPrefix: nextConfig.assetPrefix.replace(/\/$/, ''), distDir, dev: false, diff --git a/packages/next/lib/load-env-config.ts b/packages/next/lib/load-env-config.ts index 0593a1e40c..7529468ada 100644 --- a/packages/next/lib/load-env-config.ts +++ b/packages/next/lib/load-env-config.ts @@ -70,10 +70,10 @@ export function loadEnvConfig(dir: string, dev?: boolean): Env | false { } } - // load global env values prefixed with `NEXT_APP_` to process.env + // load global env values prefixed with `NEXT_PUBLIC_` to process.env for (const key of Object.keys(combinedEnv)) { if ( - key.startsWith('NEXT_APP_') && + key.startsWith('NEXT_PUBLIC_') && typeof process.env[key] === 'undefined' ) { process.env[key] = combinedEnv[key] diff --git a/packages/next/next-server/server/api-utils.ts b/packages/next/next-server/server/api-utils.ts index 778a8b3f27..6a5be7521e 100644 --- a/packages/next/next-server/server/api-utils.ts +++ b/packages/next/next-server/server/api-utils.ts @@ -8,8 +8,6 @@ import { isResSent, NextApiRequest, NextApiResponse } from '../lib/utils' import { decryptWithSecret, encryptWithSecret } from './crypto-utils' import { interopDefault } from './load-components' import { Params } from './router' -import { collectEnv } from './utils' -import { Env } from '../../lib/load-env-config' export type NextApiRequestCookies = { [key: string]: string } export type NextApiRequestQuery = { [key: string]: string | string[] } @@ -26,7 +24,6 @@ export async function apiResolver( params: any, resolverModule: any, apiContext: __ApiPreviewProps, - env: Env | false, onError?: ({ err }: { err: any }) => Promise ) { const apiReq = req as NextApiRequest @@ -41,8 +38,6 @@ export async function apiResolver( const config: PageConfig = resolverModule.config || {} const bodyParser = config.api?.bodyParser !== false - apiReq.env = env ? collectEnv(req.url!, env, config.env) : {} - // Parsing of cookies setLazyProp({ req: apiReq }, 'cookies', getCookieParser(req)) // Parsing query string diff --git a/packages/next/next-server/server/next-server.ts b/packages/next/next-server/server/next-server.ts index a90ae425f5..79cc1e0203 100644 --- a/packages/next/next-server/server/next-server.ts +++ b/packages/next/next-server/server/next-server.ts @@ -61,7 +61,7 @@ import { setSprCache, } from './spr-cache' import { isBlockedPage } from './utils' -import { loadEnvConfig, Env } from '../../lib/load-env-config' +import { loadEnvConfig } from '../../lib/load-env-config' const getCustomRouteMatcher = pathMatch(true) @@ -118,7 +118,6 @@ export default class Server { documentMiddlewareEnabled: boolean hasCssMode: boolean dev?: boolean - env: Env | false previewProps: __ApiPreviewProps customServer?: boolean ampOptimizerConfig?: { [key: string]: any } @@ -147,7 +146,7 @@ export default class Server { this.dir = resolve(dir) this.quiet = quiet const phase = this.currentPhase() - const env = loadEnvConfig(this.dir, dev) + loadEnvConfig(this.dir, dev) this.nextConfig = loadConfig(phase, this.dir, conf) this.distDir = join(this.dir, this.nextConfig.distDir) @@ -175,7 +174,6 @@ export default class Server { staticMarkup, buildId: this.buildId, generateEtags, - env: this.nextConfig.experimental.pageEnv && env, previewProps: this.getPreviewProps(), customServer: customServer === true ? true : undefined, ampOptimizerConfig: this.nextConfig.experimental.amp?.optimizer, @@ -693,7 +691,6 @@ export default class Server { query, pageModule, this.renderOpts.previewProps, - this.renderOpts.env, this.onErrorMiddleware ) return true diff --git a/packages/next/next-server/server/render.tsx b/packages/next/next-server/server/render.tsx index 787e538acb..9919c5ef4e 100644 --- a/packages/next/next-server/server/render.tsx +++ b/packages/next/next-server/server/render.tsx @@ -38,8 +38,6 @@ import { tryGetPreviewData, __ApiPreviewProps } from './api-utils' import { getPageFiles } from './get-page-files' import { LoadComponentsReturnType, ManifestItem } from './load-components' import optimizeAmp from './optimize-amp' -import { collectEnv } from './utils' -import { Env } from '../../lib/load-env-config' import { UnwrapPromise } from '../../lib/coalesced-function' import { GetStaticProps, GetServerSideProps } from '../../types' @@ -156,7 +154,6 @@ export type RenderOptsPartial = { isDataReq?: boolean params?: ParsedUrlQuery previewProps: __ApiPreviewProps - env: Env | false } export type RenderOpts = LoadComponentsReturnType & RenderOptsPartial @@ -291,7 +288,6 @@ export async function renderToHTML( staticMarkup = false, ampPath = '', App, - env = {}, Document, pageConfig = {}, DocumentMiddleware, @@ -307,8 +303,6 @@ export async function renderToHTML( previewProps, } = renderOpts - const curEnv = env ? collectEnv(pathname, env, pageConfig.env) : {} - const callMiddleware = async (method: string, args: any[], props = false) => { let results: any = props ? {} : [] @@ -509,7 +503,6 @@ export async function renderToHTML( try { data = await getStaticProps!({ - env: curEnv, ...(pageIsDynamic ? { params: query as ParsedUrlQuery } : undefined), ...(previewData !== false ? { preview: true, previewData: previewData } @@ -592,7 +585,6 @@ export async function renderToHTML( req, res, query, - env: curEnv, ...(pageIsDynamic ? { params: params as ParsedUrlQuery } : undefined), ...(previewData !== false ? { preview: true, previewData: previewData } diff --git a/packages/next/next-server/server/utils.ts b/packages/next/next-server/server/utils.ts index 9465f87e5d..452fa42091 100644 --- a/packages/next/next-server/server/utils.ts +++ b/packages/next/next-server/server/utils.ts @@ -1,5 +1,4 @@ import { BLOCKED_PAGES } from '../lib/constants' -import { Env } from '../../lib/load-env-config' export function isBlockedPage(pathname: string): boolean { return BLOCKED_PAGES.indexOf(pathname) !== -1 @@ -15,28 +14,3 @@ export function cleanAmpPath(pathname: string): string { pathname = pathname.replace(/\?$/, '') return pathname } - -export function collectEnv(page: string, env: Env, pageEnv?: string[]): Env { - const missingEnvKeys = new Set() - const collected = pageEnv - ? pageEnv.reduce((prev: Env, key): Env => { - if (typeof env[key] !== 'undefined') { - prev[key] = env[key]! - } else { - missingEnvKeys.add(key) - } - return prev - }, {}) - : {} - - if (missingEnvKeys.size > 0) { - console.warn( - `Missing env value${missingEnvKeys.size === 1 ? '' : 's'}: ${[ - ...missingEnvKeys, - ].join(', ')} for ${page}.\n` + - `Make sure to supply this value in either your .env file or in your environment.\n` + - `See here for more info: https://err.sh/next.js/missing-env-value` - ) - } - return collected -} diff --git a/packages/next/types/index.d.ts b/packages/next/types/index.d.ts index b74271a2b8..e5c41a7e4b 100644 --- a/packages/next/types/index.d.ts +++ b/packages/next/types/index.d.ts @@ -5,8 +5,6 @@ import React from 'react' import { ParsedUrlQuery } from 'querystring' import { IncomingMessage, ServerResponse } from 'http' -// @ts-ignore This path is generated at build time and conflicts otherwise -import { Env } from '../dist/lib/load-env-config' import { NextPageContext, @@ -73,7 +71,6 @@ export type GetStaticProps< params?: ParsedUrlQuery preview?: boolean previewData?: any - env: Env }) => Promise<{ props: P revalidate?: number | boolean @@ -91,7 +88,6 @@ export type GetServerSideProps< res: ServerResponse params?: ParsedUrlQuery query: ParsedUrlQuery - env: Env preview?: boolean previewData?: any }) => Promise<{ props: P }> diff --git a/test/integration/env-config-errors/app/package.json b/test/integration/env-config-errors/app/package.json deleted file mode 100644 index 9f2c5a7558..0000000000 --- a/test/integration/env-config-errors/app/package.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "name": "env-config-errors" -} diff --git a/test/integration/env-config-errors/app/pages/api/hello.js b/test/integration/env-config-errors/app/pages/api/hello.js deleted file mode 100644 index 23d11b18a6..0000000000 --- a/test/integration/env-config-errors/app/pages/api/hello.js +++ /dev/null @@ -1,5 +0,0 @@ -export const config = { - env: ['NOTION_KEY', 'SENTRY_DSN', 'DATABASE_KEY', 'DATABASE_USER'], -} - -export default (req, res) => res.json(req.env) diff --git a/test/integration/env-config-errors/app/pages/index.js b/test/integration/env-config-errors/app/pages/index.js deleted file mode 100644 index 3a943794b2..0000000000 --- a/test/integration/env-config-errors/app/pages/index.js +++ /dev/null @@ -1,14 +0,0 @@ -export const config = { - env: ['NOTION_KEY', 'SENTRY_DSN', 'DATABASE_KEY', 'DATABASE_USER'], -} - -export async function getStaticProps({ env }) { - return { - // Do not pass any sensitive values here as they will - // be made PUBLICLY available in `pageProps` - props: { env }, - revalidate: 1, - } -} - -export default ({ env }) =>

{JSON.stringify(env)}

diff --git a/test/integration/env-config-errors/app/pages/ssp.js b/test/integration/env-config-errors/app/pages/ssp.js deleted file mode 100644 index 325dc7fc77..0000000000 --- a/test/integration/env-config-errors/app/pages/ssp.js +++ /dev/null @@ -1,13 +0,0 @@ -export const config = { - env: ['NOTION_KEY', 'SENTRY_DSN', 'DATABASE_KEY', 'DATABASE_USER'], -} - -export async function getServerSideProps({ env }) { - return { - // Do not pass any sensitive values here as they will - // be made PUBLICLY available in `pageProps` - props: { env }, - } -} - -export default ({ env }) =>

{JSON.stringify(env)}

diff --git a/test/integration/env-config-errors/test/index.test.js b/test/integration/env-config-errors/test/index.test.js deleted file mode 100644 index 899764abbd..0000000000 --- a/test/integration/env-config-errors/test/index.test.js +++ /dev/null @@ -1,150 +0,0 @@ -/* eslint-env jest */ -/* global jasmine */ -import fs from 'fs-extra' -import { join } from 'path' -import { - nextBuild, - findPort, - launchApp, - killApp, - nextStart, - renderViaHTTP, -} from 'next-test-utils' - -jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 2 - -const appDir = join(__dirname, '../app') -const envFile = join(appDir, '.env') -const nextConfig = join(appDir, 'next.config.js') -const nextConfigContent = ` - experimental: { - pageEnv: true - } -` - -let app -let appPort -let output = '' - -const envValues = ['NOTION_KEY', 'SENTRY_DSN', 'DATABASE_KEY', 'DATABASE_USER'] - -const writeEnv = () => - fs.writeFile(envFile, envValues.map(val => `${val}=value`).join('\n')) -const rmEnv = () => fs.remove(envFile) - -const runTests = (isDev = false) => { - const startApp = async () => { - output = '' - appPort = await findPort() - let method = isDev ? launchApp : nextStart - - app = await method(appDir, appPort, { - onStdout(msg) { - output += msg - }, - onStderr(msg) { - output += msg - }, - }) - } - - if (isDev) { - it('should warn for missing values on SSG page', async () => { - await startApp() - await renderViaHTTP(appPort, '/') - await killApp(app) - expect(output).toContain( - `Missing env values: ${envValues.join(', ')} for /` - ) - }) - - it('should not warn for missing values on SSG page', async () => { - await writeEnv() - await startApp() - await renderViaHTTP(appPort, '/') - await killApp(app) - await rmEnv() - expect(output).not.toContain( - `Missing env values: ${envValues.join(', ')} for /` - ) - }) - } - - it('should warn for missing values on server props page', async () => { - await startApp() - await renderViaHTTP(appPort, '/ssp') - await killApp(app) - expect(output).toContain( - `Missing env values: ${envValues.join(', ')} for /ssp` - ) - }) - - it('should not warn for missing values on server props page', async () => { - await writeEnv() - await startApp() - await renderViaHTTP(appPort, '/ssp') - await killApp(app) - await rmEnv() - expect(output).not.toContain( - `Missing env values: ${envValues.join(', ')} for /ssp` - ) - }) - - it('should warn for missing values on API route', async () => { - await startApp() - await renderViaHTTP(appPort, '/api/hello') - await killApp(app) - expect(output).toContain( - `Missing env values: ${envValues.join(', ')} for /api/hello` - ) - }) - - it('should not warn for missing values on API route', async () => { - await writeEnv() - await startApp() - await renderViaHTTP(appPort, '/api/hello') - await killApp(app) - await rmEnv() - expect(output).not.toContain( - `Missing env values: ${envValues.join(', ')} for /api/hello` - ) - }) -} - -describe('Env Config', () => { - afterEach(async () => { - await fs.remove(envFile) - try { - await killApp(app) - } catch (_) {} - }) - afterAll(() => fs.remove(nextConfig)) - - describe('dev mode', () => { - beforeAll(() => - fs.writeFile(nextConfig, `module.exports = { ${nextConfigContent} }`) - ) - runTests(true) - }) - - describe('server mode', () => { - beforeAll(async () => { - beforeAll(() => - fs.writeFile(nextConfig, `module.exports = { ${nextConfigContent} }`) - ) - await nextBuild(appDir) - }) - runTests() - }) - - describe('serverless mode', () => { - beforeAll(async () => { - await fs.writeFile( - nextConfig, - `module.exports = { target: 'experimental-serverless-trace', ${nextConfigContent} }` - ) - await nextBuild(appDir) - }) - runTests() - }) -}) diff --git a/test/integration/env-config/app/.env b/test/integration/env-config/app/.env index 331912846c..edeb6e1217 100644 --- a/test/integration/env-config/app/.env +++ b/test/integration/env-config/app/.env @@ -7,4 +7,4 @@ ENV_FILE_PRODUCTION_OVERRIDEOVERRIDE_TEST=env ENV_FILE_PRODUCTION_LOCAL_OVERRIDEOVERRIDE_TEST=env ENV_FILE_TEST_OVERRIDE_TEST=env ENV_FILE_TEST_LOCAL_OVERRIDEOVERRIDE_TEST=env -NEXT_APP_TEST_DEST=another \ No newline at end of file +NEXT_PUBLIC_TEST_DEST=another \ No newline at end of file diff --git a/test/integration/env-config/app/pages/api/all.js b/test/integration/env-config/app/pages/api/all.js index 9c6743b794..f52313245a 100644 --- a/test/integration/env-config/app/pages/api/all.js +++ b/test/integration/env-config/app/pages/api/all.js @@ -1,25 +1,29 @@ -export const config = { - env: [ - 'PROCESS_ENV_KEY', - 'ENV_FILE_KEY', - 'LOCAL_ENV_FILE_KEY', - 'ENV_FILE_LOCAL_OVERRIDE_TEST', - 'PRODUCTION_ENV_FILE_KEY', - 'LOCAL_PRODUCTION_ENV_FILE_KEY', - 'DEVELOPMENT_ENV_FILE_KEY', - 'LOCAL_DEVELOPMENT_ENV_FILE_KEY', - 'ENV_FILE_DEVELOPMENT_OVERRIDE_TEST', - 'ENV_FILE_DEVELOPMENT_LOCAL_OVERRIDEOVERRIDE_TEST', - 'ENV_FILE_PRODUCTION_OVERRIDEOVERRIDE_TEST', - 'ENV_FILE_PRODUCTION_LOCAL_OVERRIDEOVERRIDE_TEST', - 'TEST_ENV_FILE_KEY', - 'LOCAL_TEST_ENV_FILE_KEY', - 'ENV_FILE_TEST_OVERRIDE_TEST', - 'ENV_FILE_TEST_LOCAL_OVERRIDEOVERRIDE_TEST', - ], -} +const variables = [ + 'PROCESS_ENV_KEY', + 'ENV_FILE_KEY', + 'LOCAL_ENV_FILE_KEY', + 'ENV_FILE_LOCAL_OVERRIDE_TEST', + 'PRODUCTION_ENV_FILE_KEY', + 'LOCAL_PRODUCTION_ENV_FILE_KEY', + 'DEVELOPMENT_ENV_FILE_KEY', + 'LOCAL_DEVELOPMENT_ENV_FILE_KEY', + 'ENV_FILE_DEVELOPMENT_OVERRIDE_TEST', + 'ENV_FILE_DEVELOPMENT_LOCAL_OVERRIDEOVERRIDE_TEST', + 'ENV_FILE_PRODUCTION_OVERRIDEOVERRIDE_TEST', + 'ENV_FILE_PRODUCTION_LOCAL_OVERRIDEOVERRIDE_TEST', + 'TEST_ENV_FILE_KEY', + 'LOCAL_TEST_ENV_FILE_KEY', + 'ENV_FILE_TEST_OVERRIDE_TEST', + 'ENV_FILE_TEST_LOCAL_OVERRIDEOVERRIDE_TEST', +] + +const items = {} + +variables.forEach(variable => { + items[variable] = process.env[variable] +}) export default async (req, res) => { // Only for testing, don't do this... - res.json(req.env) + res.json(items) } diff --git a/test/integration/env-config/app/pages/global.js b/test/integration/env-config/app/pages/global.js index bb85ce9a05..ded4746e31 100644 --- a/test/integration/env-config/app/pages/global.js +++ b/test/integration/env-config/app/pages/global.js @@ -1 +1 @@ -export default () =>

{process.env.NEXT_APP_TEST_DEST}

+export default () =>

{process.env.NEXT_PUBLIC_TEST_DEST}

diff --git a/test/integration/env-config/app/pages/index.js b/test/integration/env-config/app/pages/index.js index a276f47118..e02e79b019 100644 --- a/test/integration/env-config/app/pages/index.js +++ b/test/integration/env-config/app/pages/index.js @@ -1,29 +1,35 @@ -export const config = { - env: [ - 'PROCESS_ENV_KEY', - 'ENV_FILE_KEY', - 'LOCAL_ENV_FILE_KEY', - 'ENV_FILE_LOCAL_OVERRIDE_TEST', - 'PRODUCTION_ENV_FILE_KEY', - 'LOCAL_PRODUCTION_ENV_FILE_KEY', - 'DEVELOPMENT_ENV_FILE_KEY', - 'LOCAL_DEVELOPMENT_ENV_FILE_KEY', - 'ENV_FILE_DEVELOPMENT_OVERRIDE_TEST', - 'ENV_FILE_DEVELOPMENT_LOCAL_OVERRIDEOVERRIDE_TEST', - 'ENV_FILE_PRODUCTION_OVERRIDEOVERRIDE_TEST', - 'ENV_FILE_PRODUCTION_LOCAL_OVERRIDEOVERRIDE_TEST', - 'TEST_ENV_FILE_KEY', - 'LOCAL_TEST_ENV_FILE_KEY', - 'ENV_FILE_TEST_OVERRIDE_TEST', - 'ENV_FILE_TEST_LOCAL_OVERRIDEOVERRIDE_TEST', - ], -} +const variables = [ + 'PROCESS_ENV_KEY', + 'ENV_FILE_KEY', + 'LOCAL_ENV_FILE_KEY', + 'ENV_FILE_LOCAL_OVERRIDE_TEST', + 'PRODUCTION_ENV_FILE_KEY', + 'LOCAL_PRODUCTION_ENV_FILE_KEY', + 'DEVELOPMENT_ENV_FILE_KEY', + 'LOCAL_DEVELOPMENT_ENV_FILE_KEY', + 'ENV_FILE_DEVELOPMENT_OVERRIDE_TEST', + 'ENV_FILE_DEVELOPMENT_LOCAL_OVERRIDEOVERRIDE_TEST', + 'ENV_FILE_PRODUCTION_OVERRIDEOVERRIDE_TEST', + 'ENV_FILE_PRODUCTION_LOCAL_OVERRIDEOVERRIDE_TEST', + 'TEST_ENV_FILE_KEY', + 'LOCAL_TEST_ENV_FILE_KEY', + 'ENV_FILE_TEST_OVERRIDE_TEST', + 'ENV_FILE_TEST_LOCAL_OVERRIDEOVERRIDE_TEST', +] + +export async function getStaticProps() { + const items = {} + + variables.forEach(variable => { + if (process.env[variable]) { + items[variable] = process.env[variable] + } + }) -export async function getStaticProps({ env }) { return { // Do not pass any sensitive values here as they will // be made PUBLICLY available in `pageProps` - props: { env }, + props: { env: items }, revalidate: 1, } } diff --git a/test/integration/env-config/app/pages/some-ssg.js b/test/integration/env-config/app/pages/some-ssg.js index a276f47118..e02e79b019 100644 --- a/test/integration/env-config/app/pages/some-ssg.js +++ b/test/integration/env-config/app/pages/some-ssg.js @@ -1,29 +1,35 @@ -export const config = { - env: [ - 'PROCESS_ENV_KEY', - 'ENV_FILE_KEY', - 'LOCAL_ENV_FILE_KEY', - 'ENV_FILE_LOCAL_OVERRIDE_TEST', - 'PRODUCTION_ENV_FILE_KEY', - 'LOCAL_PRODUCTION_ENV_FILE_KEY', - 'DEVELOPMENT_ENV_FILE_KEY', - 'LOCAL_DEVELOPMENT_ENV_FILE_KEY', - 'ENV_FILE_DEVELOPMENT_OVERRIDE_TEST', - 'ENV_FILE_DEVELOPMENT_LOCAL_OVERRIDEOVERRIDE_TEST', - 'ENV_FILE_PRODUCTION_OVERRIDEOVERRIDE_TEST', - 'ENV_FILE_PRODUCTION_LOCAL_OVERRIDEOVERRIDE_TEST', - 'TEST_ENV_FILE_KEY', - 'LOCAL_TEST_ENV_FILE_KEY', - 'ENV_FILE_TEST_OVERRIDE_TEST', - 'ENV_FILE_TEST_LOCAL_OVERRIDEOVERRIDE_TEST', - ], -} +const variables = [ + 'PROCESS_ENV_KEY', + 'ENV_FILE_KEY', + 'LOCAL_ENV_FILE_KEY', + 'ENV_FILE_LOCAL_OVERRIDE_TEST', + 'PRODUCTION_ENV_FILE_KEY', + 'LOCAL_PRODUCTION_ENV_FILE_KEY', + 'DEVELOPMENT_ENV_FILE_KEY', + 'LOCAL_DEVELOPMENT_ENV_FILE_KEY', + 'ENV_FILE_DEVELOPMENT_OVERRIDE_TEST', + 'ENV_FILE_DEVELOPMENT_LOCAL_OVERRIDEOVERRIDE_TEST', + 'ENV_FILE_PRODUCTION_OVERRIDEOVERRIDE_TEST', + 'ENV_FILE_PRODUCTION_LOCAL_OVERRIDEOVERRIDE_TEST', + 'TEST_ENV_FILE_KEY', + 'LOCAL_TEST_ENV_FILE_KEY', + 'ENV_FILE_TEST_OVERRIDE_TEST', + 'ENV_FILE_TEST_LOCAL_OVERRIDEOVERRIDE_TEST', +] + +export async function getStaticProps() { + const items = {} + + variables.forEach(variable => { + if (process.env[variable]) { + items[variable] = process.env[variable] + } + }) -export async function getStaticProps({ env }) { return { // Do not pass any sensitive values here as they will // be made PUBLICLY available in `pageProps` - props: { env }, + props: { env: items }, revalidate: 1, } } diff --git a/test/integration/env-config/app/pages/some-ssp.js b/test/integration/env-config/app/pages/some-ssp.js index 5629fc9070..e314d452ae 100644 --- a/test/integration/env-config/app/pages/some-ssp.js +++ b/test/integration/env-config/app/pages/some-ssp.js @@ -1,29 +1,35 @@ -export const config = { - env: [ - 'PROCESS_ENV_KEY', - 'ENV_FILE_KEY', - 'LOCAL_ENV_FILE_KEY', - 'ENV_FILE_LOCAL_OVERRIDE_TEST', - 'PRODUCTION_ENV_FILE_KEY', - 'LOCAL_PRODUCTION_ENV_FILE_KEY', - 'DEVELOPMENT_ENV_FILE_KEY', - 'LOCAL_DEVELOPMENT_ENV_FILE_KEY', - 'ENV_FILE_DEVELOPMENT_OVERRIDE_TEST', - 'ENV_FILE_DEVELOPMENT_LOCAL_OVERRIDEOVERRIDE_TEST', - 'ENV_FILE_PRODUCTION_OVERRIDEOVERRIDE_TEST', - 'ENV_FILE_PRODUCTION_LOCAL_OVERRIDEOVERRIDE_TEST', - 'TEST_ENV_FILE_KEY', - 'LOCAL_TEST_ENV_FILE_KEY', - 'ENV_FILE_TEST_OVERRIDE_TEST', - 'ENV_FILE_TEST_LOCAL_OVERRIDEOVERRIDE_TEST', - ], -} +const variables = [ + 'PROCESS_ENV_KEY', + 'ENV_FILE_KEY', + 'LOCAL_ENV_FILE_KEY', + 'ENV_FILE_LOCAL_OVERRIDE_TEST', + 'PRODUCTION_ENV_FILE_KEY', + 'LOCAL_PRODUCTION_ENV_FILE_KEY', + 'DEVELOPMENT_ENV_FILE_KEY', + 'LOCAL_DEVELOPMENT_ENV_FILE_KEY', + 'ENV_FILE_DEVELOPMENT_OVERRIDE_TEST', + 'ENV_FILE_DEVELOPMENT_LOCAL_OVERRIDEOVERRIDE_TEST', + 'ENV_FILE_PRODUCTION_OVERRIDEOVERRIDE_TEST', + 'ENV_FILE_PRODUCTION_LOCAL_OVERRIDEOVERRIDE_TEST', + 'TEST_ENV_FILE_KEY', + 'LOCAL_TEST_ENV_FILE_KEY', + 'ENV_FILE_TEST_OVERRIDE_TEST', + 'ENV_FILE_TEST_LOCAL_OVERRIDEOVERRIDE_TEST', +] + +export async function getServerSideProps() { + const items = {} + + variables.forEach(variable => { + if (process.env[variable]) { + items[variable] = process.env[variable] + } + }) -export async function getServerSideProps({ env }) { return { // Do not pass any sensitive values here as they will // be made PUBLICLY available in `pageProps` - props: { env }, + props: { env: items }, } } diff --git a/test/integration/env-config/app/pages/ssp.js b/test/integration/env-config/app/pages/ssp.js index 5629fc9070..79ac4b006b 100644 --- a/test/integration/env-config/app/pages/ssp.js +++ b/test/integration/env-config/app/pages/ssp.js @@ -1,29 +1,34 @@ -export const config = { - env: [ - 'PROCESS_ENV_KEY', - 'ENV_FILE_KEY', - 'LOCAL_ENV_FILE_KEY', - 'ENV_FILE_LOCAL_OVERRIDE_TEST', - 'PRODUCTION_ENV_FILE_KEY', - 'LOCAL_PRODUCTION_ENV_FILE_KEY', - 'DEVELOPMENT_ENV_FILE_KEY', - 'LOCAL_DEVELOPMENT_ENV_FILE_KEY', - 'ENV_FILE_DEVELOPMENT_OVERRIDE_TEST', - 'ENV_FILE_DEVELOPMENT_LOCAL_OVERRIDEOVERRIDE_TEST', - 'ENV_FILE_PRODUCTION_OVERRIDEOVERRIDE_TEST', - 'ENV_FILE_PRODUCTION_LOCAL_OVERRIDEOVERRIDE_TEST', - 'TEST_ENV_FILE_KEY', - 'LOCAL_TEST_ENV_FILE_KEY', - 'ENV_FILE_TEST_OVERRIDE_TEST', - 'ENV_FILE_TEST_LOCAL_OVERRIDEOVERRIDE_TEST', - ], -} +const variables = [ + 'PROCESS_ENV_KEY', + 'ENV_FILE_KEY', + 'LOCAL_ENV_FILE_KEY', + 'ENV_FILE_LOCAL_OVERRIDE_TEST', + 'PRODUCTION_ENV_FILE_KEY', + 'LOCAL_PRODUCTION_ENV_FILE_KEY', + 'DEVELOPMENT_ENV_FILE_KEY', + 'LOCAL_DEVELOPMENT_ENV_FILE_KEY', + 'ENV_FILE_DEVELOPMENT_OVERRIDE_TEST', + 'ENV_FILE_DEVELOPMENT_LOCAL_OVERRIDEOVERRIDE_TEST', + 'ENV_FILE_PRODUCTION_OVERRIDEOVERRIDE_TEST', + 'ENV_FILE_PRODUCTION_LOCAL_OVERRIDEOVERRIDE_TEST', + 'TEST_ENV_FILE_KEY', + 'LOCAL_TEST_ENV_FILE_KEY', + 'ENV_FILE_TEST_OVERRIDE_TEST', + 'ENV_FILE_TEST_LOCAL_OVERRIDEOVERRIDE_TEST', +] + +export async function getServerSideProps() { + const items = {} -export async function getServerSideProps({ env }) { + variables.forEach(variable => { + if (process.env[variable]) { + items[variable] = process.env[variable] + } + }) return { // Do not pass any sensitive values here as they will // be made PUBLICLY available in `pageProps` - props: { env }, + props: { env: items }, } } diff --git a/test/integration/env-config/test/index.test.js b/test/integration/env-config/test/index.test.js index 6d06021010..08dc7e5f8d 100644 --- a/test/integration/env-config/test/index.test.js +++ b/test/integration/env-config/test/index.test.js @@ -30,7 +30,7 @@ const nextConfigContent = ` { source: '/hello', permanent: false, - destination: \`/\${process.env.NEXT_APP_TEST_DEST}\`, + destination: \`/\${process.env.NEXT_PUBLIC_TEST_DEST}\`, } ] } -- GitLab