Skip to content

[Bug?]: createMiddleware crashes when calling vinxi's proxyRequest while defineMiddleware works #2035

@llamadeus

Description

@llamadeus

Duplicates

  • I have searched the existing issues

Latest version

  • I have tested the latest version

Current behavior 😯

Calling proxyRequest from my middleware causes both the dev server and the production build to crash:

import { createMiddleware } from "@solidjs/start/middleware";
import { proxyRequest } from "vinxi/http";

export default createMiddleware({
  onRequest: (event) => {
    return proxyRequest(event.nativeEvent, "http://localhost:8080");
  },
});

The console shows the following error:

node:_http_outgoing:699
    throw new ERR_HTTP_HEADERS_SENT('set');
          ^

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at ServerResponse.setHeader (node:_http_outgoing:699:11)
    at setResponseHeader (file:///<redacted>/node_modules/h3/dist/index.mjs:799:18)
    at errorHandler (file:///<redacted>/node_modules/vinxi/lib/dev-error.js:10:2)
    at Object.onError (file:///<redacted>/node_modules/vinxi/lib/nitro-dev.js:159:11)
    at Server.toNodeHandle (file:///<redacted>/node_modules/h3/dist/index.mjs:2303:27)
    at process.processTicksAndRejections (node:internal/process/task_queues:105:5) {
  code: 'ERR_HTTP_HEADERS_SENT'
}

Node.js v22.11.0
error: script "dev" exited with code 1

The above approach works when using defineMiddleware exported from vinxi:

import { defineMiddleware, proxyRequest } from "vinxi/http";

export default defineMiddleware({
  onRequest: (event) => {
    return proxyRequest(event, "http://localhost:8080");
  },
});

EDIT: What's interesting is that it works when I don't return the response from proxyRequest, but only await it.

import { createMiddleware } from "@solidjs/start/middleware";
import { proxyRequest } from "vinxi/http";

export default createMiddleware({
  onRequest: async (event) => {
    await proxyRequest(event.nativeEvent, "http://localhost:8080");
  },
});

Expected behavior 🤔

The middleware should not cause the server to crash.

Steps to reproduce 🕹

Steps:

  1. Create a new SolidStart project.
  2. Create src/middleware.ts and paste the snippet from above.
  3. Register the middleware in app.config.ts:
    export default defineConfig({
      middleware: "src/middleware.ts",
    });
  4. Run docker run -p 8080:80 nginxdemos/hello.
  5. Navigate to http://localhost:3000.
  6. The server should crash.

Context 🔦

I am attempting to write a small application that forwards HTTP requests to another Docker container if the user is authorized to do so.

Your environment 🌎

System:
  OS: maxOS Sequoia 15.0.1
  CPU: Apple M1 Pro
Binaries:
  Node: 22.11.0 - /Users/amadeus/.nvm/versions/node/v22.11.0/bin/node
  npm: 10.9.0 - /Users/amadeus/.nvm/versions/node/v22.11.0/bin/npm
  Bun: 1.2.23 - /opt/homebrew/bin/bun
npmPackages:
  @solidjs/start: ^1.1.0
  vinxi: ^0.5.10

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions