Skip to content
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

fix: prevent reach-router dep requirement for v2 #557

Merged
merged 7 commits into from
Feb 23, 2023
23 changes: 8 additions & 15 deletions plugin/src/templates/handlers.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import type {
Handler,
HandlerContext,
HandlerEvent,
HandlerResponse,
} from '@netlify/functions'
import type { Handler, HandlerEvent } from '@netlify/functions'
import { stripIndent as javascript } from 'common-tags'
import type { GatsbyFunctionRequest } from 'gatsby'
import type {
Expand All @@ -19,8 +14,6 @@ const { join } = require('path')

const etag = require('etag')

const { gatsbyFunction } = require('./api/gatsbyFunction')
const { createRequestObject, createResponseObject } = require('./api/utils')
const {
getPagePathFromPageDataPath,
getGraphQLEngine,
Expand Down Expand Up @@ -142,11 +135,11 @@ const getHandler = (renderMode: RenderMode, appDir: string): Handler => {
* Generate an API handler
*/

const getApiHandler = (appDir: string): Handler =>
const apiHandler = javascript`(appDir) =>
function handler(
event: HandlerEvent,
context: HandlerContext,
): Promise<HandlerResponse> {
event,
context,
) {
// Create a fake Gatsby/Express Request object
const req = createRequestObject({ event, context })

Expand All @@ -158,11 +151,11 @@ const getApiHandler = (appDir: string): Handler =>
// Try to call the actual function
gatsbyFunction(req, res, event, appDir)
} catch (error) {
console.error(`Error executing ${event.path}`, error)
console.error(\`Error executing \${event.path}\`, error)
resolve({ statusCode: 500 })
}
})
}
}`

export const makeHandler = (appDir: string, renderMode: RenderMode): string =>
// This is a string, but if you have the right editor plugin it should format as js
Expand All @@ -188,5 +181,5 @@ export const makeApiHandler = (appDir: string): string =>
const { resolve } = require("path");

const pageRoot = resolve(__dirname, "${appDir}");
exports.handler = ((${getApiHandler.toString()})(pageRoot))
exports.handler = ((${apiHandler})(pageRoot))
`