Skip to content

Commit aca75c6

Browse files
committed
feat: add support for Next env vars
1 parent 14ca14a commit aca75c6

File tree

5 files changed

+33
-3
lines changed

5 files changed

+33
-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

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`,

0 commit comments

Comments
 (0)