-
-
Notifications
You must be signed in to change notification settings - Fork 26.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(react-scripts): allow PUBLIC_URL in develoment mode (#7259)
Co-authored-by: Eric Clemmons <eric@smarterspam.com> Co-authored-by: Alex Guerra <alex@heyimalex.com> Co-authored-by: Kelly <kelly.milligan@gmail.com> Co-authored-by: Eric Clemmons <eric@smarterspam.com> Co-authored-by: Alex Guerra <alex@heyimalex.com> Co-authored-by: Kelly <kelly.milligan@gmail.com>
- Loading branch information
1 parent
3190e4f
commit 1cbc6f7
Showing
14 changed files
with
290 additions
and
75 deletions.
There are no files selected for viewing
This file contains 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 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 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 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: '/' }, | ||
{ 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 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 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 |
---|---|---|
|
@@ -7,9 +7,11 @@ | |
|
||
'use strict'; | ||
|
||
module.exports = function createNoopServiceWorkerMiddleware() { | ||
const path = require('path'); | ||
|
||
module.exports = function createNoopServiceWorkerMiddleware(servedPath) { | ||
This comment has been minimized.
Sorry, something went wrong. |
||
return function noopServiceWorkerMiddleware(req, res, next) { | ||
if (req.url === '/service-worker.js') { | ||
if (req.url === path.join(servedPath, 'service-worker.js')) { | ||
This comment has been minimized.
Sorry, something went wrong.
anton-bot
|
||
res.setHeader('Content-Type', 'text/javascript'); | ||
res.send( | ||
`// This service worker file is effectively a 'no-op' that will reset any | ||
|
This file contains 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 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 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.
the paramter for path can't be 'undefined'