-
Notifications
You must be signed in to change notification settings - Fork 352
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
Conversation
There was a problem hiding this 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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
cli/src/lib/functions/registry.mjs
Lines 137 to 147 in 05c44a6
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 |
@@ -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)\/([^/]+).*/ |
There was a problem hiding this comment.
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
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)\/.+/) | |
} |
Out-of-scope for this PR, but we should still address it before shipping this:
Lines 166 to 168 in 556d545
Lines 214 to 216 in 556d545
Line 345 in 556d545
|
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:
Part of https://github.com/netlify/pod-dev-foundations/issues/578 and https://github.com/netlify/pod-dev-foundations/issues/585.