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

Change div tags to script tags for props #411

Closed
wants to merge 14 commits into from
Closed

Change div tags to script tags for props #411

wants to merge 14 commits into from

Conversation

martyphee
Copy link
Contributor

@martyphee martyphee commented May 4, 2016

WIP - Changed div's to script tags.


This change is Reviewable

@justin808
Copy link
Member

@martyphee Any update on this one?

@martyphee
Copy link
Contributor Author

Sorry, no. I'll be working on it again this week. I've been in London
with my family for two weeks and haven't had time to work on it.

On Sun, Jun 26, 2016 at 1:16 AM, Justin Gordon [email protected]
wrote:

@martyphee https://github.com/martyphee Any update on this one?


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#411 (comment),
or mute the thread
https://github.com/notifications/unsubscribe/AAuEtvZVyx7pTWDjrYKIeRWSQVzPPEnjks5qPcTsgaJpZM4IXHeO
.

@jbhatab
Copy link
Member

jbhatab commented Jul 8, 2016

@martyphee let me know if you wanna chat about this at all too.

@martyphee
Copy link
Contributor Author

Yes, do you have any time tomorrow?

On Thu, Jul 7, 2016 at 7:03 PM, Blaine Hatab [email protected]
wrote:

@martyphee https://github.com/martyphee let me know if you wanna chat
about this at all too.


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#411 (comment),
or mute the thread
https://github.com/notifications/unsubscribe/AAuEtlMThyeMifROgTFfnEuxseackYSHks5qTZPogaJpZM4IXHeO
.

@justin808
Copy link
Member

Please refer to:

To merge, we need:

  1. Ensure this works just fine with both server and client rendering.
  2. Ensure this works flawlessly with Rails 5 and Turbolinks 5.
  3. Ensure that we've properly addressed all possible sources of injected scripts.
  4. Ensure that the performance gain is worth any extra complexity.

Take a look at the home page for https://www.producthunt.com/

Seems that ProductHunt uses this strategy of a script.

2016-07-11_13-15-20

They probably have done tons of optimization.

Interesting that the embedded store values seem to be a string, rather than an object.

    window.initialState = "[\"^ \",\"annotations\",[\"~#iM\",[69195,[\"~#iR\",[\"^ \",\"n\",\"AnnotationRecord\",\"v\",[\"^ \",\"type\",\"top\",\"postId\",null,\"categoryId\",1,\"userCount\",0,\"userIds\",[]]]],69203,[\"^2\",[\"^ \",\"n\",\"AnnotationRecord\",\"v\",[\"^ \",\"^3\",\"top

So maybe the strategy is to convert to an HTML JSON string, and then JSON parse the string?
2016-07-11_13-21-19

@justin808
Copy link
Member

@martyphee Any update?

@justin808
Copy link
Member

@martyphee How's this one going? Any preliminary performance numbers?

@martyphee
Copy link
Contributor Author

martyphee commented Aug 1, 2016 via email

@justin808
Copy link
Member

@martyphee Keep us posted. Hopefully you'll have some perf numbers to share!

@justin808
Copy link
Member

@martyphee How's this one going?

@justin808 justin808 modified the milestone: 6.1 Aug 8, 2016
@martyphee
Copy link
Contributor Author

Sorry for the delay. I'm on vacation right now. No plans to head over seas
for a while so I hope to revisit not week.

On Monday, August 8, 2016, Justin Gordon [email protected] wrote:

@martyphee https://github.com/martyphee How's this one going?


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#411 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAuEtmsYvn9w6ngmlid86AqdnAENTWjfks5qdvqugaJpZM4IXHeO
.

@justin808
Copy link
Member

Hi @martyphee I'm about to push out 6.1 this weekend. Would you like to try to get this in?

@martyphee
Copy link
Contributor Author

martyphee commented Aug 20, 2016

I have two failing tests yet.

1) 2 react components, 1 store, client only, controller setup /client_side_hello_world_shared_store_controller Type in one component changes the other component
     Failure/Error: background { visit url }

     Capybara::Poltergeist::DeadClient:
       PhantomJS client died while processing {"id":"3d24856d-407d-4175-8646-9fc1db792bff","name":"visit","args":["http://127.0.0.1:63041/client_side_hello_world_shared_store_controller",30]}
     Shared Example Group: "React Component Shared Store" called from ./spec/features/integration_spec.rb:184
     # ./spec/features/integration_spec.rb:148:in `block (2 levels) in <top (required)>'

  2) 2 react components, 1 store, server side, defer /server_side_hello_world_shared_store_defer Type in one component changes the other component
     Failure/Error: background { visit url }

     Capybara::Poltergeist::DeadClient:
       PhantomJS client died while processing {"id":"f94da957-ffcd-4022-853a-7d0d2c61caa0","name":"visit","args":["http://127.0.0.1:63041/server_side_hello_world_shared_store_defer",30]}
     Shared Example Group: "React Component Shared Store" called from ./spec/features/integration_spec.rb:196
     # ./spec/features/integration_spec.rb:148:in `block (2 levels) in <top (required)>'

Need to figure these out.

@justin808
Copy link
Member

@martyphee Please address these in the PR description and the final commit. #411 (comment)

@justin808
Copy link
Member

@martyphee Here's a security reference on the XSS topic: https://rorsecurity.info/cross-site-scripting-xss-rails

@justin808
Copy link
Member

@martyphee Any new info on this one?

@justin808
Copy link
Member

@martyphee How's this one going?

@justin808
Copy link
Member

@martyphee Any more details on this one?

@justin808
Copy link
Member

@martyphee Are you still interested in this fix?

data: redux_store_data) do
# redux_store_data
"var #{redux_store_data[:store_name].tr('-', '_')} = #{Yajl.dump(redux_store_data[:props])};".html_safe
end
Copy link

Choose a reason for hiding this comment

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

Generating a JavaScript string from Ruby is a pretty bad idea in my experience. For one thing the generated identifier is in the global namespace, but it’s also potentially very fragile.

My personal preference for getting values from Ruby into JS is to provide them as data- attributes on some element, then pull them out with JS… but that seems to be pretty much what you were doing already. Is there some reason why you wanted to use a <script> tag rather than a <div>?

Copy link

@lededje lededje Oct 25, 2016

Choose a reason for hiding this comment

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

Global namespace shouldn't be a problem. Attempt to make it as unique as possible though. Prepend it with a bunch of underscores or something similar. I'd advise against using a data attribute though: You might accidentally include an ambiguous ampersand which is not allowed.

Copy link
Contributor

@squadette squadette Nov 6, 2016

Choose a reason for hiding this comment

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

@kapowaz a possible reason behind this is that initial props can be quite big, e.g. 100Kb. It is really awkward and unreadable to cram it to HTML attribute, given that every quote is replaced with &quot;.

@martyphee
Copy link
Contributor Author

@justin808 Yes, we still want to do this to try speeding up the page rendering. I'll work on it this week.

@justin808
Copy link
Member

@martyphee How's this one going?

@martyphee
Copy link
Contributor Author

I started to do some work on it last night. I'll try to get thru it
tonight. In Edinburgh right now.

On Tue, Nov 1, 2016 at 5:26 AM, Justin Gordon [email protected]
wrote:

@martyphee https://github.com/martyphee How's this one going?


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#411 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAuEtlWQ1lWUoRiPB2n_jpSelo3RI_B-ks5q5s2TgaJpZM4IXHeO
.

style: nil,
data: options.data) do
props = Yajl.dump(options.props.is_a?(String) ? JSON.parse(options.props) : options.props)
"var #{options.dom_id.tr('-', '_')} = #{props};".html_safe
Copy link
Contributor

Choose a reason for hiding this comment

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

I would suggest not playing games with global Javascript variables but instead use

<script type="application/json" id="...">{ "json": "encoded-data" }</script>

Also, it would really help development if you use human-readable JSON in non-development environment, a-la:

  def json_safe_and_pretty(something)
    if Rails.env.development?
      json_escape(JSON.pretty_generate(something.as_json))
    else
      json_escape(something.to_json)
    end
  end

Copy link
Contributor

@squadette squadette Nov 6, 2016

Choose a reason for hiding this comment

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

It also looks like JSON <script> tag also solves the Turbolinks issue, because there is no need to execute any <script> tag, you just have to replace el.getAttribute() with (IIRC) .textContent. Also, no eval()!

@martyphee
Copy link
Contributor Author

I haven't had much time to look at this yet.

@justin808
Copy link
Member

Hi @martyphee Any new developments on this one?

@justin808
Copy link
Member

@martyphee Is this one worth doing? @squadette any interest in working on this one?

@justin808
Copy link
Member

See #660.

@justin808
Copy link
Member

Deferring to #660.

@justin808 justin808 closed this Feb 2, 2017
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.

6 participants