Allow full URL with hostname/port for PUBLIC_URL in development
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)
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.
Thanks, @seanvgarner!
https://github.com/facebook/create-react-app/pull/7259
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);
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_URLtohttps://mydevproxy.domain.com/app1then/app1is prepended to development resource paths, but the browser still openshttp://localhost:3000Maybe that's a different issue, but something feels off here. :)For now I'm just adding
BROWSER=browser.jswith: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).
#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.
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}`
}
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.
Just a quick follow-up with a solution, maybe it will help someone.
- use
npx patch-package react-scripts/react-dev-utilsto add the changes below to thenode_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) {
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.