fastify / fastify-cli

Run a Fastify application with one command!
MIT License
658 stars 167 forks source link

fast reload (without restart) like Next.js #549

Open meotimdihia opened 2 years ago

meotimdihia commented 2 years ago

Prerequisites

🚀 Feature Proposal

Hi, my project has a lot of routes ~100 routes and ~150 dependencies. And everything is slowing down. I used all tools nodemon , ts-node, ts-node-dev, backpack and fastify-cli (v4) I even purchase a new good CPU with a benchmark score is 24000 But my project still takes 4-5s to restart when a file is changed.

I am using Next.js too. And I am curious why Next.js never needs to restart the server, but other node.js frameworks can't do that 🤔

Motivation

Everyone is happy.

Example

No response

Eomm commented 2 years ago

I think it can do it because of money. Vercel pays devs to work on the Next.js framework full-time.

Vercel raises $150M Series D as it looks to build an end-to-end front-end development platform

sidwebworks commented 2 years ago

"Hot reload" or HMR (Hot module replacement) is something backend devs are trying to solve for a long time,

But AFAIK its more linked to the build system than just the framework alone, eg: Nest JS's hot reload sits on top of Webpack's HMR system. https://docs.nestjs.com/recipes/hot-reload

So maybe if Fastify creates an integration with Vite or something then it might be possible to do.

I probably just scratched the surface but I think @mcollina was doing something with HMR in Node servers.

meotimdihia commented 2 years ago

Hi, I just have used fastify-cli@5 again. It is faster than other solutions. 👍 I ditched ts-node, and cut down 50% reload time.

sidwebworks commented 2 years ago

@meotimdihia If speed is the main thing you're looking for, I've had some good experiences using tsx https://github.com/esbuild-kit/tsx

meotimdihia commented 2 years ago

@sidwebworks I just tested it, it is faster than ts-node but not faster than fastify-cli.

sidwebworks commented 2 years ago

Haha good for us I guess, Fastify stands strong in being the fastest.

soulsam480 commented 2 years ago

@sidwebworks I just tested it, it is faster than ts-node but not faster than fastify-cli.

Idk sir what are your requirements, but for me tsx's been the fastest irrespective of codebase size.

ekoeryanto commented 2 years ago

I have refactored one generated by fastify cli with --lang=ts using bob-ts for simplicity https://github.com/ekoeryanto/fastify-esbuild

should we implement it here, maybe with --bob arg?

meotimdihia commented 1 year ago

For the current state of my project, this is the biggest problem. I am thinking switch to Next.js API. Because debug experience in Fastify is not good for a big project in my idea, I don't know how everyone deals with this. But for me, it is very bad.

Anyway, I often need to restart the dev server 10-20 times/per day if I am working on the API.

mcollina commented 1 year ago

We have a working but sometimes crashing version of it in https://oss.platformatic.dev/. Check out platformatic service, as it has no database component.

The technique that Next.js uses requires quite a lot of transpilation and it leaks memory. We are working on a better and more native solution to this problem. Once it stabilizes, it could be used in fastify-cli.

meotimdihia commented 1 year ago

@mcollina I tried Platformatic, and it sounds good. I have some suggestions:

I hope it is stable soon.

meotimdihia commented 1 year ago

While waiting for other solutions, I can get tsup to work now, it is very fast in comparison with other solutions.
x3-x4 times startup for me.

This is my configuration for tsup

tsup.config.ts

import { defineConfig } from "tsup"

export default defineConfig({
  entry: [
    "src/web/index.ts",
    "src/web/plugins/**/*.ts",
    "src/web/routes/**/*.ts",
    "prisma/**/*.ts"
  ],
  bundle: true,
  platform: "node",
  target: "node18",
  sourcemap: true,
  clean: true
})

package.json


  "scripts": {
      "dev": "export NODE_OPTIONS='--enable-source-maps' && tsup --watch --onSuccess \"fastify start --port 3002 --options  -l info -P  dist/src/web/index.js\"",
  }

Related to https://github.com/fastify/fastify-cli/issues/487

stefanvanherwijnen commented 1 year ago

@mcollina Does fastify support any Hot Module Replacement or is it always required to restart the server?

mcollina commented 1 year ago

You can try platformatic service for a hot module replacement solution.

I think this would trickle down to this module once it's a bit more stable.

stefanvanherwijnen commented 1 year ago

I managed to use Vite's HMR functionality to auto restart the Fastify server, and I was wondering if there is a more efficient way. I think platformatic uses the same approach?

meotimdihia commented 1 year ago

@stefanvanherwijnen Do you know any articles about how to use Vitejs to reload the API code? I think Vitejs just reload the frontend code.

stefanvanherwijnen commented 1 year ago

Here is how I do it.

Basically, once you have Fastify running in Vite, use handleHotUpdate to recreate the server. Also have a look at @fastify/restartable, but I couldn't get it to work because the arguments differ slightly from Fastify server.

paynecodes commented 11 months ago

We have a working but sometimes crashing version of it in https://oss.platformatic.dev/. Check out platformatic service, as it has no database component.

The technique that Next.js uses requires quite a lot of transpilation and it leaks memory. We are working on a better and more native solution to this problem. Once it stabilizes, it could be used in fastify-cli.

Has there been any movement on this (hot reload for fastify apps) issue. The new fastify-vite docs have convinced me this may be worth the effort. Then, I remembered the potential DX hit we'd be incurring due to having to restart the entire server on every change. This could be an overblown concern, but for large apps (especially when debugging) this seems like a huge cost.

I did a bit of reading on platformatic service but I couldn't make heads or tails of what it really is or how it's relevant to this issue 🤔

mcollina commented 11 months ago

All of the above is solved in Platformatic: Fast hot reload with TS support. it's a supercharged Fastify;).