I’ve got a project where I had some redirect rules set up to handle some “functions” endpoints. It worked well for a while but when I updated the CLI version recently it seems to have stopped.
I can hit a function path directly, as in http://localhost:8888/.netlify/functions/dboxDecks/dboxDecks.js
and it will work fine.
However when I try to go to its redirect ‘from’ path http://localhost:8888/decks
it simply serves the root, index file of the project .
I should point out that when I used to run netlify dev
a secondary terminal window launched and displayed some port information. Now this does not happen.
Here’s my netlify.toml file:
[build]
publish = "build"
functions = "functions"
[[redirects]]
from = "/generate"
to = "/.netlify/functions/generateSignature/generateSignature.js"
status = 200
[[redirects]]
from = "/decks"
to = "/.netlify/functions/dboxDecks/dboxDecks.js"
status = 200
[[redirects]]
from = "/slides"
to = "/.netlify/functions/dboxSlides/dboxSlides.js"
status = 200
[[redirects]]
from = "/thumbnail"
to = "/.netlify/functions/dboxDeckThumbnail/dboxDeckThumbnail.js"
status = 200
Anyone know what’s going on?
Scott
May 7, 2020, 3:53pm
3
Hey there,
Can you give this topic a read, please, and let me know if this helps?
I want to use redirects in gatsby. I installed netlify-cli and added netlify.toml file in the root folder.
In netlify-toml file
[[redirects]]
from = “/search/*”
to = “/search”
status = 200
When I run netlify dev, I’ve got this following errors.
Warnings while parsing netlify.toml file:
TypeError: Cannot convert object to primitive value
at ~/npm-global/lib/node_modules/netlify-cli/src/utils/rules-proxy.js:15:46
at Array.forEach ()
at parseFile (~/npm-global/lib/node_modules/netlify-cl…
So I uninstalled netlify cli and reinstalled with npm i -g netlify-cli@latest
and the issue still persists.
@willkp Can you please post the results of netlify version --verbose
!
I have the same problem.
When I run $ netlify dev
or $ netlify dev --live
the redirects rules does not works.
If I publish the project all run correctly.
My Netlify config file is like that:
[build]
command = "npm run build"
publish = "dist"
functions = "functions"
[dev]
framework = "#custom"
command = "npm run dev"
targetPort = 3030
My _redirects
file is that:
/api /.netlify/functions/api 200
This is not only a issue with functions redirects. It’s not working with any redirect rule.
Here the link to the demo project in production:
https://nervous-allen-34edcb.netlify.app/
Here the link to the code:
https://github.com/juanmiguelguerrero/auth0-test
And a capture with my netlify version --verbose!
fool
May 11, 2020, 10:53pm
8
As far as I know, we cannot host functions with the string “api” in their name - obscure bug. Mind trying it with a differently named function just to be sure?
Hi fool,
Using the string “api” in the name of my functions works well on Dev and Production. I can’t find any obscure bug.
However, I have changed the name of the function and the redirection and the problem persists.
Where there is a bug is in the latest version of Netlify CLI.
I found this and It’s not only a Gatsby problem:
opened 06:15PM - 09 May 20 UTC
closed 02:30PM - 12 Aug 20 UTC
type: bug
area: functions
*Note: I'm still working on a minimal reproduction, but you can check on the [li… ve site](https://master--dailytexascountry.netlify.app/store).*
---
### Do you want to request a _feature_ or report a _bug_?
🐛 bug
### What is the current behavior?
First, see this [Twitter discussion](https://twitter.com/bradgarropy/status/1258993787990409217) with @jlengstorf.
I defined the following redirect in `netlify.toml`.
```
[[redirects]]
from = "/api/*"
to = "/.netlify/functions/:splat"
status = 200
```
Then in my application I have a serverless function located at `/.netlify/functions/subscribe`.
I hit it via a `POST` request using the browser's `fetch` API like this.
```
const options = {
method: "POST",
body: JSON.stringify(data),
headers: {"Content-Type": "application/json"},
}
const response = await fetch("/api/subscribe", options)
```
It returns an HTML document rather than the expected JSON output.
```
<!DOCTYPE html><html><head><meta charSet="utf-8"/><meta http-equiv="x-ua-compatible" content="ie=edge"/><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"/><meta name="note" content="environment=development"/><title data-react-helmet="true"></title><link href="https://fonts.googleapis.com/css?family=Inter|Roboto|Patua+One" rel="stylesheet"/><style type="text/css">
.anchor.before {
position: absolute;
top: 0;
left: 0;
transform: translateX(-100%);
padding-right: 4px;
}
.anchor.after {
display: inline-block;
padding-left: 4px;
}
h1 .anchor svg,
h2 .anchor svg,
h3 .anchor svg,
h4 .anchor svg,
h5 .anchor svg,
h6 .anchor svg {
visibility: hidden;
}
h1:hover .anchor svg,
h2:hover .anchor svg,
h3:hover .anchor svg,
h4:hover .anchor svg,
h5:hover .anchor svg,
h6:hover .anchor svg,
h1 .anchor:focus svg,
h2 .anchor:focus svg,
h3 .anchor:focus svg,
h4 .anchor:focus svg,
h5 .anchor:focus svg,
h6 .anchor:focus svg {
visibility: visible;
}
</style><script>
document.addEventListener("DOMContentLoaded", function(event) {
var hash = window.decodeURI(location.hash.replace('#', ''))
if (hash !== '') {
var element = document.getElementById(hash)
if (element) {
var scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop
var clientTop = document.documentElement.clientTop || document.body.clientTop || 0
var offset = element.getBoundingClientRect().top + scrollTop - clientTop
// Wait for the browser to finish rendering before scrolling.
setTimeout((function() {
window.scrollTo(0, offset - 0)
}), 0)
}
}
})
</script><link rel="preconnect" href="https://instagram.com"/><script src="/socket.io/socket.io.js"></script></head><body><div id="___gatsby"></div><script>
window.gatsbyLoadInstagram = function() {
var js = document.createElement('script');
var firstScript = document.getElementsByTagName('script')[0];
js.id = 'gatsby-plugin-instagram';
js.src = 'https://instagram.com/embed.js';
firstScript.parentNode.insertBefore(js, firstScript);
return true;
}
</script><script src="/commons.js"></script>
</body></html>
```
So it seems like the `netlify dev` command does not respect function redirects.
### If the current behavior is a bug, please provide the steps to reproduce.
First get the project up and running.
```
git clone https://github.com/bradgarropy/dailytexascountry.com.git
cd dailytexascountry.com
npm install
netlify dev
```
Then go to the `/store` page and enter your email, which will kick off the API call to `/api/subscribe`.
Check the Network tab to see the unexpected HTML response.
### What is the expected behavior?
The `/api/subscribe` endpoint should return a JSON object.
### Local Environment Information
```
────────────────────┐
Environment Info │
────────────────────┘
System:
OS: Windows 10 10.0.18363
CPU: (8) x64 Intel(R) Core(TM) i7-9700K CPU @ 3.60GHz
Binaries:
Node: 12.13.1 - D:\nodejs\node.EXE
Yarn: 1.22.4 - ~\AppData\Roaming\npm\yarn.CMD
npm: 6.14.5 - D:\nodejs\npm.CMD
Browsers:
Edge: 44.18362.449.0
netlify-cli/2.50.0 win32-x64 node-v12.13.1
```
Thanks
Scott
May 13, 2020, 1:57pm
10
Hey there,
Thanks for closing the loop and linking the bug.
As per function redirects are not working with netlify dev · Issue #890 · netlify/cli · GitHub , netlify-cli@2.51.0
has just been released which contains a fix for this issue.
existo
May 16, 2020, 10:17am
11
Everything is fine width the update.
Thanks.