Skip to content

Commit

Permalink
feat: add support for netlify functions
Browse files Browse the repository at this point in the history
  • Loading branch information
jsjoeio committed May 30, 2020
1 parent acc07ad commit f59d4bc
Show file tree
Hide file tree
Showing 7 changed files with 863 additions and 10 deletions.
1 change: 1 addition & 0 deletions functions/hello.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions gatsby-config.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
const config = require('./config/website')
const pathPrefix = config.pathPrefix === '/' ? '' : config.pathPrefix
const { createProxyMiddleware } = require('http-proxy-middleware')

require('dotenv').config({
path: `.env.${process.env.NODE_ENV}`,
})

module.exports = {
developMiddleware: app => {
app.use(
'/.netlify/functions/',
createProxyMiddleware({
target: 'http://localhost:9000',
pathRewrite: {
'/.netlify/functions/': '',
},
}),
)
},
pathPrefix: config.pathPrefix,
siteMetadata: {
siteUrl: config.siteUrl + pathPrefix,
Expand Down
1 change: 1 addition & 0 deletions netlify.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[build]
functions = "functions"
publish = "public"

[[plugins]]
Expand Down
10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@
"devDependencies": {
"babel-preset-gatsby": "^0.2.20",
"dayjs": "^1.8.18",
"http-proxy-middleware": "^1.0.4",
"hygen": "^5.0.3",
"netlify-lambda": "^1.6.3",
"npm-run-all": "^4.1.5",
"open": "^7.0.3",
"prettier": "1.19.1"
},
Expand All @@ -77,10 +80,13 @@
"license": "MIT",
"main": "n/a",
"scripts": {
"build": "GATSBY_EXPERIMENTAL_PAGE_BUILD_ON_DATA_CHANGES=true gatsby build --log-pages",
"build": "GATSBY_EXPERIMENTAL_PAGE_BUILD_ON_DATA_CHANGES=true gatsby build --log-pages && netlify-lambda build src/functions",
"build:lambda": "netlify-lambda build src/functions",
"build:local": "gatsby build",
"g:post": "hygen post new",
"dev": "gatsby develop",
"start": "serve public/"
"start": "run-p start:**",
"start:app": "npm run dev",
"start:lambda": "netlify-lambda serve src/functions"
}
}
11 changes: 11 additions & 0 deletions src/functions/hello.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// For more info, check https://www.netlify.com/docs/functions/#javascript-lambda-functions
export function handler(event, context, callback) {
console.log('queryStringParameters', event.queryStringParameters)
callback(null, {
// return null to show no errors
statusCode: 200, // http status code
body: JSON.stringify({
msg: 'Hello, World! ' + Math.round(Math.random() * 10),
}),
})
}
4 changes: 4 additions & 0 deletions src/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ const Hero = () => {
const theme = useTheme()
const date = new Date()

fetch('/.netlify/functions/hello')
.then(response => response.json())
.then(console.log)

return (
<section
css={css`
Expand Down
Loading

0 comments on commit f59d4bc

Please sign in to comment.