fastify-cli icon indicating copy to clipboard operation
fastify-cli copied to clipboard

fast reload (without restart) like Next.js

Open meotimdihia opened this issue 3 years ago • 19 comments

Prerequisites

  • [X] I have written a descriptive issue title
  • [X] I have searched existing issues to ensure the feature has not already been requested

🚀 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

meotimdihia avatar Sep 11 '22 16:09 meotimdihia

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

Eomm avatar Sep 12 '22 06:09 Eomm

"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.

sidwebworks avatar Sep 12 '22 09:09 sidwebworks

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.

meotimdihia avatar Sep 14 '22 09:09 meotimdihia

@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

sidwebworks avatar Sep 14 '22 15:09 sidwebworks

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

meotimdihia avatar Sep 14 '22 16:09 meotimdihia

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

sidwebworks avatar Sep 14 '22 16:09 sidwebworks

@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.

soulsam480 avatar Sep 20 '22 02:09 soulsam480

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?

ekoeryanto avatar Nov 08 '22 01:11 ekoeryanto

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.

  • it takes 4-5s to reload when files are changed.
  • when the process stopped at the breakpoint => I edited the file but didn't resume the program => it was broken here. I have to restart the dev server.
  • The dev server often needs to restart to continue working if you leave it for a long time.

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

meotimdihia avatar Mar 03 '23 18:03 meotimdihia

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.

mcollina avatar Mar 04 '23 16:03 mcollina

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

  • I don't know how to migrate from fastify-cli to Platformatic. Hopefully, we have a guide about this.
  • It looks like the dashboard that is using react-admin. It is great if Platformatic DB compatible with react-admin.
  • As for authentication, currently, I am using https://github.com/ory/kratos I don't know if Platformatic will help here.

I hope it is stable soon.

meotimdihia avatar Mar 04 '23 16:03 meotimdihia

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

meotimdihia avatar Mar 06 '23 17:03 meotimdihia

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

stefanvanherwijnen avatar May 03 '23 17:05 stefanvanherwijnen

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.

mcollina avatar May 03 '23 18:05 mcollina

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?

stefanvanherwijnen avatar May 03 '23 19:05 stefanvanherwijnen

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

meotimdihia avatar May 04 '23 01:05 meotimdihia

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.

stefanvanherwijnen avatar May 04 '23 09:05 stefanvanherwijnen

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 🤔

jarrodpayne avatar Nov 24 '23 23:11 jarrodpayne

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

mcollina avatar Nov 25 '23 07:11 mcollina