Skip to content

feat: add support for Next env vars #842

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

Merged
merged 3 commits into from
Nov 29, 2021
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
1 change: 1 addition & 0 deletions demos/default/.env.production
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
HELLO_WORLD="Hello Production"
2 changes: 1 addition & 1 deletion demos/default/pages/api/hello.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction

export default (req, res) => {
res.status(200).json({ name: 'John Doe', query: req.query })
res.status(200).json({ name: 'John Doe', query: req.query, env: process.env.HELLO_WORLD })
}
5 changes: 3 additions & 2 deletions demos/default/pages/getServerSideProps/[id].js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Error from 'next/error'
import Link from 'next/link'

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

<hr />
Expand Down Expand Up @@ -46,6 +46,7 @@ export const getServerSideProps = async ({ params }) => {
props: {
errorCode,
show: data,
env: process.env.HELLO_WORLD || null,
},
}
}
Expand Down
24 changes: 24 additions & 0 deletions demos/default/pages/getStaticProps/env.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import Link from 'next/link'

const Env = ({ env }) => (
<div>
<p>This page uses getStaticProps() to populate env vars.</p>

<hr />
<p>env: {env}</p>

<Link href="/">
<a>Go back home</a>
</Link>
</div>
)

export function getStaticProps(context) {
return {
props: {
env: process.env.HELLO_WORLD || null,
},
}
}

export default Env
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
"publish:test": "npm test",
"test": "run-s build build:demo test:jest",
"test:jest": "jest",
"test:jest:update": "jest --updateSnapshot",
"test:update": "run-s build build:demo test:jest:update",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[sand] we might want to use actual objects, this makes it too easy to run this and ignore snapshots and captures implementation details. It might be a huge object but at least the update will be more deliberate

"prepare": "npm run build",
"clean": "rimraf lib",
"build": "tsc",
Expand Down
4 changes: 4 additions & 0 deletions src/helpers/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,10 @@ exports.configureHandlerFunctions = ({ netlifyConfig, publish, ignore = [] }) =>
netlifyConfig.functions[functionName].node_bundler = 'nft'
netlifyConfig.functions[functionName].included_files ||= []
netlifyConfig.functions[functionName].included_files.push(
'.env',
'.env.local',
'.env.production',
'.env.production.local',
`${publish}/server/**`,
`${publish}/serverless/**`,
`${publish}/*.json`,
Expand Down
8 changes: 8 additions & 0 deletions test/__snapshots__/index.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ exports.resolvePages = () => {
require.resolve('../../../.next/server/pages/getServerSideProps/all/[[...slug]].js')
require.resolve('../../../.next/server/pages/getServerSideProps/static.js')
require.resolve('../../../.next/server/pages/getStaticProps/[id].js')
require.resolve('../../../.next/server/pages/getStaticProps/env.js')
require.resolve('../../../.next/server/pages/getStaticProps/static.js')
require.resolve('../../../.next/server/pages/getStaticProps/with-revalidate.js')
require.resolve('../../../.next/server/pages/getStaticProps/withFallback/[...slug].js')
Expand Down Expand Up @@ -52,6 +53,7 @@ exports.resolvePages = () => {
require.resolve('../../../.next/server/pages/getServerSideProps/all/[[...slug]].js')
require.resolve('../../../.next/server/pages/getServerSideProps/static.js')
require.resolve('../../../.next/server/pages/getStaticProps/[id].js')
require.resolve('../../../.next/server/pages/getStaticProps/env.js')
require.resolve('../../../.next/server/pages/getStaticProps/static.js')
require.resolve('../../../.next/server/pages/getStaticProps/with-revalidate.js')
require.resolve('../../../.next/server/pages/getStaticProps/withFallback/[...slug].js')
Expand All @@ -74,6 +76,8 @@ Array [
"en/getStaticProps/1.json",
"en/getStaticProps/2.html",
"en/getStaticProps/2.json",
"en/getStaticProps/env.html",
"en/getStaticProps/env.json",
"en/getStaticProps/static.html",
"en/getStaticProps/static.json",
"en/getStaticProps/withFallback/3.html",
Expand All @@ -92,12 +96,16 @@ Array [
"en/previewTest.html",
"en/previewTest.json",
"en/static.html",
"es/getStaticProps/env.html",
"es/getStaticProps/env.json",
"es/getStaticProps/static.html",
"es/getStaticProps/static.json",
"es/image.html",
"es/previewTest.html",
"es/previewTest.json",
"es/static.html",
"fr/getStaticProps/env.html",
"fr/getStaticProps/env.json",
"fr/getStaticProps/static.html",
"fr/getStaticProps/static.json",
"fr/image.html",
Expand Down
4 changes: 4 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,10 @@ describe('onBuild()', () => {

await plugin.onBuild(defaultArgs)
const includes = [
'.env',
'.env.local',
'.env.production',
'.env.production.local',
'.next/server/**',
'.next/serverless/**',
'.next/*.json',
Expand Down