Fiber: ๐Ÿค” Relative path for Static() files

Created on 4 Jun 2020  ยท  10Comments  ยท  Source: gofiber/fiber

Question description

Please take a look at the following Code snippet which serve files via Static() function.

https://github.com/suntong/fiber_demo/blob/master/app/static1.go

The result is much different than them being serving from normal http server or as plain file://.

How can I make relative paths work for Static() files? Thx

โ™ป Wait for Response

Most helpful comment

I used your static files using the code snippet you linked, and I got valid results.
Did you try to clear your browser cache, or use incognito mode?


package main

import (
    "github.com/gofiber/fiber"
)

func main() {
    app := fiber.New()

    app.Static("/", "../../static") // http://localhost:3000

    app.Listen(3000)
}

All 10 comments

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

Hello @suntong, thank you for opening your first issue ๐Ÿฅณ!

I tried to re-produce your described problem, but it seems to work on my end ( windows ). It could be browser-specific, some content-types are initiated as download by default.

package main

import (
    "github.com/gofiber/fiber" // v1.10.5
)

func main() {
    app := fiber.New()

        // contains index.html outside the app folder
    app.Static("/", "../fiber/.github")

    app.Listen(3000)
}

Could you show me what kind of files you are serving?

Oh, they're at
https://github.com/suntong/fiber_demo/tree/master/web/sample0

My fiber was lastest I got yesterday.

I used your static files using the code snippet you linked, and I got valid results.
Did you try to clear your browser cache, or use incognito mode?


package main

import (
    "github.com/gofiber/fiber"
)

func main() {
    app := fiber.New()

    app.Static("/", "../../static") // http://localhost:3000

    app.Listen(3000)
}

Oh, interesting, I'll try, and get back to you. strange...

OK. It turns out that I was using a method that you haven't covered/tested before.

So I've made mine working, by visiting http://localhost:3000/sample0/index.html
However, if I go back to my old visiting method, i.e., merely http://localhost:3000/sample0, then all those should be found are not found.

Please double check. thx.

Oh, looking at your example again, I realized that you might never duplicate what I found unless you use prefix, instead just plain root /. I.e., use

app.Static("/prefix", "./public")

instead of just

app.Static("/", "./public")

Hi @suntong, I double checked our tests and even added more to see if this was a internal bug.
But my conclusion is that it isn't, I forgot to tell you that this is the correct browser behaviour.

When you add a prefix to your static files it's basically a fake URL, therefor you need to correct the file paths in your index.html

<link rel="stylesheet" href="/styles.css" />

becomes

<link rel="stylesheet" href="/prefix/styles.css" />

I hope this solves your problem :)

ok, give me some time to test on real HTTP servers, then I'll close it.

Hi Fenny, I tested on my real HTTP servers, and was able to get correct view no matter what:

image

I know using absolute path will absolutely work, but the question is using and making relative paths work. Since real HTTP servers work, I think so should fiber be. Don't worry, the fix is easy --

  • Let me start with the observation, then the fix will be obvious. http://localhost:3000/sample0 will not work while http://localhost:3000/sample0/ will work.
  • So the fix is as simple as detecting if the url is on a folder than server with / appending to it.
  • That's it.

Please consider.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jeyraj picture jeyraj  ยท  4Comments

mewben picture mewben  ยท  3Comments

Ivan-Feofanov picture Ivan-Feofanov  ยท  3Comments

bashery picture bashery  ยท  4Comments

mayowa picture mayowa  ยท  3Comments