Skip to content

Conversation

@NGMarmaduke
Copy link

This enables additional code to be added to the Exec JS context when initialised.

Our app is consuming assets generated via Sprockets and Webpack, this option allows the Webpacked assets to injected along side the Sprockets assets for use in the ExecJS runtime.

This enables additional code to be added to the Exec JS context when initialised.

Our app is consuming assets generated via Sprockets and Webpack, this option allows the Webpacked assets to injected along side the Sprockets assets for use in the ExecJS runtime.
@NGMarmaduke NGMarmaduke force-pushed the nm/feature/sprockets-code-param branch from 36c3432 to e082973 Compare September 27, 2016 12:26
@rmosolgo
Copy link
Member

Very nice! For user code, does this option go in server_render_options, for example:

config.react.server_renderer_options = {
  code: File.read(webpack_bundle),
  # ...
}

?

@NGMarmaduke
Copy link
Author

It could certainly be set via the server_render_options in application.rb, for our app we already use a custom render to enable i18n:

class JsRenderer < SprocketsRenderer
  def initialize(options = {})
    options[:code] = webpack_assets
    super(options)
  end

  def before_render(component_name, props, prerender_options)
    super + set_i18n_js
  end

  def after_render(component_name, props, prerender_options)
    super + 'alt.recycle();'
  end

  private

  def set_i18n_js
    [i81n_js, moment_js].join
  end

  def i81n_js
    "I18n.locale = '#{ I18n.locale }';"
  end

  def moment_js
    "moment.locale(I18n.locale);"
  end

  def webpack_assets
    webpack_manifest.javascript_assets.map do |filename|
      webpack_manifest.find_asset(filename)
    end.join
  end

  def webpack_manifest
    WebpackAssets.manifest
  end
end

@rmosolgo
Copy link
Member

rmosolgo commented Oct 4, 2016

Very cool, thanks for sharing your use case!!

@rmosolgo rmosolgo merged commit 59bde30 into reactjs:master Oct 4, 2016
@rmosolgo
Copy link
Member

rmosolgo commented Oct 7, 2016

Just shipped in 1.9.0!

@NGMarmaduke NGMarmaduke deleted the nm/feature/sprockets-code-param branch October 7, 2016 11:26
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

Successfully merging this pull request may close these issues.

2 participants