Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hot Reload fails with dynamic imports containing top-level-await #14006

Open
renekaesler opened this issue Sep 18, 2024 · 0 comments
Open

Hot Reload fails with dynamic imports containing top-level-await #14006

renekaesler opened this issue Sep 18, 2024 · 0 comments
Labels
bug Something isn't working needs triage

Comments

@renekaesler
Copy link

renekaesler commented Sep 18, 2024

What version of Bun is running?

1.1.27+267afa293

What platform is your computer?

Linux 6.8.0-44-generic x86_64 x86_64

What steps can reproduce the bug?

The bun --hot functionality does not seem to work correctly when dynamically importing a module which contains a top-level await.

livereload.js
const server = Bun.serve({
  async fetch(req, server) {
    const url = new URL(req.url);

    if (url.pathname === "/livereload") {
      if (server.upgrade(req)) return;
    }

    const template = await import("./template.js");

    return new Response(template.default, {
      headers: { "Content-Type": "text/html" },
    });
  },
  websocket: {
    open(ws) {
      ws.subscribe("livereload");
    },
    close(ws) {
      ws.unsubscribe("livereload");
    },
  },
});

server.publish("livereload", "reload");
template.js
const message = await new Promise(resolve =>
  setTimeout(resolve, 1000, "Bun LiveReload!")
);

export default `
  <!DOCTYPE html>
  <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>LiveReload</title>

      <script>
        const socket = new WebSocket("ws://localhost:3000/livereload");

        socket.addEventListener("message", event => {
          location.reload();
        });
      </script>
    </head>

    <body>
      <h1>${message}</h1>
    </body>
  </html>
`;

Steps to Reproduce:

  1. Start the Server: bun --hot livereload.js
  2. Open localhost:3000 in your Browser
  3. Modify the "Bun LiveReload!" string in template.js

What is the expected behavior?

The website should be reloaded according to the change.

What do you see instead?

The server throws an error:

ReferenceError: Cannot access 'default' before initialization.
      at /temp/livereload.js:12:25
GET - / failed
 7 |     }
 8 | 
 9 |     const template = await import("./template.js");
10 |     // await Bun.sleep(2000);
11 | 
12 |     return new Response(template.default, {

Additional information

When setting the setTimeout to 0ms, the hot reload works as expected.
const message = await new Promise(resolve =>
  setTimeout(resolve, 0, "Bun LiveReload!")
);

export default `
  <!DOCTYPE html>
  <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>LiveReload</title>

      <script>
        const socket = new WebSocket("ws://localhost:3000/livereload");

        socket.addEventListener("message", event => {
          location.reload();
        });
      </script>
    </head>

    <body>
      <h1>${message}</h1>
    </body>
  </html>
`;
When using Bun.sleep(2000) after the import, the hot reload works as expected.
const server = Bun.serve({
  async fetch(req, server) {
    const url = new URL(req.url);

    if (url.pathname === "/livereload") {
      if (server.upgrade(req)) return;
    }

    const template = await import("./template.js");
    await Bun.sleep(2000);

    return new Response(template.default, {
      headers: { "Content-Type": "text/html" },
    });
  },
  websocket: {
    open(ws) {
      ws.subscribe("livereload");
    },
    close(ws) {
      ws.unsubscribe("livereload");
    },
  },
});

server.publish("livereload", "reload");
@renekaesler renekaesler added bug Something isn't working needs triage labels Sep 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs triage
Projects
None yet
Development

No branches or pull requests

1 participant