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

alter index html response #33

Open
samselikoff opened this issue Jan 21, 2015 · 14 comments
Open

alter index html response #33

samselikoff opened this issue Jan 21, 2015 · 14 comments

Comments

@samselikoff
Copy link
Contributor

samselikoff commented Jan 21, 2015

Just jotting an idea down, Luke mentioned in his talk they sometimes alter the HTML response after it was fetched from Redis. For example, they inject current_user into a meta tag, so the Ember app doesn't have to make an XHR request. You could also add

  • CSRF tokens
  • analytics params
  • config/feature flags
  • embedded JSON payload required for initial page render

We may want to eventually have an API for this type of thing.

@hhff
Copy link
Contributor

hhff commented Mar 10, 2015

was just about to open an issue for this - this would be amazing

@hhff
Copy link
Contributor

hhff commented Mar 10, 2015

Perhaps the developer simply defines a hash in the app controller to be converted to meta tags

def feb_meta(app_name)
    if app_name == "blog"
        { yapp_user: current_user }
    end
end

@samselikoff
Copy link
Contributor Author

Just brainstorming with Ryan about this, one way we could design this would be to have user-defined transforms.

class MyTransformer < FrontEndBuilds::Transformer
  def transforms(scope, html)
    append_to_head(html, "<meta value=#{scope.current_user.to_json}")
  end
end

FrontEndBuilds.register_transforms(MyTransformer)

# every request, instantiate the transformers, and call #transform on the scope and html

add_to_head could be a helper method provided by FEB, which adds the string to the end of the <head> tag in the index html (which was fetched from the db). You could similarly define prepend_to_head, append_to_body and prepend_to_body methods.

This way, FEB could provide a CSRF transformer that works out the box (you could disable it), and we could apply that transform in addition to any user-generated ones.

@ngottlieb
Copy link

Hmm, I like the flexibility of that idea, but I wonder if it isn't more complex than is needed. @hhff's suggestion does everything I need and strikes me as a lot simpler and I would guess covers the vast majority of people's requirements. Especially given that the existing BestsController already has logic for injecting meta tags, so adding the custom hash to that would really be trivial.

@samselikoff
Copy link
Contributor Author

Yep, great point. I think this covers all known use cases for now, and much simpler!

@ngottlieb
Copy link

Cool -- I'll try to get this sorted and submit a PR this week sometime.

@hhff
Copy link
Contributor

hhff commented Sep 21, 2015

i tend to think it would be worth exposing a low level hook that the index.html string gets passed through for the developer to override and do whatever manipulation they want too. maybe that's where we call the setup_metatags hook?

@ngottlieb
Copy link

I wonder if it isn't worth making that a separate feature, though? Just because the mechanics for meta tags is already there, and it's a very simple and common requirement.

@hhff
Copy link
Contributor

hhff commented Sep 21, 2015

yeah, i mean it's just 3 lines of code to add the low level hook also

@ngottlieb
Copy link

I guess I could go either way -- I'm relatively new to this gem, so I'll defer to you guys.

@samselikoff
Copy link
Contributor Author

Why don't we start with a feb_meta method that returns a hash

@ngottlieb
Copy link

How are you guys envisioning that a user/developer overrides feb_meta? I'd love to do something like Devise does so you could call super and incorporate the default meta tags + your own, but the way Devise makes that work involves a lot of overhead that I'm sure we don't want. So I just tried implementing a solution to that using Rails' decorator motif and it doesn't seem great. It doesn't end up saving the developer much time over just decorating the BestsController method and overriding meta_tags.

https://github.com/ngottlieb/front_end_builds/tree/feb_meta

Thoughts? It could be pretty neat to go further with this and expose setting custom meta variables to the UI per app or per build.

@samselikoff
Copy link
Contributor Author

I think what you have solves your need, and we're happy to merge it in. We can revisit it later as more needs arise - I guess we don't have a ton of time just this moment to look further into this. How does that sound?

@ngottlieb
Copy link

Works for me, I'll submit a PR. I'd be happy to do more work on it with a bit of direction -- whether that's towards exposing customizable meta tags to the UI or building the Transformers idea out -- I think both are useful ideas.

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

3 participants