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

%PUBLIC_URL% in development #6280

Closed

Conversation

ericclemmons
Copy link
Contributor

@ericclemmons ericclemmons commented Jan 26, 2019

In response to #6135 (and possibly replacing #4158), I'm attempting to enable support for homepage in development as well.

This way, development matches production URLs, which is especially necessary when using CRA as part of a larger app, or behind a proxy.

I've been able to record a video of this working:

https://gfycat.com/AcclaimedEssentialAlabamamapturtle

I could use some help on how I could further test this, & to what degree.

@ericclemmons
Copy link
Contributor Author

Aha! Looks like I can duplicate the tests in create-react-app/test/fixtures/relative-paths to include this example.

@iamandrewluca
Copy link
Contributor

@ericclemmons with this PR on npm run start this will open localhost:3000/homepage instead of only localhost:3000 ?
Thanks!

@ericclemmons
Copy link
Contributor Author

ericclemmons commented Feb 6, 2019 via email

@wmpay
Copy link

wmpay commented Feb 8, 2019

Need any help getting this merged? I currently need this for my development environment. In production we have multiple apps being served from different folders, and I would like my development environment to mimic that. e.g. localhost/admin

@nhagen
Copy link

nhagen commented Feb 8, 2019

We've been waiting for this for a long time. Also happy to help if there are specific to-dos left to work on. Do we know whats causing the build failure?

@ericclemmons
Copy link
Contributor Author

ericclemmons commented Feb 8, 2019 via email

@ianschmitz ianschmitz added this to the 2.1.x milestone Feb 9, 2019
@raix
Copy link
Contributor

raix commented Feb 28, 2019

@ericclemmons can you try merging in master both to resolve the merge conflict and rerun the build (if it's npm hickups maybe add - travis_retry yarn)


And yes, we would also like to see this feature in development as it enables us to have near 1:1 mapping with our production environment when developing

@raix
Copy link
Contributor

raix commented Mar 5, 2019

@ericclemmons let me know if you need any help on this pull request

@mhornbacher
Copy link

@raix, @ericclemmons is there anything I can do to help to make this feature a reality?

Is the entire bottleneck the failing tests?

@iansu iansu added this to the 3.1 milestone Apr 22, 2019
@raix
Copy link
Contributor

raix commented Apr 29, 2019

@ericclemmons do you need help addressing review comments? or are you waiting for more reviewers to join?

@ArnaudBarre
Copy link

ArnaudBarre commented Apr 29, 2019

I tested these changes (on top of v3 release). It works well but it's tricky to get the assets in public works well. If your homepage (or PUBLIC_URL in my case is /project-1), you need to have your favicon.ico in public/project-1/ for dev, but public/ for build.

It's good if you want to start multiple dev instance with different assets. But not sure it's the main use case and it requires some cp/mv before yarn build.

May be it can be simplify if we differentiate publicPath from publicUrl in dev env, but I'm not sure and I didn't succeed.

Also some comment could be updated in webpack.config.js:
L. 175: // We use "/" in development.
L. 556:

// In production, it will be an empty string unless you specify "homepage"
// in `package.json`, in which case it will be the pathname of that URL.
// In development, this will be an empty string.

@raix
Copy link
Contributor

raix commented Apr 29, 2019

@ArnaudBarre I see, so we actually have two scenarios to cater for:

  1. The development server is behind a proxy that maps paths to different services
    • The assets should be served from root eg. "publicPath=/"
    • Urls / require / HMR should use the "public" url
  2. The development server is using a proxy to access the backend and rest of the system
    • The development should only serve assets from the "publicPath=publicUrl" - everything else should be passed on using the proxy config
    • Urls / require / HMR should use the the "public" url

We might actually ending up using both since we some times do development in containers and other times use the development server to proxy to the cluster.

@@ -68,7 +68,7 @@ module.exports = function(proxy, allowedHost) {
hot: true,
// It is important to tell WebpackDevServer to use the same "root" path
// as we specified in the config. In development, we always serve from /.
publicPath: '/',
publicPath: paths.servedPath.slice(0, -1),

Choose a reason for hiding this comment

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

I have been messing around with this locally and I have found that
further down in this file needs to be updated as well, something similar to:

    historyApiFallback: {
      // Paths with dots should still use the history fallback.
      // See https://github.com/facebook/create-react-app/issues/387.
      disableDotRule: true,
      index: paths.servedPath,
    },

otherwise when you go to pages other than the home page with react router setup and refresh it will fail to load files.

@tbhag
Copy link

tbhag commented May 9, 2019

I suppose it may be complex to handle the two use cases (in near term) but perhaps this PR is a good first step to get out? We've forked this PR at my company because we really needed to set PUBLIC_URL in development. Currently stress testing but seems to be working just fine. It's kinda like the same as it was with a little more control in dev

@brunodahora
Copy link

Hey guys, we need exact this to be able to use CRA in our projects.
How is the status?
Can we help with something?

@margobaxter
Copy link

I would like to be able to use this in my project. What's the status? Can we get this merged?

@schemburkar
Copy link

schemburkar commented May 20, 2019 via email

@jiahuay
Copy link

jiahuay commented May 20, 2019

I also want to be able to use this in my project. What is blocking this pull request from being merged? Anything we can help?

@omninonsense
Copy link

omninonsense commented May 22, 2019

Another thing that might be useful to make the /sockjs-node path configurable, this will require changing a few other things (react-dev-utils comes to mind, and possibly others). The comment in react-dev-utils/webpackHotDevClient.js:66 says it's hard-coded in webpack-dev-server, but that is no longer the case (according to this).

I guess the most convenient way would be through an environment variable, (WEBPACK_DEV_SOCKJS_PATH, or just SOCKJS_PATH), since it probably doesn't need to be inferred from homepage.

The reason I am suggesting this is because in more complex development setups that mimic production it's not always possible to access to /sockjs-node (like using a local kubernetes cluster with an ingress controller).

Edit: Although, this suggestion might be better suited for another PR.

@menocomp
Copy link

menocomp commented Jun 4, 2019

Is that fork going to be merged one day? I can not wait for getting public-url to work in development to mimic the same behaviour as in production.

@brettwinterflood
Copy link

Let's close the loop and merge!

@jannikkeye
Copy link

As there have already been two approvals for reviews, is this now going to be merged any time soon? I encountered this issue now in a serverless environment using now.sh. Local development is hindered by CRA not supporting PUBLIC_PATH under development environment.

@tim-phillips
Copy link

Listen, anyone can submit a review, so I would take reviews from members outside the team and especially non-contributors with a grain of salt.

With that said, there are still unresolved comments, one of which mentions that this PR breaks react-router.

@ericclemmons
Copy link
Contributor Author

ericclemmons commented Jun 19, 2019 via email

@raix
Copy link
Contributor

raix commented Jun 20, 2019

@ericclemmons If you or the mainainers of CRA don't have the time to fix the comments - we could create a new pull-request and get this ready - but I would like to have a "go" from one of the maintainers that they actually want this change in.. cc @iansu

It's fair enough if CRA project is only meant as a helping hand to newcommers to get started with react and not for advanced usage/developers/containers etc.


@tim-phillips I agree that the code owners should review - but the react router issue is a react router issue - in the sense that it's solvable by the developer? eg. you could set the base url to match what ever your PUBLIC_URL is.

@iamandrewluca
Copy link
Contributor

iamandrewluca commented Jun 20, 2019

I always use this code to initialize router history

const history = createBrowserHistory({
  basename: process.env.PUBLIC_URL
})

@iamandrewluca
Copy link
Contributor

iamandrewluca commented Jun 21, 2019

I have the time to take care of this. I made a new PR with all this changes, and review changes.
#7259

@ianschmitz
Copy link
Contributor

Closing in favor of #7259

@ianschmitz ianschmitz closed this Jun 22, 2019
@lock lock bot locked and limited conversation to collaborators Jun 27, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.