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

Frontend website strategy #53

Closed
metakermit opened this issue Oct 15, 2018 · 18 comments
Closed

Frontend website strategy #53

metakermit opened this issue Oct 15, 2018 · 18 comments

Comments

@metakermit
Copy link
Contributor

Really like the concepts inside responder! I'm wondering what would be the strategy for integrating frontend websites? I see webpack is mentioned in the README. Would something like https://parceljs.org/ also be interesting? Since the choice of frontend framework / tooling is pretty tangental to other things in responder, would it maybe make sense to just round up a few example folders or cookiecutters and link them up? E.g.

  • responder + webpack + react
  • responder + parcel + react
  • responder + angular

One thing that would make sense to standardise IMHO would be the build command in package.json. E.g. always have an npm run build command for building the frontend. That way a standard responder Heroku buildpack / Dockerfile could be used for deploying the webapp no matter which frontend stack is used.

@kennethreitz
Copy link
Owner

I like that.

@kennethreitz
Copy link
Owner

running 'npm build' that is.

@metakermit
Copy link
Contributor Author

OK, great! I'll see if I can cook up a nice working example with Parcel + React and then we can talk about an official buildpack / Dockerfile or something that would run npm install / npm run build if there's a package.json and therefore encode this standard 🙂.

@metakermit
Copy link
Contributor Author

metakermit commented Oct 16, 2018

As a first baby step, I've made a very minimal React + Parcel example featuring:

  • ES6 modules
  • image importing
  • frontend routing (using React-Router)

To deploy it's enough to do npm install & npm run build which packages everything in a dist folder ready for serving.

Here's the code:

https://github.com/metakermit/responder-react

I haven't touched any Responder stuff so far, but I see that there are going to be some challenges:

  • it should be possible to serve static files on / instead of static/
  • / should serve index.html
  • all urls not found in Responder's own routes should be forwarded to index.html (which would be needed for frontend routing)

Starlette has options for some of these things, but I think they aren't all exposed in Responder.

As a side-note, I did solve some of these issues with Django & Whitenoise here, so I have some ideas for how these things could be solved. Not sure what the best approach would be in Responder architecture-wise.

@kennethreitz
Copy link
Owner

I have a prototype available here: #72

@kennethreitz
Copy link
Owner

kennethreitz commented Oct 17, 2018

@metakermit off master, you can now run static from /, but that disables all other routes;

api = responder.API(static_route="/")

@metakermit
Copy link
Contributor Author

metakermit commented Oct 17, 2018

Wow, that's a great start. I'll continue working on this tonight and see if I can get the cli to build the frontend if it's there (e.g. a package.json is present) and copy whatever's in dist to the static folder.

kennethreitz added a commit that referenced this issue Oct 17, 2018
@kennethreitz
Copy link
Owner

kennethreitz commented Oct 17, 2018

Now, off master, if you do this:

api.add_route("/", static=True)

static/index.html's content will be the response to /.

kennethreitz added a commit that referenced this issue Oct 17, 2018
@kennethreitz
Copy link
Owner

Now, off master, if you have a static=True route, all routes will be forwarded to static index.html responder.

kennethreitz added a commit that referenced this issue Oct 17, 2018
@kennethreitz
Copy link
Owner

this still leaves

it should be possible to serve static files on / instead of static/

but I think that's a small workaround, for now.

@metakermit
Copy link
Contributor Author

@kennethreitz Perfect, that solves most of the SPA requirements, I think 🎉 I'll test it and if it works, we can later see if we want to have some order of precedence in the future, e.g. in the static=True mode:

  1. check responder routes first (e.g. for some /api/users/ endpoints etc.)
  2. if there aren't any, redirect everything to index.html to handle
  3. the frontend then checks its routing and is responsible for the 404 page etc.

@kennethreitz
Copy link
Owner

@metakermit that's exactly the flow that's in place.

@kennethreitz
Copy link
Owner

I don't know much about single-page web apps, but this will always result in a 200 response from the server, even if it appears as though it's 404.

@kennethreitz
Copy link
Owner

unless there's a specific 404 view defined

@kennethreitz
Copy link
Owner

cool, i'll ship it :shipit:

@kennethreitz
Copy link
Owner

v0.1.0 released, which includes this.

@metakermit
Copy link
Contributor Author

Sure, I think that's fine. I think the way it's done is that if the frontend doesn't find its template, it renders a 404 page. Traditionally this is done by setting nginx to forward everything to index.html, but with Responder we don't need nginx 🙂

Awesome! 🚀

@metakermit
Copy link
Contributor Author

Just an update that I've managed to update https://github.com/metakermit/responder-react to seamlessly deploy to Heroku with everything working as expected 🎉

It took some custom Parcel flags, a custom Heroku build step and an app.json that configures multiple buildpacks, but now you can simply hit that "Deploy to Heroku" button and you have a super smooth Responder + React SPA experience 🙂

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

No branches or pull requests

2 participants