From 861f58e0090b0838609038753ea26bc2d8cb8eb6 Mon Sep 17 00:00:00 2001 From: Anmol Sethi Date: Thu, 4 Feb 2021 16:44:53 -0500 Subject: [PATCH] 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 #8623 Also see https://github.com/cdr/code-server/issues/2565 --- .../react-dev-utils/getPublicUrlOrPath.js | 33 +++++++++---------- 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/packages/react-dev-utils/getPublicUrlOrPath.js b/packages/react-dev-utils/getPublicUrlOrPath.js index dbb7cda5aa2..ae979f31ccd 100644 --- a/packages/react-dev-utils/getPublicUrlOrPath.js +++ b/packages/react-dev-utils/getPublicUrlOrPath.js @@ -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 @@ -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, homepage 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 '/';