fiber icon indicating copy to clipboard operation
fiber copied to clipboard

🐛 [Bug]: app.Static, CacheDuration of 0 does not work intuitively

Open eli-rich opened this issue 3 years ago • 5 comments

Bug Description

Setting the CacheDuration to 0 in app.Static does not disable caching. The minimum cache time is 1 * time.Nanosecond

How to Reproduce

Steps to reproduce the behavior:

  1. Create a project and use fiber to serve a static folder.
  2. Set the CacheDuration as shown below.
  3. Edit the index.html in the static folder.
  4. Notice how it does not change on refresh.

Expected Behavior

Disable caching for that static directory.

Fiber Version

v2.39.0

Code Snippet (optional)

package main

import "github.com/gofiber/fiber/v2"
import "log"

func main() {
  app := fiber.New()
  // The below will correctly serve `./public` but cache is not disabled
  app.Static("/", "./public", fiber.Static{
    Compress:      true,
    CacheDuration: 0,
  })
  /*
  This code effectively disables the cache.
  app.Static("/", dir, fiber.Static{
    Compress:      true,
    CacheDuration: 1 * time.Nanosecond,
  })
  */
  app.Listen(":3000")
}

Checklist:

  • [X] I agree to follow Fiber's Code of Conduct.
  • [X] I have checked for existing issues that describe my problem prior to opening this one.
  • [X] I understand that improperly formatted bug reports may be closed without explanation.

eli-rich avatar Nov 08 '22 21:11 eli-rich

Thanks for opening your first issue here! 🎉 Be sure to follow the issue template! If you need help or want to chat with us, join us on Discord https://gofiber.io/discord

welcome[bot] avatar Nov 08 '22 21:11 welcome[bot]

Hi, the challenge here is that we need to find out in the config which is passed if the value would be set or not

If it would not be set we take the default value for the cache duration

Since 0 is the value that is initially set for integer, your expected behavior collides with the previous explained one

Maybe we can disable the cache for negative values

ReneWerner87 avatar Nov 08 '22 21:11 ReneWerner87

Ahh I see. I was unsure if I should even label this a bug since it really just isn't intuitive, not necessarily a bug.

I think the negative values thing is a good idea.

I definitely would not have the default caching behavior be "no caching." Since I think in most cases I would be wanting to cache things.

eli-rich avatar Nov 09 '22 01:11 eli-rich

The FS in fasthttp will set default CacheDuration val when param is less than or equal zero.

https://github.com/valyala/fasthttp/blob/v1.41.0/fs.go#L423.

wangjq4214 avatar Nov 15 '22 14:11 wangjq4214

wait for next version of valyala/fasthttp

peczenyj avatar Nov 06 '23 08:11 peczenyj