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

feat: return 404 on default URL when function has custom routes #5998

Merged
merged 4 commits into from
Oct 9, 2023

Conversation

eduardoboucas
Copy link
Member

Summary

Follow-up to #5954. When a function defines custom routes, we should return 404 when trying to access the function on the /.netlify/functions default URL.

When this happens, we show a message in the console to help people understand why they're seeing a 404:

›   Warning: Function v2-js cannot be invoked on /.netlify/functions/v2-js, because the function has the following URL paths defined: /v2/:foo

Part of https://github.com/netlify/pod-dev-foundations/issues/578 and https://github.com/netlify/pod-dev-foundations/issues/585.

@eduardoboucas eduardoboucas requested a review from a team as a code owner September 15, 2023 17:32
@github-actions
Copy link

github-actions bot commented Sep 15, 2023

📊 Benchmark results

Comparing with ba672f8

  • Dependency count: 1,385 (no change)
  • Package size: 388 MB ⬆️ 0.00% increase vs. ba672f8

Copy link
Contributor

@Skn0tt Skn0tt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The functionality looks good. Noted two code-level things that I'd love to be refactored before merging this - fine to land in a refactoring as well, though.

@@ -123,6 +124,32 @@ export class FunctionsRegistry {
}

async getFunctionForURLPath(urlPath, method) {
const defaultURLMatch = urlPath.match(DEFAULT_URL_EXPRESSION)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this block should live in NetlifyFunction#matchUrlPath. That'd make the logic a little easier to reason about, as well.

It'd probably result in more regex matchings needed to be done, but I don't think that's a performance concern at the orders of magnitude that functions have in local dev.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay, scratch that - would make

if (routes.length !== 0) {
const paths = routes.map((route) => chalk.underline(route.pattern)).join(', ')
warn(
`Function ${chalk.yellow(func.name)} cannot be invoked on ${chalk.underline(
urlPath,
)}, because the function has the following URL paths defined: ${paths}`,
)
return
a lot harder to implement

@@ -14,6 +14,7 @@ import { getPathInProject } from '../settings.mjs'
import NetlifyFunction from './netlify-function.mjs'
import runtimes from './runtimes/index.mjs'

const DEFAULT_URL_EXPRESSION = /^\/.netlify\/(functions|builders)\/([^/]+).*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should be refactoring this so that the regex is shared with isFunction

cli/src/utils/proxy.mjs

Lines 85 to 91 in 556d545

/**
* @param {boolean|number|undefined} functionsPort
* @param {string} url
*/
function isFunction(functionsPort, url) {
return functionsPort && url.match(/^\/.netlify\/(functions|builders)\/.+/)
}

@Skn0tt
Copy link
Contributor

Skn0tt commented Sep 22, 2023

Out-of-scope for this PR, but we should still address it before shipping this:

isFunction is used in a bunch of places to inform redirecting rules etc. We should check if that needs to take custom routes into account:

cli/src/utils/proxy.mjs

Lines 166 to 168 in 556d545

if (isFunction(true, url)) {
return []
}

cli/src/utils/proxy.mjs

Lines 214 to 216 in 556d545

if (isFunction(options.functionsPort, req.url)) {
return proxy.web(req, res, { target: options.functionsServer })
}

if (isFunction(options.functionsPort, req.url) || functionWithCustomRoute) {

@Skn0tt Skn0tt enabled auto-merge (squash) October 9, 2023 10:56
@Skn0tt Skn0tt merged commit 05c44a6 into main Oct 9, 2023
63 checks passed
@Skn0tt Skn0tt deleted the feat/custom-route-exclusive branch October 9, 2023 11:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants