Skip to content

Commit 24fd88a

Browse files
feat: add support for Next env vars (#842)
* feat: add support for Next env vars Release-As: 4.0.0-beta.12 * chore: update snapshots Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
1 parent 2c99f15 commit 24fd88a

File tree

8 files changed

+47
-3
lines changed

8 files changed

+47
-3
lines changed

demos/default/.env.production

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
HELLO_WORLD="Hello Production"

demos/default/pages/api/hello.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
22

33
export default (req, res) => {
4-
res.status(200).json({ name: 'John Doe', query: req.query })
4+
res.status(200).json({ name: 'John Doe', query: req.query, env: process.env.HELLO_WORLD })
55
}

demos/default/pages/getServerSideProps/[id].js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Error from 'next/error'
22
import Link from 'next/link'
33

4-
const Show = ({ errorCode, show }) => {
4+
const Show = ({ errorCode, show, env }) => {
55
// If show item was not found, render 404 page
66
if (errorCode) {
77
return <Error statusCode={errorCode} />
@@ -15,7 +15,7 @@ const Show = ({ errorCode, show }) => {
1515
<br />
1616
Refresh the page to see server-side rendering in action.
1717
<br />
18-
You can also try changing the ID to any other number between 1-10000.
18+
You can also try changing the ID to any other number between 1-10000. Env: {env}
1919
</p>
2020

2121
<hr />
@@ -46,6 +46,7 @@ export const getServerSideProps = async ({ params }) => {
4646
props: {
4747
errorCode,
4848
show: data,
49+
env: process.env.HELLO_WORLD || null,
4950
},
5051
}
5152
}
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import Link from 'next/link'
2+
3+
const Env = ({ env }) => (
4+
<div>
5+
<p>This page uses getStaticProps() to populate env vars.</p>
6+
7+
<hr />
8+
<p>env: {env}</p>
9+
10+
<Link href="/">
11+
<a>Go back home</a>
12+
</Link>
13+
</div>
14+
)
15+
16+
export function getStaticProps(context) {
17+
return {
18+
props: {
19+
env: process.env.HELLO_WORLD || null,
20+
},
21+
}
22+
}
23+
24+
export default Env

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
"publish:test": "npm test",
2727
"test": "run-s build build:demo test:jest",
2828
"test:jest": "jest",
29+
"test:jest:update": "jest --updateSnapshot",
30+
"test:update": "run-s build build:demo test:jest:update",
2931
"prepare": "npm run build",
3032
"clean": "rimraf lib",
3133
"build": "tsc",

src/helpers/config.js

+4
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,10 @@ exports.configureHandlerFunctions = ({ netlifyConfig, publish, ignore = [] }) =>
190190
netlifyConfig.functions[functionName].node_bundler = 'nft'
191191
netlifyConfig.functions[functionName].included_files ||= []
192192
netlifyConfig.functions[functionName].included_files.push(
193+
'.env',
194+
'.env.local',
195+
'.env.production',
196+
'.env.production.local',
193197
`${publish}/server/**`,
194198
`${publish}/serverless/**`,
195199
`${publish}/*.json`,

test/__snapshots__/index.js.snap

+8
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ exports.resolvePages = () => {
1818
require.resolve('../../../.next/server/pages/getServerSideProps/all/[[...slug]].js')
1919
require.resolve('../../../.next/server/pages/getServerSideProps/static.js')
2020
require.resolve('../../../.next/server/pages/getStaticProps/[id].js')
21+
require.resolve('../../../.next/server/pages/getStaticProps/env.js')
2122
require.resolve('../../../.next/server/pages/getStaticProps/static.js')
2223
require.resolve('../../../.next/server/pages/getStaticProps/with-revalidate.js')
2324
require.resolve('../../../.next/server/pages/getStaticProps/withFallback/[...slug].js')
@@ -52,6 +53,7 @@ exports.resolvePages = () => {
5253
require.resolve('../../../.next/server/pages/getServerSideProps/all/[[...slug]].js')
5354
require.resolve('../../../.next/server/pages/getServerSideProps/static.js')
5455
require.resolve('../../../.next/server/pages/getStaticProps/[id].js')
56+
require.resolve('../../../.next/server/pages/getStaticProps/env.js')
5557
require.resolve('../../../.next/server/pages/getStaticProps/static.js')
5658
require.resolve('../../../.next/server/pages/getStaticProps/with-revalidate.js')
5759
require.resolve('../../../.next/server/pages/getStaticProps/withFallback/[...slug].js')
@@ -74,6 +76,8 @@ Array [
7476
"en/getStaticProps/1.json",
7577
"en/getStaticProps/2.html",
7678
"en/getStaticProps/2.json",
79+
"en/getStaticProps/env.html",
80+
"en/getStaticProps/env.json",
7781
"en/getStaticProps/static.html",
7882
"en/getStaticProps/static.json",
7983
"en/getStaticProps/withFallback/3.html",
@@ -92,12 +96,16 @@ Array [
9296
"en/previewTest.html",
9397
"en/previewTest.json",
9498
"en/static.html",
99+
"es/getStaticProps/env.html",
100+
"es/getStaticProps/env.json",
95101
"es/getStaticProps/static.html",
96102
"es/getStaticProps/static.json",
97103
"es/image.html",
98104
"es/previewTest.html",
99105
"es/previewTest.json",
100106
"es/static.html",
107+
"fr/getStaticProps/env.html",
108+
"fr/getStaticProps/env.json",
101109
"fr/getStaticProps/static.html",
102110
"fr/getStaticProps/static.json",
103111
"fr/image.html",

test/index.js

+4
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,10 @@ describe('onBuild()', () => {
296296

297297
await plugin.onBuild(defaultArgs)
298298
const includes = [
299+
'.env',
300+
'.env.local',
301+
'.env.production',
302+
'.env.production.local',
299303
'.next/server/**',
300304
'.next/serverless/**',
301305
'.next/*.json',

0 commit comments

Comments
 (0)