From f59d4bc0937b10df93e233e748f9964d4dbafc96 Mon Sep 17 00:00:00 2001 From: Joe Previte Date: Sat, 30 May 2020 15:07:14 -0700 Subject: [PATCH] feat: add support for netlify functions --- functions/hello.js | 1 + gatsby-config.js | 12 + netlify.toml | 1 + package.json | 10 +- src/functions/hello.js | 11 + src/pages/index.js | 4 + yarn.lock | 834 ++++++++++++++++++++++++++++++++++++++++- 7 files changed, 863 insertions(+), 10 deletions(-) create mode 100644 functions/hello.js create mode 100644 src/functions/hello.js diff --git a/functions/hello.js b/functions/hello.js new file mode 100644 index 00000000..4fb25ed1 --- /dev/null +++ b/functions/hello.js @@ -0,0 +1 @@ +!function(e,r){for(var t in r)e[t]=r[t]}(exports,function(e){var r={};function t(n){if(r[n])return r[n].exports;var o=r[n]={i:n,l:!1,exports:{}};return e[n].call(o.exports,o,o.exports,t),o.l=!0,o.exports}return t.m=e,t.c=r,t.d=function(e,r,n){t.o(e,r)||Object.defineProperty(e,r,{enumerable:!0,get:n})},t.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},t.t=function(e,r){if(1&r&&(e=t(e)),8&r)return e;if(4&r&&"object"==typeof e&&e&&e.__esModule)return e;var n=Object.create(null);if(t.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:e}),2&r&&"string"!=typeof e)for(var o in e)t.d(n,o,function(r){return e[r]}.bind(null,o));return n},t.n=function(e){var r=e&&e.__esModule?function(){return e.default}:function(){return e};return t.d(r,"a",r),r},t.o=function(e,r){return Object.prototype.hasOwnProperty.call(e,r)},t.p="",t(t.s=0)}([function(e,r,t){"use strict";function n(e,r,t){console.log("queryStringParameters",e.queryStringParameters),t(null,{statusCode:200,body:JSON.stringify({msg:"Hello, World! "+Math.round(10*Math.random())})})}t.r(r),t.d(r,"handler",(function(){return n}))}])); \ No newline at end of file diff --git a/gatsby-config.js b/gatsby-config.js index 50923bce..abfcc5c3 100644 --- a/gatsby-config.js +++ b/gatsby-config.js @@ -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, diff --git a/netlify.toml b/netlify.toml index 2215286f..83fe98b4 100644 --- a/netlify.toml +++ b/netlify.toml @@ -1,4 +1,5 @@ [build] + functions = "functions" publish = "public" [[plugins]] diff --git a/package.json b/package.json index e8da0077..ed3646ee 100644 --- a/package.json +++ b/package.json @@ -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" }, @@ -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" } } diff --git a/src/functions/hello.js b/src/functions/hello.js new file mode 100644 index 00000000..7fcef2f8 --- /dev/null +++ b/src/functions/hello.js @@ -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), + }), + }) +} diff --git a/src/pages/index.js b/src/pages/index.js index 8a985e98..4f50a264 100644 --- a/src/pages/index.js +++ b/src/pages/index.js @@ -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 (