Code Monkey home page Code Monkey logo

Comments (4)

joerdav avatar joerdav commented on August 25, 2024

I believe you might need to return here:

if !isHtml(w) {
			next.ServeHTTP(w, r)
			return
}

from templ.

gfffrtt avatar gfffrtt commented on August 25, 2024

I believe you might need to return here:

if !isHtml(w) {
			next.ServeHTTP(w, r)
			return
}

Yes it was that, but now I cant get the content type from the header, every time I print it print []map

func isHtml(w http.ResponseWriter) bool {
	return strings.HasPrefix(w.Header().Get("Content-Type"), "text/html")
}

type App struct {
	Router *chi.Mux
}

func NewApp() *App {
	router := chi.NewRouter()
	router.Use(middleware.Logger)
	return &App{
		Router: chi.NewRouter(),
	}
}

func (a *App) Middleware(next http.HandlerFunc) http.HandlerFunc {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if !isHtml(w) {
			next.ServeHTTP(w, r)
			return
		}

		w.Write([]byte("<p>Loading...</p>"))
		next.ServeHTTP(w, r)
	})
}

func (a *App) Component(route string, handler http.HandlerFunc) {
	a.Router.Get(route, a.Middleware(handler))
}

func (a *App) Listen(port string) {
	http.ListenAndServe(port, a.Router)
}

func main() {
	app := NewApp()

	app.Component("/", func(w http.ResponseWriter, r *http.Request) {
		w.Header().Add("Content-Type", "text/html")
		views.Component().Render(context.Background(), w)
	})

	app.Listen(":3000")
}

from templ.

joerdav avatar joerdav commented on August 25, 2024

This may not be the only issue, but I can see that you are writing to the response before setting headers, you should set the header in the middleware.

from templ.

gfffrtt avatar gfffrtt commented on August 25, 2024

@joerdav Thank you for your help, but I figured out what I actually had to do, what I wanted is to intercept the reponse, so I had to first run the next handler than modify the response if it is html, but thank you man.

image

from templ.

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.