-
-
Notifications
You must be signed in to change notification settings - Fork 27k
feat(react-scripts): allow PUBLIC_URL in develoment mode #7259
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
andriijas
merged 1 commit into
facebook:master
from
unrevised6419:public-url-in-development
Feb 2, 2020
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
packages/react-dev-utils/__tests__/getPublicUrlOrPath.test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
/** | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const getPublicUrlOrPath = require('../getPublicUrlOrPath'); | ||
|
||
const tests = [ | ||
// DEVELOPMENT with homepage | ||
{ dev: true, homepage: '/', expect: '/' }, | ||
{ dev: true, homepage: '/test', expect: '/test/' }, | ||
{ dev: true, homepage: '/test/', expect: '/test/' }, | ||
{ dev: true, homepage: './', expect: '/' }, | ||
unrevised6419 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ dev: true, homepage: '../', expect: '/' }, | ||
{ dev: true, homepage: '../test', expect: '/' }, | ||
{ dev: true, homepage: './test/path', expect: '/' }, | ||
{ dev: true, homepage: 'https://create-react-app.dev/', expect: '/' }, | ||
{ | ||
dev: true, | ||
homepage: 'https://create-react-app.dev/test', | ||
expect: '/test/', | ||
}, | ||
// DEVELOPMENT with publicURL | ||
{ dev: true, publicUrl: '/', expect: '/' }, | ||
{ dev: true, publicUrl: '/test', expect: '/test/' }, | ||
{ dev: true, publicUrl: '/test/', expect: '/test/' }, | ||
{ dev: true, publicUrl: './', expect: '/' }, | ||
{ dev: true, publicUrl: '../', expect: '/' }, | ||
{ dev: true, publicUrl: '../test', expect: '/' }, | ||
{ dev: true, publicUrl: './test/path', expect: '/' }, | ||
{ dev: true, publicUrl: 'https://create-react-app.dev/', expect: '/' }, | ||
{ | ||
dev: true, | ||
publicUrl: 'https://create-react-app.dev/test', | ||
expect: '/test/', | ||
}, | ||
// DEVELOPMENT with publicURL and homepage | ||
{ dev: true, publicUrl: '/', homepage: '/test', expect: '/' }, | ||
{ dev: true, publicUrl: '/test', homepage: '/path', expect: '/test/' }, | ||
{ dev: true, publicUrl: '/test/', homepage: '/test/path', expect: '/test/' }, | ||
{ dev: true, publicUrl: './', homepage: '/test', expect: '/' }, | ||
{ dev: true, publicUrl: '../', homepage: '/test', expect: '/' }, | ||
{ dev: true, publicUrl: '../test', homepage: '/test', expect: '/' }, | ||
{ dev: true, publicUrl: './test/path', homepage: '/test', expect: '/' }, | ||
{ | ||
dev: true, | ||
publicUrl: 'https://create-react-app.dev/', | ||
homepage: '/test', | ||
expect: '/', | ||
}, | ||
{ | ||
dev: true, | ||
publicUrl: 'https://create-react-app.dev/test', | ||
homepage: '/path', | ||
expect: '/test/', | ||
}, | ||
|
||
// PRODUCTION with homepage | ||
{ dev: false, homepage: '/', expect: '/' }, | ||
{ dev: false, homepage: '/test', expect: '/test/' }, | ||
{ dev: false, homepage: '/test/', expect: '/test/' }, | ||
{ dev: false, homepage: './', expect: './' }, | ||
{ dev: false, homepage: '../', expect: '../' }, | ||
{ dev: false, homepage: '../test', expect: '../test/' }, | ||
{ dev: false, homepage: './test/path', expect: './test/path/' }, | ||
{ dev: false, homepage: 'https://create-react-app.dev/', expect: '/' }, | ||
{ | ||
dev: false, | ||
homepage: 'https://create-react-app.dev/test', | ||
expect: '/test/', | ||
}, | ||
// PRODUCTION with publicUrl | ||
{ dev: false, publicUrl: '/', expect: '/' }, | ||
{ dev: false, publicUrl: '/test', expect: '/test/' }, | ||
{ dev: false, publicUrl: '/test/', expect: '/test/' }, | ||
{ dev: false, publicUrl: './', expect: './' }, | ||
{ dev: false, publicUrl: '../', expect: '../' }, | ||
{ dev: false, publicUrl: '../test', expect: '../test/' }, | ||
{ dev: false, publicUrl: './test/path', expect: './test/path/' }, | ||
{ | ||
dev: false, | ||
publicUrl: 'https://create-react-app.dev/', | ||
expect: 'https://create-react-app.dev/', | ||
}, | ||
{ | ||
dev: false, | ||
publicUrl: 'https://create-react-app.dev/test', | ||
expect: 'https://create-react-app.dev/test/', | ||
}, | ||
// PRODUCTION with publicUrl and homepage | ||
{ dev: false, publicUrl: '/', homepage: '/test', expect: '/' }, | ||
{ dev: false, publicUrl: '/test', homepage: '/path', expect: '/test/' }, | ||
{ dev: false, publicUrl: '/test/', homepage: '/test/path', expect: '/test/' }, | ||
{ dev: false, publicUrl: './', homepage: '/test', expect: './' }, | ||
{ dev: false, publicUrl: '../', homepage: '/test', expect: '../' }, | ||
{ dev: false, publicUrl: '../test', homepage: '/test', expect: '../test/' }, | ||
{ | ||
dev: false, | ||
publicUrl: './test/path', | ||
homepage: '/test', | ||
expect: './test/path/', | ||
}, | ||
{ | ||
dev: false, | ||
publicUrl: 'https://create-react-app.dev/', | ||
homepage: '/test', | ||
expect: 'https://create-react-app.dev/', | ||
}, | ||
{ | ||
dev: false, | ||
publicUrl: 'https://create-react-app.dev/test', | ||
homepage: '/path', | ||
expect: 'https://create-react-app.dev/test/', | ||
}, | ||
]; | ||
|
||
describe('getPublicUrlOrPath', () => { | ||
tests.forEach(t => | ||
it(JSON.stringify(t), () => { | ||
const actual = getPublicUrlOrPath(t.dev, t.homepage, t.publicUrl); | ||
expect(actual).toBe(t.expect); | ||
}) | ||
); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/** | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const { URL } = require('url'); | ||
|
||
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 use `path` module functions for operations | ||
* | ||
* @param {boolean} isEnvDevelopment | ||
* @param {(string|undefined)} homepage a valid url or pathname | ||
* @param {(string|undefined)} envPublicUrl a valid url or pathname | ||
* @returns {string} | ||
*/ | ||
function getPublicUrlOrPath(isEnvDevelopment, homepage, envPublicUrl) { | ||
const stubDomain = 'https://create-react-app.dev'; | ||
|
||
if (envPublicUrl) { | ||
// ensure last slash exists | ||
envPublicUrl = envPublicUrl.endsWith('/') | ||
? envPublicUrl | ||
: 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; | ||
} | ||
|
||
if (homepage) { | ||
// strip last slash if exists | ||
homepage = homepage.endsWith('/') ? homepage : 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 '/'; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/** | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
'use strict'; | ||
|
||
const path = require('path'); | ||
|
||
module.exports = function createRedirectServedPathMiddleware(servedPath) { | ||
// remove end slash so user can land on `/test` instead of `/test/` | ||
servedPath = servedPath.slice(0, -1); | ||
return function redirectServedPathMiddleware(req, res, next) { | ||
if ( | ||
servedPath === '' || | ||
req.url === servedPath || | ||
req.url.startsWith(servedPath) | ||
) { | ||
next(); | ||
} else { | ||
const newPath = path.join(servedPath, req.path !== '/' ? req.path : ''); | ||
res.redirect(newPath); | ||
} | ||
}; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.