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

Add documentation for using mux to serve a SPA #493

Merged
merged 5 commits into from Jul 20, 2019

Conversation

fharding1
Copy link
Contributor

@fharding1 fharding1 commented Jun 29, 2019

Fixes #464

Summary of Changes

  1. Adds documentation to the README.md for using mux to serve API endpoints alongside a SPA.

Testing

I ran create-react-app, made a prod build, copied the build directory, and tested this out. Seems to work great.

README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
"github.com/gorilla/mux"
)

type spaHandler struct {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be more accessible to make this middleware or more self-contained http.HandlerFunc?

e.g.

func ServeJavaScriptApp(staticDir string, entryPoint string) http.Handler {
    // fn := func(w http.ResponseWriter, r *http.Request) { ... }
    //
    // return http.HandlerFunc(fn)
}

... and then:

serveJSHandler := ServeJavaScriptApp("./dist", "index.html")
r.Use(staticHandler)
r.PathPrefix("/").Handler(serveJSHandler)

Not tied to this - just might be more accessible to a newcomer? Open to feedback on this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. I can see how a http.HandlerFunc might be easier for a beginner than a type implementing http.Handler, however I think the overhead of the closure might outweigh that. Overall I think both are about as easy for a beginner to understand (also open to feedback of course 😄).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, thinking on this: should we add a “ServeStatic” or similar handler as a built-in? This has come up a few times.

README.md Show resolved Hide resolved
@elithrar elithrar mentioned this pull request Jul 6, 2019
Copy link
Contributor

@elithrar elithrar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Options:

a) leave this as docs
b) add a new built-in handler

README.md Show resolved Hide resolved
@fharding1
Copy link
Contributor Author

fharding1 commented Jul 9, 2019

I think implementing a built-in handler in gorilla/mux for the few people trying to use the wrong tool for the job (gorilla/mux to serve an SPA—maybe it fits somewhere else in the gorilla toolkit?) wouldn't be worth adding more stuff to the API. Instead, I think it makes sense to leave this as docs so that people will at least stop opening issues asking how to serve an SPA with mux.

@fharding1
Copy link
Contributor Author

Let me know if you think it still makes sense to add a built-in handler with the docs improvements and I'll implement that 👍

@elithrar
Copy link
Contributor

Let's leave it as-is for now, @fharding1 - we can revisit later. http.FileServer continues to trip users up, so wary of that.

@elithrar elithrar merged commit 7a1bf40 into gorilla:master Jul 20, 2019
@elithrar
Copy link
Contributor

(Thanks for working on this, too!)

@fharding1 fharding1 deleted the spa-docs branch July 20, 2019 16:26
@fharding1
Copy link
Contributor Author

Np, thank you!

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

Successfully merging this pull request may close these issues.

Serve React Router
2 participants