-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Use CDN for chunks as well #1084
Comments
Are this issue and #1080 the same ones? |
Yes, sounds similar. Lets fix it 👍 |
So this one looks a bit tricky because webpack seems to trying to load the assets asynchronously and assuming the url to be the current document url. If we supply full public path into the webpack build then Rails will try to add the path with it's A workaround is to include following in a file where chunks are declared: // app/javascript/helpers/public-path.js
const removeOuterSlashes = string =>
string.replace(/^\/*/, '').replace(/\/*$/, '')
__webpack_public_path__ = `${removeOuterSlashes(process.env.WEBPACKER_ASSET_HOST)}/packs` // whatever the public output path is // packs/application.js
import '../helpers/public-path'
//... rest of the imports |
@gauravtiwari in order for this to work do I need to try with master branch? ( so #1107 is included) |
Yes please although it should work with 3.0.2 as well 👍 |
@gauravtiwari I am getting a pretty weird behaviour now. p.s. also I made it I have
as a first line in my pack file. |
@TheRusskiy Have you setup CDN for rails? # production.rb
config.action_controller.asset_host = 'http://example.com' |
@gauravtiwari yes :-) |
Does this chunk file: https://www.mysite.com/packs/0-2ae1b2669d9cdd1c9988.chunk.js if (process.env.NODE_ENV === "production") {
require("common/helpers/publicPath")
} has this import at the top? If you have multiple packs you would need to include it in all packs. Or you can make a pack just for public path and put at the top, somewhere in the //packs/public-path.js
if (process.env.NODE_ENV === "production") {
require("common/helpers/publicPath")
} <head>
<%= javascript_pack_tag 'public-path' %>
</head> |
@gauravtiwari |
Webpack should be doing all of this for us already.
Which is very hacky but gets the job done. |
@TheRusskiy Yes, setting like this will apply CDN at compile time and not runtime, which means you might get duplicate CDN URLs (one from Rails and one from webpack compile). const environment = require("./environment")
const { config } = require("@rails/webpacker")
const host = "https://cdn.mysite.com" // process.env.ASSET_HOST
environment.config.publicPath = `${host}/${config.public_output_path}/`
module.exports = environment.toWebpackConfig() |
isn't
just override to
? I don't see |
You don't want to mutate original config object but instead environment specific webpack config. config.publicPath = `${host}/packs/` this mutates internal config object, not the webpack config object. This changes production specific option for webpack: environment.config.publicPath = `${host}/${config.public_output_path}/` |
Not sure if this is related by since upgrading to 3.2.0 image assets that are within the manifest.json are not being added with the proper CDN url attached with it. I can only assume it is related to #1107 Edit: Noticed this issue was posted before 3.2.0 was released might make sense to turn this into it's own issue. |
We've tried updating // config/webpack/production.js
const { assetHost } require('@rails/webpacker')
if (process.env.WEBPACKER_ASSET_HOST) {
environment.config.set('output.publicPath', assetHost.publicPathWithHost)
} We then upgraded to 3.2.0, which removed // config/webpack/production.js
const { config } = require('@rails/webpacker')
if (process.env.WEBPACKER_ASSET_HOST) {
const publicPath = `${process.env.WEBPACKER_ASSET_HOST}/${config.public_output_path}/`
environment.config.set('output.publicPath', publicPath)
} Not sure what the exact difference between It's also been mentioned that this would result in duplication, not sure what is meant exactly but this. I also noticed that in 3.0.2 the file loader used to include the |
Changing Change from: const output = {
path: resolve("public", settings.public_output_path),
publicPath: formatPublicPath(env.ASSET_HOST, settings.public_output_path)
}; To: const output = {
path: resolve("public", settings.public_output_path),
publicPath: formatPublicPath(env.WEBPACKER_ASSET_HOST, settings.public_output_path)
}; |
New split chunks API New file loader option for adding static extensions Basic node modules compilation apart from app code CDN support - #1084 Perhaps someone can help with this one?
Please try the latest RC, this is now fixed. |
Right now
asset_host
setting seems to only affect the main JS file.The main JS pack file is loading from CDN correctly:
https://cdn.mysite.com/packs/admins-fb39652d413e925cd086.js
but then it requests a chunk and it ignores the CDN setting:
https://www.mysite.com/packs/0-d21df208925975fc5ed1.chunk.js
The text was updated successfully, but these errors were encountered: