cli icon indicating copy to clipboard operation
cli copied to clipboard

Function redirects works for netlify dev but not production

Open bodadotsh opened this issue 5 years ago • 12 comments

- What is the current behavior?

In my Gatsby app, I have these rules for redirects in my netlify.toml

[build]
    publish = "public"
    command = "npm run build"
    functions = "functions"

[build.environment]
  NODE_VERSION = "12"
  YARN_VERSION = "1.22.4"

[[redirects]]
    from = "/api/*"
    to = ".netlify/functions/:splat"
    status = 200

My app works fine when I do netlify dev, all functions redirect to /api like so

Request URL: http://localhost:8888/api/get-items
Request Method: GET
Status Code: 200 OK

but when I deploy to prod, functions fetch result in 404 error

Request URL: https://xxx.netlify.app/api/get-items
Request Method: GET
Status Code: 404 

Although they still work on https://xxx.netlify.app/.netlify/functions/get-items

How can I keep the redirect behaviours consistent on netlify dev and prod?

────────────────────┐ Environment Info │ ────────────────────┘

System: OS: macOS 10.15.4 CPU: (12) x64 Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz Binaries: Node: 12.16.3 - ~/.nvm/versions/node/v12.16.3/bin/node Yarn: 1.22.4 - /usr/local/bin/yarn npm: 6.14.4 - ~/.nvm/versions/node/v12.16.3/bin/npm Browsers: Chrome: 81.0.4044.129 Firefox: 75.0 Safari: 13.1 npmGlobalPackages: netlify-cli: 2.48.0

netlify-cli/2.48.0 darwin-x64 node-v12.16.3

bodadotsh avatar Apr 30 '20 01:04 bodadotsh

I have exactly the same issue.

nlevo avatar Apr 30 '20 22:04 nlevo

It seems like the redirect rules from netlify.toml are not being picked up. I've raised this issue internally but in the meantime you can use a _redirects file to manage your redirects. The rule you have above would look like following in _redirects file.

/api/*  /.netlify/functions/:splat 200

Make sure to copy the _redirects file to your deployment directory (e.g. public) after you've run gatsby build.

RaeesBhatti avatar May 05 '20 05:05 RaeesBhatti

@fool Can you please shed some light on this. I can reproduce this locally, even after I manually copied netlify.toml to publish directory and then make a deployment, the redirect rules don't get applied.

RaeesBhatti avatar May 05 '20 17:05 RaeesBhatti

Any news? I have the same issue...

lublot avatar Dec 13 '20 14:12 lublot

Same issue here

michaelcaterisano avatar Dec 21 '20 19:12 michaelcaterisano

The destination route must be absolute for that proxy to work correctly. The fact that it actually works in netlify dev is actually an issue in the cli for not respecting the http standard for building relative urls.

If you make this change, both, dev and production will work as expected:

diff --git a/netlify.toml b/netlify.toml
index a4b8f3c..70e88ae 100644
--- a/netlify.toml
+++ b/netlify.toml
@@ -17,5 +17,5 @@ to = "https://netlify.com?utm_source=test"
 
 [[redirects]]
 from = "/api/*"
-to = ".netlify/functions/:splat"
+to = "/.netlify/functions/:splat"
 status = 200

You can see that example in this test site:

https://github.com/calavera/web-test2/blob/test/netlify.toml#L18

And a function working with that rule:

https://changelog.netlify.app/.netlify/functions/hello-buildbot https://changelog.netlify.app/api/hello-buildbot

calavera avatar Dec 21 '20 19:12 calavera

@calavera My netlify.toml is like yours, but this still only works for me using netlify dev and not in production.

michaelcaterisano avatar Dec 21 '20 19:12 michaelcaterisano

@michaelcaterisano can you share the rules here, with the url to your production site?

calavera avatar Dec 21 '20 19:12 calavera

Any news? I have the same issue...

Was this issue resolved ?

musebe avatar Feb 19 '21 21:02 musebe

I'm still having this issue as well. Are there any updates?

gjdickens avatar Feb 15 '22 11:02 gjdickens

We have not seen anyone with a properly configured redirect who has the issue. Are you following the advice in https://github.com/netlify/cli/issues/870#issuecomment-749158368 ? Can you share your redirect that is not working and the deploy ID (last part of the URL when you look at deploy logs) that you see it in, so we can review your redirect and hopefully point out a fix?

As far as netlify dev not matching production, I did just file a feature request to improve this here: https://github.com/netlify/cli/issues/4318

nolessafool avatar Feb 17 '22 03:02 nolessafool

@fool thanks for having a look so quickly! Here's my redirect which is same as the advice in comment #870:

[[redirects]]
  from = "/api/*"
  to = "/.netlify/functions/:splat"
  status = 200

Here's the full context of the issue I'm having along with deploy ID's:

  • Redirect is working with no problems in Dev
  • When I build automatically from Github using continuous deployment, the redirect doesn't work in Prod (deploy ID: 620b883236a79b0007b0ca5d)
  • If I build manually and push from the CLI using netlify deploy -p the redirect works fine in Prod using the same configuration (deploy ID: 620b8a1d722a0e1bfe78d428)
  • The redirect also worked in Prod by triggering a new deploy from the GUI (deploy ID: 620b949f3a7b520999f778be)

So it seems to be a continuous deployment issue.

gjdickens avatar Feb 17 '22 08:02 gjdickens

Closing as stale. Please re-open if needed

sarahetter avatar Sep 14 '23 14:09 sarahetter