Cli: [netlify dev] Redirect not working for functions

Created on 12 Oct 2019  Β·  11Comments  Β·  Source: netlify/cli

- Do you want to request a feature or report a bug?
Bug

- What is the current behavior?

I have following section in my netlify.toml

[build]
  publish = "public"
  environment = { AWS_LAMBDA_JS_RUNTIME = "nodejs10.x" }
  functions = "src/functions/"

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

When I start netlify dev and open http://localhost:34567/api/graphql for example it shows me a Function not found.... But when I deploy this into production on Netlify it works. And it still works when I open http://localhost:34567/.netlify/functions/graphql So the redirect (rewrite) is not working locally.

- If the current behavior is a bug, please provide the steps to reproduce.

Just create a netlify.toml as described above and a function called src/functions/graphql.js

- What is the expected behavior?

netlify dev should behave as in production.

- Local Environment Information
β€Ί Error: You don't appear to be in a folder that is linked to a site
Because I deploy my site from GitHub actions. I am not logged in locally.

Most helpful comment

I referenced netlify/cli#master and it's working now. Awesome! πŸ‘ πŸŽ‰

I finally can open http://localhost:8888/api/graphql and it shows me the GraphQL playground. When I open http://localhost:8888/api/foo it shows Function not found... and when I open http://localhost:8888/foo I can see the Gatsby 404 page πŸ’ƒ

All 11 comments

I am experiencing a similar problem but I am using the '_redirects' file method.

The major difference being that I can hit the functions through 8888 with a GET request but all other requests return a 405 BEFORE the function handler is called, this is not a pre-flight issue. Very bizarre, but once I stopped using redirects and hit the .netlify/functions/... endpoints it all works fine.

I lost the better half of a day to this issue. Is there a way to downgrade the CLI in the mean time?

I have had a similar issue. When netlify dev server redirects a function call the last query string parameter has /index.html appended to it. If you use the .netlify/functions endpoint it works fine, as does both variants when deployed to Netlify

/.netlify/functions/test?foo=bar

event: { queryStringParameters: {"foo":"bar"} }

/api/test?foo=bar

event: { queryStringParameters: {"foo":"bar/index.htm"} }

I'm also hitting this. Redirects don't appear to get picked up from netlify.toml or _redirects. Repro here: https://github.com/jlengstorf/jamstack-intro/tree/bug/dev

More testing shows that redirects _do_ work for e.g. SPA setups.

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

[[redirects]]
  from = '/test/*'
  to = '/test'
  status = 200

Running netlify dev from the bug/dev branch of the repro will bring up the Gatsby site at localhost:8888. Visiting localhost:8888/test/something/nested works as expected, but /api/hello hits Gatsby’s development 404 page.

I spun up a single HTML file to see if anything changed and it looks like Gatsby was masking a CLI error.

β—ˆ Rewrote URL to  /.netlify/functions/hello
/Users/jlengstorf/.config/yarn/global/node_modules/netlify-redirector/lib/redirects.js:116
      throw ex;
      ^

ReferenceError: Cannot access 'addonUrl' before initialization
    at ProxyServer.<anonymous> (/Users/jlengstorf/.config/yarn/global/node_modules/netlify-cli/src/commands/dev/index.js:137:28)
    at ProxyServer.emit (/Users/jlengstorf/.config/yarn/global/node_modules/eventemitter3/index.js:183:35)
    at ClientRequest.<anonymous> (/Users/jlengstorf/.config/yarn/global/node_modules/http-proxy/lib/http-proxy/passes/web-incoming.js:171:27)
    at ClientRequest.emit (events.js:203:13)
    at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:575:27)
    at HTTPParser.parserOnHeadersComplete (_http_common.js:116:17)
    at Socket.socketOnData (_http_client.js:449:22)
    at Socket.emit (events.js:203:13)
    at addChunk (_stream_readable.js:295:12)
    at readableAddChunk (_stream_readable.js:276:11)

I added a statictest folder to the repro with the code I used to get this error.

I've just published an update that should fix this issue. Please update to the latest version and try again. And please make sure that your netlify.toml has a build.functions key like the following.

[build]
  functions = "functions/" # path to your functions directory

netlify dev needs a build.functions key to determine where to run the functions server.

@screendriver feel free to reopen if the issue is not fixed.

/ps
@coopsmoss It would be great it you can create a separate issue for your problem and give us a minimal reproducible code test the issue.
@robbishop That seems like a bug. I've just filed it here: https://github.com/netlify/cli/issues/573

@raeesbhatti I updated to 2.18.2 but nothing changed. Same problem as before.

@screendriver Can you please create a repo with a reproducible problem and share it here!

@screendriver Also, I see that in your original description of the issue, you're trying to access http://localhost:34567/api/graphql. Port 34567 is the port for lambda server, not the netlify dev server. netlify dev server is usually present on localhost:8888 (you should see a message similar to this: "β—ˆ Server now ready on http://localhost:8888"). Can you please make sure that you're hitting the correct address (http://localhost:8888/api/graphql).

Ah! I didn't saw that. I thought that I can call / open my functions at port 34567. That's a little bit confusing because

  • 34567 is used by lambda server (what exactly does that mean?)
  • 8888 is used by netlify dev
  • 8000 is used by Gatsby's own server

When I run netlify dev I see all three of them.

At the moment I have the problem that when I open http://localhost:8888/api/graphql my Gatbsy site shows me the 404 page of Gatsby instead of the GraphQL playground. Is it possible to start netlify dev without building and starting the Gatsby stuff? Or make the functions available at a different port or something like that?

@screendriver That was indeed a bug. I've pushed some changes to master branch. I've tested them locally against a Gatsby site and your rules. Can you try npm link master branch of this repo and let me know of the results.

I referenced netlify/cli#master and it's working now. Awesome! πŸ‘ πŸŽ‰

I finally can open http://localhost:8888/api/graphql and it shows me the GraphQL playground. When I open http://localhost:8888/api/foo it shows Function not found... and when I open http://localhost:8888/foo I can see the Gatsby 404 page πŸ’ƒ

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ercgrat picture ercgrat  Β·  3Comments

lukasluecke picture lukasluecke  Β·  3Comments

janbaer picture janbaer  Β·  4Comments

jasikpark picture jasikpark  Β·  5Comments

cco3 picture cco3  Β·  4Comments