question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Fastify hooks on the route opts type

See original GitHub issue

Prerequisites

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

🚀 Feature Proposal

This would allow to simplify the custom request properties example on the readme https://github.com/fastify/fastify-nextjs#custom-properties-on-the-request-object

Instead of needing a register with a prefix, you could add the hook on the .next call directly.

fastify.next('/hello', {
  onRequest: function (request, reply, done) {
    // define a custom property on the request
    request.raw.customProperty = { hello: "world" }
    // OR make the instance of fastify-redis available in the request
    request.raw.redisInstance = instance.redis
    done()
  }
});

Motivation

Currently it is not possible (as far as I can tell?) to apply a hook to the routes handled by the next plugin only, unless there is a route prefix like in the readme example.

Example

fastify.next('*', {
  {
    onRequest: () => {
      // hook contents here
    },
  },
});

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
mcollinacommented, Jan 6, 2022

A PR would be highly welcomed.

1reaction
stefeecommented, Jan 5, 2022

I found that encapsulating the .next(‘*’) in a plugin (with .register) fixed my issue - now the onRequest is only called for requests handled by Next.js.

I also have an early return in the onRequest handler for public static files:

const PUBLIC_FILE_PATHS = [
  '/_next/',
  '/static/',
  '/favicon.ico',
  '/robots.txt',
];
      const { url } = request;
      const isPublicFile = PUBLIC_FILE_PATHS.some((path) =>
        url.startsWith(path),
      );

      if (isPublicFile) {
        return;
      }

I was going to try to find a better way of doing this - maybe just disabling static file serving in the plugin and doing it with a separate plugin instead.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Hooks - Fastify
Hooks. Hooks are registered with the fastify.addHook method and allow you to listen to specific events in the application or request/response lifecycle.
Read more >
Policy hooks using fastify.route method - Medium
In this blog, how policies are handled by middleware operations at the route level with help of the fastify route method using pre-handler...
Read more >
Apply Hooks only to some routes · Issue #185 · fastify/help
What I understood is that if in my plugin I register the hooks like that module.exports = function (fastify, opts, done) { fastify.addHook(....
Read more >
What is the order of execution of the same-type hooks in fastify?
This shows that the hooks in the options for the routes are always added after the addHook handlers. How Fastify executes hooks. This...
Read more >
Hooks - Fastify
Hooks. By using the hooks you can interact directly inside the lifecycle of Fastify. There are five different Hooks that you can use...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found