cli: Netlify dev server hangs if function returns 404

This is a bug report.

Netlify dev server hangs when returning status code 404. This occurs when used with Eleventy dev server, though I am not sure if this is related.

I suspect this is somehow related to proxy behaviour - maybe there is some kind of conflict between servers handling 404.

  1. Set up Elventy + Netlify dev as instructed here https://github.com/philhawksworth/eleventyone

  2. Create a following function bug404.js

exports.handler = async (event, context) => {

    return {
        statusCode: 404,
        body: "this hangs"
    }
};
  1. Start server
npx netlify dev

(starts eleventy serve on the background)

  1. Call function using curl
curl --request POST --data "xxx" "http://localhost:8888/api/bug404"

What is the expected behaviour?

The dev server returns the response immediately.

What is the actual behaviour?

The server never finishes processing the response and the curl waits out for it forever. You need to abort with CTRL+C.

The behaviour is the same under actual browsers. AJAX POST, e.g. initiated by jQuery, never returns when status code is set to 404.

However, if you change the status code e.g. to 403, or 200, it works:

exports.handler = async (event, context) => {
    
    return {
        statusCode: 403,
        body: "this does not hang"
    }
};

curl call returns immediately.

Local Environment Information

  System:
    OS: macOS Mojave 10.14
    CPU: (4) x64 Intel(R) Core(TM) i5-7500 CPU @ 3.40GHz
  Binaries:
    Node: 11.0.0 - ~/.nvm/versions/node/v11.0.0/bin/node
    Yarn: 1.21.1 - ~/code/capitalgram/node_modules/.bin/yarn
    npm: 6.4.1 - ~/.nvm/versions/node/v11.0.0/bin/npm
  Browsers:
    Chrome: 79.0.3945.130
    Firefox: 72.0.2
    Safari: 12.0
  npmGlobalPackages:
    netlify: 3.0.0

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 7
  • Comments: 15 (5 by maintainers)

Most upvoted comments

Running into the same issue. The issue doesn’t exist for requests without data, but it does exist for requests with data.

Here is a minimal project demo with example curl commands and output: https://github.com/richard-flosi/netlify-dev-404

e.g. curl --request GET "http://localhost:8888/.netlify/functions/notfoundbug" is OK curl --request GET --data "{}" "http://localhost:8888/.netlify/functions/notfoundbug" Hangs

Actually, to provide an update, this only seems to be happen with nested functions (ie. functions inside subfolders within my functions directory). I copied the pattern implemented in the netlify workshop REST API example (with the API methods being called from a subfolder using a switch statement in a file in the root of the functions directory). When I return a 404 from the API method in the subfolder, the 404 seems to be returned, but then netlify starts looking for html files of the same name. Maybe this is a redirects issue?

Here’s a sample of a the logs from a request that returns a 404:

Request from ::1: GET /.netlify/functions/songs/kanye-west-jesus-walkss
api/read
  api/read called
  db-connect
    Starting attempt to connect to db.
    Setting Mongo URI to dev DB
  Error occurred running read: Could not find matching song
Response with status 404 in 1960 ms.
Request from ::1: GET /.netlify/functions/songs/kanye-west-jesus-walkss.html
api/read
  api/read called
  db-connect
    Starting attempt to connect to db.
    Using cached database instance
  Error occurred running read: Could not find matching song
Response with status 404 in 77 ms.
Request from ::1: GET /.netlify/functions/songs/kanye-west-jesus-walkss.htm
api/read
  api/read called
  db-connect
    Starting attempt to connect to db.
    Using cached database instance
  Error occurred running read: Could not find matching song
Response with status 404 in 77 ms.
Request from ::1: GET /.netlify/functions/songs/kanye-west-jesus-walkss/index.html
segments length: 2
Response with status 500 in 1 ms.

I did create a test function called 404test (file: 404test.js) in the root of my functions dir, and it does successfully return 404 to postman when called. Super strange.