Skip to content

Commit

Permalink
Netlify function support (init. version)
Browse files Browse the repository at this point in the history
This commit attempts to set up netlify function support, it has hello.js example.
One big issue is that the nelify functions are essentially only work on :9000, which is a different port (8000) then what gatsby run in develop mode. I need to fix this, not sure how atm.
  • Loading branch information
ZoltanVeres committed Feb 28, 2019
1 parent 01cad74 commit 513c819
Show file tree
Hide file tree
Showing 7 changed files with 253 additions and 12 deletions.
15 changes: 15 additions & 0 deletions gatsby-config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
var proxy = require("http-proxy-middleware")

module.exports = {
siteMetadata: {
title: 'Gatsby + Netlify CMS Starter',
Expand Down Expand Up @@ -74,4 +76,17 @@ module.exports = {
}, // must be after other CSS plugins
'gatsby-plugin-netlify', // make sure to keep it last in the array
],
// for avoiding CORS while developing Netlify Functions locally
// read more: https://www.gatsbyjs.org/docs/api-proxy/#advanced-proxying
developMiddleware: app => {
app.use(
"/.netlify/functions/",
proxy({
target: "http://localhost:8000",
pathRewrite: {
"/.netlify/functions/": "",
},
})
)
},
}
1 change: 1 addition & 0 deletions lambda/hello.js

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

1 change: 1 addition & 0 deletions netlify.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[build]
publish = "public"
command = "npm run build"
functions = "lambda"
[build.environment]
YARN_VERSION = "1.9.4"
YARN_FLAGS = "--no-ignore-optional"
173 changes: 167 additions & 6 deletions package-lock.json

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

11 changes: 9 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,22 @@
"license": "MIT",
"main": "n/a",
"scripts": {
"start": "npm run develop",
"clean": "rimraf .cache public",
"build": "npm run clean && gatsby build",
"start:app": "npm run develop",
"start:lambda": "netlify-lambda serve src/lambda",
"start": "run-p start:**",
"build:app": "npm run clean && gatsby build",
"build:lambda": "netlify-lambda build src/lambda",
"build": "run-p build:**",
"develop": "npm run clean && gatsby develop",
"serve": "gatsby serve",
"format": "prettier --trailing-comma es5 --no-semi --single-quote --write \"{gatsby-*.js,src/**/*.js}\"",
"test": "echo \"Error: no test specified\" && exit 1"
},
"devDependencies": {
"http-proxy-middleware": "^0.19.1",
"netlify-lambda": "^1.4.2",
"npm-run-all": "^4.1.5",
"prettier": "^1.15.3",
"rimraf": "^2.6.2"
}
Expand Down
17 changes: 17 additions & 0 deletions src/lambda/hello.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// 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),
}),
})
}

// Now you are ready to access this API from anywhere in your Gatsby app! For example, in any event handler or lifecycle method, insert:
// fetch("/.netlify/functions/hello")
// .then(response => response.json())
// .then(console.log)
// For more info see: https://www.gatsbyjs.org/blog/2018-12-17-turning-the-static-dynamic/#static-dynamic-is-a-spectrum
Loading

0 comments on commit 513c819

Please sign in to comment.