Skip to content

Hosting rewrites are not working on emulators when using Next.js with webframeworks #5840

Closed

Description

@nikita-mishchenko

[REQUIRED] Environment info

firebase-tools: 12.0.0

Platform: macOS

[REQUIRED] Test case

Example firebase.json

{
    "hosting": {
        ...
        "rewrites": [
            {
                "source": "/api/helloworld",
                "function": {
                    "functionId": "helloworld",
                    "region": "europe-west1"
                }
            },
        ...
        ]
    }
    ...
}

Should add rewrite to the hosting emulator.

[REQUIRED] Steps to reproduce

  1. Add function rewrite rule to the hosting block in firebase.json
  2. Add some simple firebase function helloworld
  3. Have some Next.js application
  4. Enable webframeworks experiments
  5. Run firebase emulators:start
  6. Call http://<emulators_host>:<hosting_port>/api/helloworld

[REQUIRED] Expected behavior

Receive a response from helloworld firebase function

[REQUIRED] Actual behavior

Receive a 404 error page from Next.js app

Why is this happening?

This bug is related to pull request #5582. It was released in version 11.25.3.

Firebase hosting emulator is basically the superstatic server. firebase-tools using it to create a dev server with the superstatic strict stack which is ["redirects", "headers", "files", "rewrites", "missing"]. Also, firebase-tools adding functions before and after files part. So the resulting request processing pipeline is the following:

  1. Check redirects
  2. Check headers
  3. Check firebase service urls
  4. Check files
  5. Check Next.js app response
  6. Check rewrites
  7. Check missing

In the pull request #5582 createServerResponseProxy function was replaced with the simpleProxy function. But createServerResponseProxy had the following piece of code, that is currently missing:

if (proxiedRes.statusCode === 404) {
    next();
}

It means that before, if the route is not found in the Next.js app the request pipeline would continue to evaluate rewrites and missing. And now, it just returns the response from Node.js app and ignores rewrites and missing.
As the result the whole rewrites section under hosting in firebase.json is ignored.

Activity

  1. jamesdaniels commented on May 16, 2023

    @jamesdaniels
    Contributor

    Great catch, we'll get that functionality back into simpleProxy

  2. added a commit that references this issue on Jun 1, 2023
    0261dc8
  3. nikita-mishchenko commented on Jun 3, 2023

    @nikita-mishchenko
    Author

    @jamesdaniels Seems like fix is not working for me. Getting the following error message after upgrade to 12.3.0:

    Error [ERR_HTTP_HEADERS_SENT]: Cannot render headers after they are sent to the client
        at new NodeError (node:internal/errors:399:5)
        at ServerResponse.writeHead (node:_http_server:382:13)
        at ServerResponse.writeHead (/path-to-my-project/node_modules/on-headers/index.js:44:26)
        at ServerResponse.writeHead (/path-to-my-project/node_modules/on-headers/index.js:44:26)
        at ServerResponse.writeHead (/path-to-my-project/node_modules/next/dist/compiled/compression/index.js:46:263)
        at ServerResponse.writeHead (/path-to-my-project/node_modules/next/dist/compiled/compression/index.js:46:263)
        at /path-to-my-project/node_modules/firebase-tools/lib/frameworks/utils.js:46:18
        at /path-to-my-project/node_modules/firebase-tools/lib/frameworks/utils.js:98:45
    i  hosting: 127.0.0.1 - - [03/Jun/2023:14:08:06 +0000] "GET /api/helloworld HTTP/1.1" 500 - "-" "PostmanRuntime/7.32.2"
    

    That make sense since hostOrRequestHandler is called before proxy with original response.

  4. MidnightLightning commented on Jun 14, 2023

    @MidnightLightning

    I also am getting that error when using Firebase Emulator Tools (using the "web frameworks" logic to host a Next.JS site). I'm not defining any specific redirects, but Next.JS does redirects under the hood. When using firebase-tools v12.3.0, that error of "Cannot render headers after they are sent to the client" happens for files that aren't the main index file (e.g. /_next/static/chunks/node_modules_foo.js).

    Downgrading to [email protected] made this issue go away for my environment.

  5. jasan-s commented on May 30, 2024

    @jasan-s

    This still seems like an issue using next js with firebase hosting , i cant use

    		"rewrites": [
    			{
    				"source": "/internal-api/**",
    				"function": "api"
    			}
    		]
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions