Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update environment support #11524

Merged
merged 5 commits into from
Mar 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/next/build/webpack-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,6 @@ const nextServerlessLoader: loader.Loader = function() {
Object.assign({}, parsedUrl.query, params ),
resolver,
${encodedPreviewProps},
process.env,
onError
)
} catch (err) {
Expand Down
3 changes: 2 additions & 1 deletion packages/next/export/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions packages/next/lib/load-env-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
5 changes: 0 additions & 5 deletions packages/next/next-server/server/api-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[] }
Expand All @@ -26,7 +24,6 @@ export async function apiResolver(
params: any,
resolverModule: any,
apiContext: __ApiPreviewProps,
env: Env | false,
onError?: ({ err }: { err: any }) => Promise<void>
) {
const apiReq = req as NextApiRequest
Expand All @@ -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
Expand Down
7 changes: 2 additions & 5 deletions packages/next/next-server/server/next-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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 }
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -693,7 +691,6 @@ export default class Server {
query,
pageModule,
this.renderOpts.previewProps,
this.renderOpts.env,
this.onErrorMiddleware
)
return true
Expand Down
8 changes: 0 additions & 8 deletions packages/next/next-server/server/render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -156,7 +154,6 @@ export type RenderOptsPartial = {
isDataReq?: boolean
params?: ParsedUrlQuery
previewProps: __ApiPreviewProps
env: Env | false
}

export type RenderOpts = LoadComponentsReturnType & RenderOptsPartial
Expand Down Expand Up @@ -291,7 +288,6 @@ export async function renderToHTML(
staticMarkup = false,
ampPath = '',
App,
env = {},
Document,
pageConfig = {},
DocumentMiddleware,
Expand All @@ -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 ? {} : []

Expand Down Expand Up @@ -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 }
Expand Down Expand Up @@ -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 }
Expand Down
26 changes: 0 additions & 26 deletions packages/next/next-server/server/utils.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
}
4 changes: 0 additions & 4 deletions packages/next/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -73,7 +71,6 @@ export type GetStaticProps<
params?: ParsedUrlQuery
preview?: boolean
previewData?: any
env: Env
}) => Promise<{
props: P
revalidate?: number | boolean
Expand All @@ -91,7 +88,6 @@ export type GetServerSideProps<
res: ServerResponse
params?: ParsedUrlQuery
query: ParsedUrlQuery
env: Env
preview?: boolean
previewData?: any
}) => Promise<{ props: P }>
Expand Down
3 changes: 0 additions & 3 deletions test/integration/env-config-errors/app/package.json

This file was deleted.

5 changes: 0 additions & 5 deletions test/integration/env-config-errors/app/pages/api/hello.js

This file was deleted.

14 changes: 0 additions & 14 deletions test/integration/env-config-errors/app/pages/index.js

This file was deleted.

13 changes: 0 additions & 13 deletions test/integration/env-config-errors/app/pages/ssp.js

This file was deleted.

Loading