nuxt / cli

⚡️ Nuxt Generation CLI Experience.
https://github.com/nuxt/cli/discussions/3
MIT License
239 stars 51 forks source link

Nuxt restart hook breaks HMR from 3.7.4 #231

Open TechAkayy opened 9 months ago

TechAkayy commented 9 months ago

Environment


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 20 secs
  setTimeout(() => {
    console.log('Restarting in the 20 secs..')
    nuxt.callHook('restart', {hard: true})
  }, 20000)
})

image

Additional context

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

Logs

No response

TechAkayy commented 7 months ago

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

negativems commented 1 month ago

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?

TechAkayy commented 1 month ago

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!