vite-plugin-node icon indicating copy to clipboard operation
vite-plugin-node copied to clipboard

NestApplication reloading after each GET request

Open vic1707 opened this issue 3 years ago • 4 comments

[this part is due to me forgetting if (import.meta.env.PROD) around the bootstrap function] to jump to the issue

image As you can see I get all the init messages twice 🤔.

here is the vite.config.ts

import { defineConfig } from 'vite';
import { VitePluginNode } from 'vite-plugin-node';

// eslint-disable-next-line import/no-default-export
export default defineConfig({
  server: {
    port: 3000
  },
  plugins: [
    ...VitePluginNode({
      appName: 'stargraph-api',
      adapter: 'nest',
      appPath: './src/main.ts',
      exportName: 'viteNodeApp',
      tsCompiler: 'swc'
    })
  ],
  optimizeDeps: {
    exclude: [
      '@nestjs/microservices',
      '@nestjs/websockets',
      'cache-manager',
      'class-transformer',
      'class-validator',
      'fastify-swagger'
    ]
  }
});

and my main.ts

import { NestFactory } from '@nestjs/core';
import { DocumentBuilder, SwaggerCustomOptions, SwaggerModule } from '@nestjs/swagger';

import { ApiModule } from './api.module';

(async () => {
  const app = await NestFactory.create(ApiModule);

  const config = new DocumentBuilder()
    .setTitle('Cats example')
    .setDescription('The cats API description')
    .setVersion('1.0')
    .addTag('cats')
    .build();

  const swaggerCustomOptions: SwaggerCustomOptions = {};

  const document = SwaggerModule.createDocument(app, config);
  SwaggerModule.setup('api', app, document, swaggerCustomOptions);

  await app.listen(3000);
})();

export const viteNodeApp = NestFactory.create(ApiModule); // this returns a Promise, which is ok, this plugin can handle it

The rest of the application is the auto generated one

And those are the versions of the packages I used :

    "@nestjs/common": "^8.4.7",
    "@nestjs/core": "^8.4.7",
    "@nestjs/microservices": "^8.4.7",
    "@nestjs/platform-express": "^8.4.7",
    "@nestjs/swagger": "^5.2.1",

    "@swc/core": "^1.2.207",
    "typescript": "^4.7.4",
    "vite": "^2.9.13",
    "vite-plugin-node": "^1.0.0"

Thanks for that awesome plugin, hope I gave enough infos for you to debug ^^

vic1707 avatar Jun 29 '22 15:06 vic1707

also sometimes each time I get the base URL I get the log for Nest application successfully started 🤔 image

vic1707 avatar Jun 29 '22 20:06 vic1707

adding the if (import.meta.env.PROD) because I'm an idiot seems to fix the duplicate instance but doesn't fix the restart(?) of the app after each get image

vic1707 avatar Jun 29 '22 20:06 vic1707

when I run the production version I still get two instances image

here's my new main.ts

import { Logger } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import { DocumentBuilder, SwaggerCustomOptions, SwaggerModule } from '@nestjs/swagger';

import { ApiModule } from './api.module';

if (process.env.NODE_ENV === 'production') {
  (async () => {
    const PORT = process.env.API_PORT || 3000;
    const app = await NestFactory.create(ApiModule);

    const config = new DocumentBuilder()
      .setTitle('Cats example')
      .setDescription('The cats API description')
      .setVersion('1.0')
      .addTag('cats')
      .build();

    const swaggerCustomOptions: SwaggerCustomOptions = {};

    const document = SwaggerModule.createDocument(app, config);
    SwaggerModule.setup('api', app, document, swaggerCustomOptions);

    Logger.log(`Server running on 'localhost:${PORT}'`, 'Bootstrap');
    await app.listen(PORT);
  })();
}

export const viteNodeApp = NestFactory.create(ApiModule);

vic1707 avatar Jun 30 '22 11:06 vic1707

I think with the repo here: https://github.com/axe-me/vite-plugin-node/issues/58 --- this can be reproduced and tested against as it is merely a symptom of this issue.

sebastiangug avatar Aug 09 '22 09:08 sebastiangug

let instance;

export const NestHandler: RequestAdapter<INestApplication> = async ({
  app,
  req,
  res,
}) => {
  if (!instance) {
    await app.init();
    instance = app.getHttpAdapter().getInstance();
  }

  // Todo: handle nest-fastify case

  instance(req, res);
};

MarioJuniorPro avatar Sep 23 '22 16:09 MarioJuniorPro

closing along #67 merge

axe-me avatar Jan 12 '23 03:01 axe-me