Skip to content

Serving HTML imports at nested path (e.g. /foo/bar) fails to import client-side JS when using a single-file executable #23431

@luketurner

Description

@luketurner

What version of Bun is running?

1.2.23+cf1367137

What platform is your computer?

Linux 6.8.0-1030-azure x86_64 x86_64

What steps can reproduce the bug?

Define a server.ts like this:

import indexPage from "./index.html";

Bun.serve({
  routes: {
    '/*': indexPage
  }
})

with an index.html like this:

<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Demo</title>
    <script type="module" src="./client.ts" async></script>
  </head>
  <body>
  </body>
</html>

And a client.ts like this:

alert("client code loaded");

Then compile the server into a single-file executable and run it:

bun build server.ts --compile
./server

Then open https://localhost:3000/foo/bar in the browser. Note that the client JS is not loaded. (whereas it is loaded for e.g. https://localhost:3000/foo).

Appears to be because the bundled HTML is importing the client JS using a relative path, which fails for nested paths:

<script type="module" crossorigin src="./chunk-crqcz4h7.js"></script>

What is the expected behavior?

The client JS should be loaded. Probably either the bundled HTML should load the JS using an absolute path (/chunk-crqcz4h7.js instead of ./chunk-crqcz4h7.js) or the server should serve up the JS for the relative paths.

What do you see instead?

The browser attempts to load the JS chunk at a URL like http://localhost:3000/foo/chunk-crqcz4h7.js, but the server responds with HTML instead, resulting in a NS_ERROR_CORRUPTED_CONTENT error and the client JS does not execute.

Additional information

Complete reproduction steps and code are provided here: https://github.com/luketurner/bun-html-serve-repro.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingbundlerSomething to do with the bundler

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions