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

🤗 [Question]: app.Static(...) function sends content-type "text/plain" instead of "application/javascript" #2659

Closed
3 tasks done
yaghoubi-mn opened this issue Oct 5, 2023 · 5 comments

Comments

@yaghoubi-mn
Copy link

Question Description

hello
i work in project with gofiber and react
when i serve the static files with app.Static("...", "...") the browser send request for main.jsx file and server send response, the problem is server should send content-type "application/javascript" and instead of it, server send "text/plain", and browser block the file after file received.
how can i fix it.

Code Snippet (optional)

No response

Checklist:

  • I agree to follow Fiber's Code of Conduct.
  • I have checked for existing issues that describe my questions prior to opening this one.
  • I understand that improperly formatted questions may be closed without explanation.
@welcome
Copy link

welcome bot commented Oct 5, 2023

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

@yaghoubi-mn yaghoubi-mn changed the title 🤗 [Question]: app.Static(...) function sends content-type "text/plain" instand of "application/javascript" 🤗 [Question]: app.Static(...) function sends content-type "text/plain" instead of "application/javascript" Oct 5, 2023
@peczenyj
Copy link
Contributor

peczenyj commented Oct 5, 2023

I think the issue came from fasthttp

https://github.com/valyala/fasthttp/blob/master/fs.go#L1324

func (h *fsHandler) newFSFile(f *os.File, fileInfo os.FileInfo, compressed bool, fileEncoding string) (*fsFile, error) {
...
	// detect content-type
	ext := fileExtension(fileInfo.Name(), compressed, h.compressedFileSuffixes[fileEncoding])
	contentType := mime.TypeByExtension(ext)
	if len(contentType) == 0 {
		data, err := readFileHeader(f, compressed, fileEncoding)
		if err != nil {
			return nil, fmt.Errorf("cannot read header of the file %q: %w", f.Name(), err)
		}
		contentType = http.DetectContentType(data) // this probably is reading data and setting the wrong content type

you can test local if your system can recognize the extension .jsx

package main

import (
	"fmt"
	"mime"
)

func main() {
	fmt.Println("jsx", mime.TypeByExtension(".jsx"))
	fmt.Println("js", mime.TypeByExtension(".js"))
}

if not, you have two options:

  1. add/update the .jsx into one of the mime type files https://pkg.go.dev/mime#TypeByExtension
  2. you can add a handler to modify the response and update the content type on such cases: https://pkg.go.dev/github.com/gofiber/fiber/v2#Static like this example: https://github.com/gofiber/fiber/blob/master/app_test.go#L929

enjoy

@ReneWerner87
Copy link
Member

this problem has been documented many times and is due to the golang version and or operation system

#352
#1763
golang/go#32350

check this
golang/go#32350 (comment)

@yaghoubi-mn
Copy link
Author

Thanks for your helps
i searched more and i found the solution
the solution: i must build the react app (npm run build) and the set static to builded file (app.Static("/", "frontend/dist"))

@peczenyj your solution work but the browser can't read jsx file and it got syntax error

@ReneWerner87 i'm not have windows but i searched to add jsx type to linux mime database but i can't do it.

Thanks

@ReneWerner87
Copy link
Member

since you have found the solution, i will close the issue

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

No branches or pull requests

3 participants