Skip to content

Commit

Permalink
Allow relative $PUBLIC_URL in dev mode
Browse files Browse the repository at this point in the history
  • Loading branch information
nhooyr committed Feb 4, 2021
1 parent 282c03f commit ad78cbe
Showing 1 changed file with 15 additions and 18 deletions.
33 changes: 15 additions & 18 deletions packages/react-dev-utils/getPublicUrlOrPath.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module.exports = getPublicUrlOrPath;
/**
* Returns a URL or a path with slash at the end
* In production can be URL, abolute path, relative path
* In development always will be an absolute path
* In development can be a relative or absolute path
* In development can use `path` module functions for operations
*
* @param {boolean} isEnvDevelopment
Expand All @@ -31,34 +31,31 @@ function getPublicUrlOrPath(isEnvDevelopment, homepage, envPublicUrl) {
? envPublicUrl
: envPublicUrl + '/';

// Some apps do not use client-side routing with pushState.
// For these, "$PUBLIC_URL" can be set to "." to enable relative asset paths.
if (envPublicUrl.startsWith(".")) {
return envPublicURL
}

// validate if `envPublicUrl` is a URL or path like
// `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;
return isEnvDevelopment ? validPublicUrl.pathname : envPublicUrl;
}

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

// Some apps do not use client-side routing with pushState.
// For these, "$PUBLIC_URL" can be set to "." to enable relative asset paths.
if (homepage.startsWith(".")) {
return homepage
}

// validate if `homepage` is a URL or path like and use just pathname
const validHomepagePathname = new URL(homepage, stubDomain).pathname;
return isEnvDevelopment
? homepage.startsWith('.')
? '/'
: validHomepagePathname
: // Some apps do not use client-side routing with pushState.
// For these, "homepage" can be set to "." to enable relative asset paths.
homepage.startsWith('.')
? homepage
: validHomepagePathname;
return isEnvDevelopment ? validHomepagePathname : homepage;
}

return '/';
Expand Down

0 comments on commit ad78cbe

Please sign in to comment.