Giter VIP home page Giter VIP logo

Comments (9)

delvedor avatar delvedor commented on May 22, 2024 5

decided this was the wrong solution

@coreyfarrell please moderate your tone, an aggressive behavior is not welcomed.

from fastify-static.

delvedor avatar delvedor commented on May 22, 2024 2

This is a bug.

Nope, this is the correct behavior of the API, you are adding multiple times the same decorator.

I'll release very soon the new version of Fastify, which will have hasReplyDecorator, so we can check it directly inside fastify-static if a specific decorator has already been added.

In any case, the main issue will be that a user will get an "unpredictable" behavior from reply.sendFile because it will work only for the first registered static plugin.

The best solution would be register the two instances of fastify-static in two different plugins (which is the main idea behind the Fastify's plugin architecture), in this way there will be no conflicts.

// first plugin
fastify.register((instance, opts, next) => {
  instance.register(require('fastify-static'), {
	root: path.join(__dirname, 'node_modules'),
	prefix: '/node_modules/'
  })
  // here `reply.sendFile` refers to 'node_modules' files
  next()
})

// second plugin
fastify.register((instance, opts, next) => {
  instance.register(require('fastify-static'), {
	root: path.join(__dirname, 'html/myapp'),
	prefix: '/myapp/'
  })
  // here `reply.sendFile` refers to 'html/myapp' files
  next()
})

from fastify-static.

rauschma avatar rauschma commented on May 22, 2024 2

If there is time, it’d be nice to have the official solution documented in the readme: I don’t understand how and where one can do a reply.sendFile() that considers both example directories.

I’d also find it useful if it were possible to merge multiple local directories into the same online directory. Local directories that are mentioned later would provide fallbacks for earlier local directories.

from fastify-static.

mcollina avatar mcollina commented on May 22, 2024 1

@rauschma would you mind to open an issue about this "merging" functionality? I don't know see how that could work.

from fastify-static.

allevo avatar allevo commented on May 22, 2024

Nice catch! Thanks!

This is a bug. Would you mind to send a PR?

from fastify-static.

coreyfarrell avatar coreyfarrell commented on May 22, 2024

I've created an option with docs and a test to ensure this can be dealt with, I don't think it can be fixed another way.

from fastify-static.

mcollina avatar mcollina commented on May 22, 2024

from fastify-static.

coreyfarrell avatar coreyfarrell commented on May 22, 2024

#63 adds an option to disable the reply decorator per instance of fastify-static. This allows fastify-static to be added multiple times in the same plugin context but requires explicitly choosing no more than one to provide reply.sendFile. I'm aware you can put each fastify-static into a separate plugin context but then you don't get reply.sendFile in the global context. I saw hasReplyDecorator in the code, decided this was the wrong solution for this module since each reply.sendFile can have different settings so the conflict must be resolved explicitly.

// first plugin
fastify.register((instance, opts, next) => {
  instance.register(require('fastify-static'), {
	root: path.join(__dirname, 'node_modules'),
	prefix: '/node_modules/'
  })
  // here `reply.sendFile` refers to 'node_modules' files
  next()
})

// second plugin
fastify.register((instance, opts, next) => {
  instance.register(require('fastify-static'), {
	root: path.join(__dirname, 'html/myapp'),
	prefix: '/myapp/'
  })
  // here `reply.sendFile` refers to 'html/myapp' files
  next()
})

// here `reply.sendFile` is undefined.

from fastify-static.

FishOrBear avatar FishOrBear commented on May 22, 2024

This information should be extracted from the document or README.

from fastify-static.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.