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

Fix asset host duplication #335

Closed
wants to merge 10 commits into from
Closed

Fix asset host duplication #335

wants to merge 10 commits into from

Conversation

gauravtiwari
Copy link
Member

@gauravtiwari gauravtiwari commented May 6, 2017

Fixes #320 and other linked issues around asset host.

Since we are using Rails asset helper tag internally, CDN is applied automatically on all top level webpack assets, however the internal JS assets like images and fonts don't use CDN.

Previously in #224 a fix was introduced but it broke the expected behaviour - basically we were applying ASSET_HOST twice. This PR fixes this and only applies ASSET_HOST to internal file loader assets.

const { slashes, href } = new Url(host)
if (slashes) { return `${href}/${paths.entry}/` }
if (href) { return `//${href}/${paths.entry}/` }
return `/${paths.entry}/`
Copy link
Member

Choose a reason for hiding this comment

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

This is a bit confusing flow. Can we replace with a case statement instead? Not a big fan of multiple exit functions like this.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yepp, me don't like it either but seems we can't fully use switch as we are dependent on multiple params. I will break into two functions.


module.exports = {
test: /\.(jpg|jpeg|png|gif|svg|eot|ttf|woff|woff2)$/i,
use: [{
loader: 'file-loader',
options: {
publicPath,
publicPath: env.NODE_ENV === 'production' ? computeAssetPath(env.ASSET_HOST) : publicPath,
name: env.NODE_ENV === 'production' ? '[name]-[hash].[ext]' : '[name].[ext]'
Copy link
Member

Choose a reason for hiding this comment

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

Since both of these options differ on the same conditional, we should extract the conditional to a higher level. Maybe:

cont assetOptions = (env) => {
  if (env.NODE_ENV === 'production') {
    return { publicPath: computeAssetPath(env.ASSET_HOST), name: '[name]-[hash].[ext]' }
  } else {
    return { publicPath: publicPath, name: '[name].[ext]' }
  }
}

options: assetOptions(env)

@gauravtiwari
Copy link
Member Author

@dhh If you are happy we can merge this one 😄

// Make sure host has slashes
const ensureSlashes = ({ href, slashes }) => {
if (slashes) { return `${href}/${paths.entry}/` }
return `//${href}/${paths.entry}/`
Copy link
Member

Choose a reason for hiding this comment

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

This should be a regular if/else conditional, rather than a double exit function. Same with the other methods here.

Copy link
Member Author

@gauravtiwari gauravtiwari May 10, 2017

Choose a reason for hiding this comment

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

Yepp, done the same when you suggested the other day but it seems eslint doesn't like it. If an if block contains a return statement, the else block becomes unnecessary. (one of the JS things 😉 )

@dhh
Copy link
Member

dhh commented May 10, 2017 via email

@gauravtiwari
Copy link
Member Author

Screwed 😄 - 8fcaf18

@javan
Copy link
Contributor

javan commented May 10, 2017

Can you show what a compiled manifest.json looks like before and after this change? I've having a hard time following the flow through your new helper functions in config/loaders/core/assets.js.

@gauravtiwari
Copy link
Member Author

gauravtiwari commented May 10, 2017

@javan I am now confused too 😄 Hang on, checking...

@gauravtiwari
Copy link
Member Author

gauravtiwari commented May 10, 2017

Just checked, so this change is for internal assets linked within components that don't directly get referenced from manifest and it will be embedded inside source after compile -

screen shot 2017-05-10 at 20 28 54

Previously, I have done this for all, forgetting Rails helper tag already does this too so the CDN url was included twice per #320

@gauravtiwari
Copy link
Member Author

Slow poke @javan 😄

@javan
Copy link
Contributor

javan commented May 12, 2017

Rails already knows how to do this in Ruby. See compute_asset_host. How about we just use that instead of duplicating the same logic in JavaScript?

--- a/lib/tasks/webpacker/compile.rake
+++ b/lib/tasks/webpacker/compile.rake
@@ -6,7 +6,7 @@ namespace :webpacker do
   desc "Compile javascript packs using webpack for production with digests"
   task compile: ["webpacker:verify_install", :environment] do
     puts "Compiling webpacker assets <U+1F389>"
-    asset_host = Rails.application.config.action_controller.asset_host
+    asset_host = ApplicationController.helpers.compute_asset_host

Copy link
Member

@guilleiguaran guilleiguaran left a comment

Choose a reason for hiding this comment

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

+1 for @javan suggestion, let's try to use compute_asset_host helper for this

it's possible to use the helper without depending on an existing ApplicationController ??

@javan
Copy link
Contributor

javan commented May 14, 2017

Can use ActionController::Base if you want to avoid referencing ApplicationController: ActionController::Base.helpers.compute_asset_host

@guilleiguaran
Copy link
Member

@javan cool!! 👍

@gauravtiwari
Copy link
Member Author

@javan @guilleiguaran Yepp thought about using that but don't want to couple too much with Rails internals so the Gem works with other older Rails versions and standalone ruby apps (may be something that we can move towards?). There is some duplication but I guess it makes sense if we are thinking to support larger community. What do you think?

@catskull
Copy link

Anyone have an update on where this is at? This causing my JS assets to not work on staging/production. Is there a temp workaround?

@gauravtiwari
Copy link
Member Author

@gauravtiwari
Copy link
Member Author

or in your Gemfile

gem 'webpacker', github: 'gauravtiwari/webpacker', branch: 'fix-asset-host'

@catskull
Copy link

Thanks, using your configuration.js works!

@javan
Copy link
Contributor

javan commented May 18, 2017

Fixed + more in #397

@javan javan closed this in #397 May 18, 2017
@gauravtiwari gauravtiwari deleted the fix-asset-host branch May 18, 2017 15:59
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.

javascript_pack_tag seems to add path twice with asset host
5 participants