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

Applications served from cdn on different host #647

Closed
SeeThruHead opened this issue Sep 14, 2016 · 12 comments
Closed

Applications served from cdn on different host #647

SeeThruHead opened this issue Sep 14, 2016 · 12 comments
Milestone

Comments

@SeeThruHead
Copy link

I'm in the process of converting our companies internal boilerplate into something that uses create-react-app.

We load all of our applications into html rendered by rails views in our application. Js/css/etc built by webpack is served via cdn.

In our boilerplate we look in the env for CDN_URL and set that as publicpath. This cdn url is provided in local dev by a .env file (loaded via dotenv module) and during deploys by the build server.

In trying to keep with 12 factor app methodology we don't commit these urls to the codebase.

Seems like publicpath is only currently modifiable by editing package.json.

Would it be possible to use something like REACT_APP_STATIC_URL (or any other, better name) in order to construct the publicpath for webpack?

@drobannx
Copy link

I have a similar issue. I need the resulting index.html to have its link and script tags point to a url that has a CDN url prefixed to the fingerprinted assets. Right now I see no way of achieving this without abusing the homepage property in package.json by doing //https://pathToCDN.com

Would it be possible to do something like CDN_URL=https://pathToCDN.com npm run build? Or is there another route in order to achieve this that I am missing?

@gaearon gaearon added this to the 1.0.0 milestone Sep 15, 2016
@SeeThruHead
Copy link
Author

I'm in the process of forking create-react-app to handle a few things specific to my companies needs, which includes this feature. (I've implemented it as process.env.HOMPAGE) currently. I could make a pr for this if it's coherent with create-react-app's scope and philosophy.

@skyuplam
Copy link

I need the same stuff as well. I have made a small change to the build script and added publicPath to the package.js file as a workaround. 😈

Ref: Webpack Config - -publicpath

@gaearon
Copy link
Contributor

gaearon commented Sep 21, 2016

I’m open to merging support for CDN_URL env variable.

@SeeThruHead
Copy link
Author

Working on it

SeeThruHead added a commit to SeeThruHead/create-react-app that referenced this issue Sep 21, 2016
closes facebook#647

Previously we only allowed settings the pathname via the "homepage" key
in package.json

Allows user to configure the full base URL via a $CDN_URL env
variable

Development has the same behaviour.
@mcallistersean
Copy link

I solved a similar problem where I needed absolute urls in development builds.
Without modifying the webpack config used by create-react-app it is possible to inject the publicPath at runtime by using a global variable.
I added a file (publicPath.js) with this content to the src folder:

// eslint-disable-next-line
__webpack_public_path__ = 'http://127.0.0.1:3000/';

and imported it right at the top of the index.js.

import "./publicPath";

import React from 'react';
....

It's kind of important that it is imported before any other file and just defining the variable insidethe index.js also does not work because all imports are hoisted to the top of the file.

@gaearon
Copy link
Contributor

gaearon commented Sep 30, 2016

Interesting. We don’t officially support this so it might break in the future.

@mcallistersean
Copy link

Yep, it's a hack and probably should not even work.
Related to this issue: would you be interested in a pull request to the hot reloading client that connects to the server that actually served the hot reloading script instead of window.location.hostname etc. as is currently the case.
Something like what I did here: e142fbd

That would make this project even easier to integrate into non-js backends.

@gaearon
Copy link
Contributor

gaearon commented Sep 30, 2016

Right now I’m wary of increasing surface area for bugs. I don’t want to make this as complicated as WDS client. I still plan to make changes to it, so I intend to keep it scoped to features CRA supports, and serving JS from a different host or port is not supported right now.

@jjasonclark
Copy link

This seems a bit related to PR #937. Adding for context.

@Timer
Copy link
Contributor

Timer commented Feb 11, 2017

Hi there! react-scripts v0.9.0 was just released which adds support for specifying a CDN url. You may read how to configure it here.

Please test it and don't hesitate to reach out if this doesn't solve your specific use case!

@gaearon
Copy link
Contributor

gaearon commented Feb 11, 2017

Going to close this.

@gaearon gaearon closed this as completed Feb 11, 2017
@lock lock bot locked and limited conversation to collaborators Jan 22, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants