homepage in package.json breaks react-create-app if it contains a fragment identifier (#foo) #8514
+5 −0
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.
This is a subtle bug which causes create-react-app to strip a character from the homepage URL and then crash. It was discovered by @cyrille-arundo, @geevb and myself.
Steps to reproduce
npx create-react-app my-app
package.json
'shomepage
:https://domain.com/page#hello"
npm start
Pay attention to the two URLs
http://localhost:3000/pag
andhttp://10.10.10.86:3000/pag
. They're both missing the last character (e
).Why is this happening?
There's a lot of technical acrobatics going on when create-react-app tries to determine its URL.
I've added a failing test case to demonstrate how
packages/react-dev-utils/getPublicUrlOrPath
returns a path without a trailing slash (/) if the URL contains a fragment identifier. This becomes a problem later because becausepackages/react-scripts/scripts/start.js
assumes the URL always contain a trailing slash. Instead it ends up stripping the last character from the URL.Feedback
Also, debugging this was unnecessarily painful. Lines like the following, with three-way ternaries, make me reconsider my career options.
This is taken from
packages/react-dev-utils/getPublicUrlOrPath
.