Skip to content

Commit 861f58e

Browse files
committed
Allow relative $PUBLIC_URL in dev mode
As long as client side routing isn't actively used, a relative $PUBLIC_URL should work correctly in both development and production. There's no reason it should be limited to production. Closes facebook#8623 Also see coder/code-server#2565
1 parent 282c03f commit 861f58e

File tree

1 file changed

+15
-18
lines changed

1 file changed

+15
-18
lines changed

packages/react-dev-utils/getPublicUrlOrPath.js

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ module.exports = getPublicUrlOrPath;
1414
/**
1515
* Returns a URL or a path with slash at the end
1616
* In production can be URL, abolute path, relative path
17-
* In development always will be an absolute path
17+
* In development can be a relative or absolute path
1818
* In development can use `path` module functions for operations
1919
*
2020
* @param {boolean} isEnvDevelopment
@@ -31,34 +31,31 @@ function getPublicUrlOrPath(isEnvDevelopment, homepage, envPublicUrl) {
3131
? envPublicUrl
3232
: envPublicUrl + '/';
3333

34+
// Some apps do not use client-side routing with pushState.
35+
// For these, "$PUBLIC_URL" can be set to "." to enable relative asset paths.
36+
if (envPublicUrl.startsWith(".")) {
37+
return envPublicUrl;
38+
}
39+
3440
// validate if `envPublicUrl` is a URL or path like
3541
// `stubDomain` is ignored if `envPublicUrl` contains a domain
3642
const validPublicUrl = new URL(envPublicUrl, stubDomain);
37-
38-
return isEnvDevelopment
39-
? envPublicUrl.startsWith('.')
40-
? '/'
41-
: validPublicUrl.pathname
42-
: // Some apps do not use client-side routing with pushState.
43-
// For these, "homepage" can be set to "." to enable relative asset paths.
44-
envPublicUrl;
43+
return isEnvDevelopment ? validPublicUrl.pathname : envPublicUrl;
4544
}
4645

4746
if (homepage) {
4847
// strip last slash if exists
4948
homepage = homepage.endsWith('/') ? homepage : homepage + '/';
5049

50+
// Some apps do not use client-side routing with pushState.
51+
// For these, homepage can be set to "." to enable relative asset paths.
52+
if (homepage.startsWith(".")) {
53+
return homepage;
54+
}
55+
5156
// validate if `homepage` is a URL or path like and use just pathname
5257
const validHomepagePathname = new URL(homepage, stubDomain).pathname;
53-
return isEnvDevelopment
54-
? homepage.startsWith('.')
55-
? '/'
56-
: validHomepagePathname
57-
: // Some apps do not use client-side routing with pushState.
58-
// For these, "homepage" can be set to "." to enable relative asset paths.
59-
homepage.startsWith('.')
60-
? homepage
61-
: validHomepagePathname;
58+
return isEnvDevelopment ? validHomepagePathname : homepage;
6259
}
6360

6461
return '/';

0 commit comments

Comments
 (0)