Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to Fastify v4 #224

Merged
merged 4 commits into from
May 5, 2022
Merged

Update to Fastify v4 #224

merged 4 commits into from
May 5, 2022

Conversation

mcollina
Copy link
Member

@mcollina mcollina commented May 4, 2022

Due to the changes introduced in fastify/fastify#2954, we need to change how this plugin is registered from:

  const fastify = Fastify()
  fastify.register(compressPlugin, {
    global: true
  })
  fastify.get('/one', (request, reply) => {
    reply.send(json)
  })

to

  const fastify = Fastify()
  await fastify.register(compressPlugin, {
    global: true
  }) // we must use await or after now.
  fastify.get('/one', (request, reply) => {
    reply.send(json)
  })

The fundamental problem of the change is that in Fastify v4 routes are now defined synchronously while plugins are defined asynchronously. Therefore requiring the await as there is no library to look for the onRoute hook.

Note that users of nested plugins are not impacted, so the following will still work:

  const fastify = Fastify()
  fastify.register(compressPlugin, {
    global: true
  })
  fastify.register(async (fastify) => {
    fastify.get('/one', (request, reply) => {
      reply.send(json)
    })
  })

Are we ok with the change?

cc @metcoder95 @fastify/core @fastify/plugins

Checklist

@Eomm
Copy link
Member

Eomm commented May 4, 2022

The fundamental problem of the change is that in Fastify v4, routes are now defined synchronously while plugins are defined asynchronously. Therefore requiring the await as there is no library to look for the onRoute hook.

I missed that change! 😱

Now the onRoute position is now like the onRegister one

const fastify = require('fastify')({ logger: true })
fastify.addHook('onRoute', function hook(routeOptions) {
  console.log('works')
})
fastify.get('/', async (request, reply) => {
  return { hello: 'world' }
})
fastify.addHook('onRoute', function hook(routeOptions) {
  console.log('does not work')
})
fastify.ready()

Are we ok with the change?

Are we discussing if the onRoute hooks should be async or not?

@mcollina
Copy link
Member Author

mcollina commented May 4, 2022

Now the onRoute position is now like the onRegister one

Exactly

Are we discussing if the onRoute hooks should be async or not?

Yes, exactly

@Fdawgs
Copy link
Member

Fdawgs commented May 4, 2022

I may have missed this in previous issues re v4, so apologies if this has already been stated, but are we moving to ES modules?
The changes from require to import in the readme have thrown me.

@mcollina
Copy link
Member Author

mcollina commented May 4, 2022

No. I have changed to import because top-level-await is available only on esm. The snippets would be invalid otherwise. I can restore require or just remove it in favor of fastifyCompress

README.md Show resolved Hide resolved
@jsumners
Copy link
Member

jsumners commented May 5, 2022

I think this is okay. I'm really wondering if we should move to removing the callback api completely, but that's a radical idea.

We need to fully illustrate this breaking change in the release notes.

Copy link
Member

@delvedor delvedor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with a nit.

I'm really wondering if we should move to removing the callback api completely, but that's a radical idea.

I agree, maybe deprecating them in v4 and completely remove in v5?

@@ -551,6 +551,6 @@ function createError (code, message, statusCode) {
}

module.exports = fp(compressPlugin, {
fastify: '3.x',
fastify: '4.x',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think here we can keep ≥3, as the plugin code doesn't change. It only changes how plugin users are registering it. But this code as is could still work in v3.x

Suggested change
fastify: '4.x',
fastify: '>=3.x',

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From past experiences, maintaining that is a nightmare. We need separate tests for v3 and v4. I prefer to bump the major so we can avoid any compatibility issues down the line.

@mcollina
Copy link
Member Author

mcollina commented May 5, 2022

v4 is already too late. Let's focus on shipping with the current set of features.

@fox1t
Copy link
Member

fox1t commented May 5, 2022

Maybe it will generate some confusion initially, but having fewer options might be more straightforward to the users in the long run.

@Eomm
Copy link
Member

Eomm commented May 5, 2022

Now the onRoute position is now like the onRegister one

Exactly

Are we discussing if the onRoute hooks should be async or not?

Yes, exactly

talking about the onRoute hook, if we compare the async vs sync, I see only pros for the async version.
Meanwhile, the sync onRegister is a technical obligation, it is not clear to me the reasoning to implement the sync onRoute

@climba03003 climba03003 dismissed their stale review May 5, 2022 07:42

My mistake, it actually works

Copy link
Member

@climba03003 climba03003 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

index.js Outdated Show resolved Hide resolved
Co-authored-by: KaKa <climba03003@gmail.com>
@mcollina mcollina merged commit 500b26a into master May 5, 2022
@Fdawgs
Copy link
Member

Fdawgs commented May 5, 2022

Do we still need engines in package.json, or does that need updating to node 14.x?
Also the version in package.json wasn't updated with the v6.0.0 release?

@climba03003
Copy link
Member

climba03003 commented May 5, 2022

Do we still need engines in package.json, or does that need updating to node 14.x?

IMO, engines fields should be removed, it is hard to manage.

Also the version in package.json wasn't updated with the v6.0.0 release?

I think it just forgotten to push the related commit.

@Fdawgs Fdawgs mentioned this pull request May 5, 2022
1 task
@mcollina mcollina deleted the update-to-v4 branch May 5, 2022 13:10
@mcollina
Copy link
Member Author

mcollina commented May 5, 2022

Fixed the tag.

@metcoder95
Copy link
Member

LGTM as well, thanks for bringing me in the loop. Sorry for the headache, now I think I didn't document the changes properly, sorry for that 🙁

I think this is okay. I'm really wondering if we should move to removing the callback api completely, but that's a radical idea.

We need to fully illustrate this breaking change in the release notes.

Agree

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants