cli icon indicating copy to clipboard operation
cli copied to clipboard

Nuxt restart hook breaks HMR from 3.7.4

Open TechAkayy opened this issue 1 year ago • 4 comments

Environment

npx nuxi info

  • Operating System: Darwin
  • Node Version: v20.14.0
  • Nuxt Version: 3.13.0
  • CLI Version: 3.13.1
  • Nitro Version: 2.9.7
  • Package Manager: [email protected]
  • Builder: -
  • User Config: modules, hooks, devtools
  • Runtime Modules: ()
  • Build Modules: -

Reproduction

https://github.com/TechAkayy/nuxt-374

I was testing the previous closed issue - https://github.com/nuxt/cli/issues/135

Clone & install deps. Open nuxt.config.ts, and start dev-server. After re-start in 12 secs, HMR breaks and the client loses connection with the server. Try uncommenting the div in app.vue, HMR is not applied anymore, loses connection with nuxt devtools too.

nuxt.hook('ready', async (nuxt) => {
  // Restart in 12 secs
  setTimeout(() => {
    console.log('Restarting in the 12 secs..')
    nuxt.callHook('restart', {hard: true})
  }, 12000)
})

image

Additional context

Thanks for looking into this. If you require any more info, please let m eknow.

Logs

No response

TechAkayy avatar Sep 26 '23 16:09 TechAkayy

Updated reproduction to latest (3.8) using npx nuxi upgrade --force. Same issue even when running with npx nuxi-edge@latest dev.

TechAkayy avatar Nov 30 '23 04:11 TechAkayy

Same issue with the next plugin:

import mongoose from "mongoose";
import { createTunnel, type ServerOptions } from "tunnel-ssh";

export const connections: { name: string; connection: mongoose.Connection }[] = [];

export default defineNitroPlugin(async (nitro) => {
  console.log("Loading Mongoose plugin...");

  nitro.hooks.hook("request", (event) => {
    console.log("on request ", event.path);
  });

  const config = useRuntimeConfig();

  try {
    const tunnelOptions = { autoClose: false };

    const sshOptions = {
      host: process.env.SSH_HOST,
      port: 22,
      username: process.env.SSH_USERNAME,
      password: process.env.SSH_PASSWORD,
    };

    const serverOptions: ServerOptions = { port: 27017 };

    const forwardOptions = {
      dstAddr: "127.0.0.1",
      dstPort: 27017,
    };

    const [Server, Client] = await createTunnel(tunnelOptions, serverOptions, sshOptions, forwardOptions)
      .then((result) => {
        console.log("Tunnel created successfully");
        return result;
      })
      .catch((err) => {
        console.error("Error creating tunnel", err);
        throw err;
      });

    Client.on("error", (err) => {
      console.error("SSH Client error", err);
    });

    Server.on("error", (err) => {
      console.error("SSH Server error", err);
    });

    const dbs = Object.keys(config).filter(key => key.includes("MongoUri"));

    dbs.forEach((db) => {
      const connection = mongoose.createConnection(config[db] as string);
      connections.push({
        name: db.split("MongoUri")[0],
        connection,
      });

      connection.on("connected", () => {
        console.log(`Connected to ${db} MongoDB`);
      });
    });

    nitro.hooks.hookOnce("close", () => {
      for (const { connection } of connections) {
        if (connection.readyState === 1) {
          connection.close();
        }
      }

      console.log("Disconnected from MongoDB connections");

      Server.close();
      Client.destroy();
      console.log("Closed SSH tunnel");
    });
  }
  catch (error) {
    console.error("Error connecting to MongoDB ", error);
  }
});

image


Is it possible to keep the connection open while HMR restarts? If not, how can I close and open the connection without getting this error?

negativems avatar May 25 '24 07:05 negativems

Updated reproduction to latest (3.11) using npx nuxi upgrade --force, and manually updated package.json (for those changes to the starter template). Same issue even when running with npx nuxi-edge@latest dev.

Also, apart from HMR break, and connection loss, I can notice the same disconnection issue with nuxt devtools too.

Welcome to Nuxt!

TechAkayy avatar Jun 04 '24 04:06 TechAkayy

Updated reproduction to latest (3.13.0) using npx nuxi upgrade --force, and removed vue & vue-router from package.json as they not required to be added explicitly anymore.

Same issue even when running with npx nuxi-edge@latest dev. Thanks for looking into this, let me know if you require any more info.

image

TechAkayy avatar Aug 31 '24 02:08 TechAkayy