Skip to content

Commit

Permalink
Merge branch 'main' into fix/functions-v2-api-path-root
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasholzer authored Oct 10, 2023
2 parents cbbb2c2 + 8183fd0 commit a77d691
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 33 deletions.
69 changes: 41 additions & 28 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,13 @@
"dependencies": {
"@bugsnag/js": "7.20.2",
"@fastify/static": "6.10.2",
"@netlify/build": "29.21.2",
"@netlify/build": "29.22.0",
"@netlify/build-info": "7.9.0",
"@netlify/config": "20.9.0",
"@netlify/edge-bundler": "9.1.0",
"@netlify/local-functions-proxy": "1.1.1",
"@netlify/serverless-functions-api": "1.8.0",
"@netlify/zip-it-and-ship-it": "9.21.0",
"@netlify/zip-it-and-ship-it": "9.22.0",
"@octokit/rest": "19.0.13",
"ansi-escapes": "6.2.0",
"ansi-styles": "6.2.1",
Expand Down
7 changes: 6 additions & 1 deletion src/lib/functions/form-submissions-handler.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@ const getFormHandler = function ({ functionsRegistry }) {

export const createFormSubmissionHandler = function ({ functionsRegistry, siteUrl }) {
return async function formSubmissionHandler(req, res, next) {
if (req.url.startsWith('/.netlify/') || req.method !== 'POST') return next()
if (
req.url.startsWith('/.netlify/') ||
req.method !== 'POST' ||
(await functionsRegistry.getFunctionForURLPath(req.url, req.method))
)
return next()

const fakeRequest = new Readable({
read() {
Expand Down
6 changes: 5 additions & 1 deletion src/lib/functions/registry.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,11 @@ export class FunctionsRegistry {
return this.functions.get(name)
}

async getFunctionForURLPath(urlPath, method) {
async getFunctionForURLPath(url, method) {
// We're constructing a URL object just so that we can extract the path from
// the incoming URL. It doesn't really matter that we don't have the actual
// local URL with the correct port.
const urlPath = new URL(url, 'http://localhost').pathname
const defaultURLMatch = urlPath.match(DEFAULT_URL_EXPRESSION)

if (defaultURLMatch) {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/proxy.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ const onRequest = async (
return proxy.web(req, res, { target: edgeFunctionsProxyURL })
}

const functionMatch = await functionsRegistry.getFunctionForURLPath(req.url, req.method)
const functionMatch = functionsRegistry && (await functionsRegistry.getFunctionForURLPath(req.url, req.method))

if (functionMatch) {
// Setting an internal header with the function name so that we don't
Expand Down
6 changes: 6 additions & 0 deletions tests/integration/commands/dev/v2-api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ describe.runIf(gte(version, '18.13.0'))('v2 api', () => {
expect(await response.text()).toBe(`With literal path: ${url}`)
})

test<FixtureTestContext>('doesnt run form logic on paths matching function', async ({ devServer }) => {
const url = `http://localhost:${devServer.port}/products`
await fetch(url, { method: 'POST' })
expect(devServer.output).not.toContain("Missing form submission function handler")
})

test<FixtureTestContext>('supports custom URLs with method matching', async ({ devServer }) => {
const url = `http://localhost:${devServer.port}/products/really-bad-product`
const response = await fetch(url, { method: 'DELETE' })
Expand Down

0 comments on commit a77d691

Please sign in to comment.