create-react-app icon indicating copy to clipboard operation
create-react-app copied to clipboard

Allow full URL with hostname/port for PUBLIC_URL in development

Open seanvgarner opened this issue 6 years ago • 10 comments

Is your proposal related to a problem?

Recently there was a change so that PUBLIC_URL is no longer ignored in development mode. This is a great change, however it seems limited in that PUBLIC_URL in development can only be a path while environments outside of development can be full URLs with hostname/port, etc.

Describe the solution you'd like

Please allow for PUBLIC_URL to specify a URL in development, not just a path.

Describe alternatives you've considered

Only other solution we have to work with is change our entire server-side setup (quite involved) or utilize patch-package (ew)

seanvgarner avatar May 13 '20 21:05 seanvgarner

Having the same issue, trying to develop with a micro-frontends based approach, but can only run a built version of the sub-application since the PUBLIC_URL won't affect the relative URLs for images.

In https://github.com/facebook/create-react-app/blob/master/packages/react-dev-utils/getPublicUrlOrPath.js#L41, it's not entirely clear to me why this takes the pathname rather than just using the provided PUBLIC_URL.

mmartinsky avatar May 15 '20 16:05 mmartinsky

Thanks, @seanvgarner!

SergeyVolynkin avatar May 15 '20 23:05 SergeyVolynkin

https://github.com/facebook/create-react-app/pull/7259

SergeyVolynkin avatar May 15 '20 23:05 SergeyVolynkin

Having read through #7259 it's still not clear to me that the full PUBLIC_URL is being used fully in development mode. If I set PUBLIC_URL to https://mydevproxy.domain.com/app1 then /app1 is prepended to development resource paths, but the browser still opens http://localhost:3000 Maybe that's a different issue, but something feels off here. :)

For now I'm just adding BROWSER=browser.js with:

const open = require('open');
open(process.env.PUBLIC_URL);

Codelica avatar Oct 09 '20 19:10 Codelica

Having read through #7259 it's still not clear to me that the full PUBLIC_URL is being used fully in development mode. If I set PUBLIC_URL to https://mydevproxy.domain.com/app1 then /app1 is prepended to development resource paths, but the browser still opens http://localhost:3000 Maybe that's a different issue, but something feels off here. :)

For now I'm just adding BROWSER=browser.js with:

const open = require('open');
open(process.env.PUBLIC_URL);

It doesn't. This also causes issues when using code splitting with React.lazy and you're loading in the generated development bundle from a different url than the localhost the bundle is hosted on (it will try to fetch chunks from the hostname you're loading the bundle from instead of the hostname). I also created an issue (#8993) regarding this behavior, in this case PUBLIC_URL is also ignored (although the specified path inside PUBLIC_URL is used).

Anwardo avatar Oct 12 '20 09:10 Anwardo

#7259

Having read through #7259, but really strongly need to use the FULL PUBLIC_PATH(with domain & port) in development. Because the project is running as a sub-app by another single-spa project.Some async code like React.Lazy will not get the real resource url(e.g. ./js/vender.js will finally get 'the.main.app.dev.domain:devPort/js/vender.js' but expect 'the.sub-app.dev.domain:devPort/js/vender.js').Pls solve it.

uihoh0 avatar Mar 09 '21 04:03 uihoh0

Find out a solution provided by webpack itself. For the document, pls check out this page. Solution code:

// add these lines in your entry(e.g. 'src/index.js')
if(process.env.NODE_ENV === 'development'){
    const PUBLIC_URL = process.env.PUBLIC_URL || '/'
    // @ts-ignore for ts lines
    // eslint-disable-next-line camelcase for eslint checker
    __webpack_public_path__ = `http://localhost:3000${PUBLIC_URL}`
}

uihoh0 avatar Apr 13 '21 14:04 uihoh0

Find out a solution provided by webpack itself. For the document, pls check out this page. Solution code:

// add these lines in your entry(e.g. 'src/index.js')
if(process.env.NODE_ENV === 'development'){
    const PUBLIC_URL = process.env.PUBLIC_URL || '/'
    // @ts-ignore for ts lines
    // eslint-disable-next-line camelcase for eslint checker
    __webpack_public_path__ = `http://localhost:3000${PUBLIC_URL}`
}

Couldn't get past the '__webpack_public_path__ ' is undefined error with this solution.

I did manage to fix it in a different way using the rewire package and creating a separate file to initialize the development build using the following setup:

const rewire = require('rewire');
const defaults = rewire('react-scripts/scripts/start.js');
let configFactory = defaults.__get__('configFactory');

defaults.__set__('configFactory', (env) => {
  const config = configFactory(env);
  config.output.publicPath = 'http://localhost:3000'+config.output.publicPath;
  return config;  
})

Then initialize the development build using node your-file.js or add it to your package.json so you can initialize it using npm.

Anwardo avatar Apr 14 '21 07:04 Anwardo

Just a quick follow-up with a solution, maybe it will help someone.

  1. use npx patch-package react-scripts/react-dev-utils to add the changes below to the node_modules/react-scripts/node_modules/react-dev-utils/getPublicUrlOrPath.js
diff --git a/node_modules/react-scripts/node_modules/react-dev-utils/getPublicUrlOrPath.js b/node_modules/react-scripts/node_modules/react-dev-utils/getPublicUrlOrPath.js
index dbb7cda..4e248c8 100644
--- a/node_modules/react-scripts/node_modules/react-dev-utils/getPublicUrlOrPath.js
+++ b/node_modules/react-scripts/node_modules/react-dev-utils/getPublicUrlOrPath.js
@@ -35,13 +35,12 @@ function getPublicUrlOrPath(isEnvDevelopment, homepage, envPublicUrl) {
     // `stubDomain` is ignored if `envPublicUrl` contains a domain
     const validPublicUrl = new URL(envPublicUrl, stubDomain);
 
-    return isEnvDevelopment
-      ? envPublicUrl.startsWith('.')
-        ? '/'
-        : validPublicUrl.pathname
-      : // Some apps do not use client-side routing with pushState.
-        // For these, "homepage" can be set to "." to enable relative asset paths.
-        envPublicUrl;
+    /**
+     *  In our web-app, during the development we serve the web-app HTML/CSS/JS assets via server,
+     *  thus web-app should know the full URL to its assets not only in prod, but also in development
+     *  e.g. during local development the server runs on `http://localhost:8080/` and the web-app runs on `http://localhost:3000` -> `http://localhost:8080/` serves the `index.html` to the browser
+     */
+    return envPublicUrl
   }
 
   if (homepage) {

SergeyVolynkin avatar May 11 '21 12:05 SergeyVolynkin

Commenting here to note that the public URL as fed to webpack is also used as the base for the URLs generated for hot reloading. A version of the snippet in https://github.com/facebook/create-react-app/issues/9001#issuecomment-818778659 was a sufficient shim in my case, thank you.

I've also opened the MR above to the documentation to note the limitations of the current environment variable approach.

bradjones1 avatar Jul 06 '21 19:07 bradjones1