-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Server index improvements #838
Conversation
Codecov Report
@@ Coverage Diff @@
## master #838 +/- ##
=========================================
+ Coverage 91.46% 91.67% +0.2%
=========================================
Files 68 68
Lines 3573 3687 +114
=========================================
+ Hits 3268 3380 +112
- Misses 305 307 +2
Continue to review full report at Codecov.
|
@@ -66,7 +66,7 @@ class Bundler extends EventEmitter { | |||
const publicURL = | |||
options.publicUrl || | |||
options.publicURL || | |||
'/' + Path.basename(options.outDir || 'dist'); | |||
'/' + Path.basename(options.outDir || ''); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why set it to options.outDir
? What does that provide? Why are these two related? I notice that this is the part that is not changed. I think we should change it, as I described in #932. To simply /
by default.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn’t want to change it too much, just fix a bug and prevent future issues regarding the default. Although i do agree with you, would be nice to have Devon’s opinion on these changes
src/Server.js
Outdated
!req.url.startsWith(bundler.options.publicURL) || | ||
req.url === bundler.options.publicURL | ||
) { | ||
// If the URL doesn't start with the public path or is the public path, send the main HTML bundle |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
function respond() {
if (bundler.errored) {
return send500();
} else if (!req.url.startsWith(bundler.options.publicURL)) {
return sendIndex();
} else if (req.url === bundler.options.publicURL) {
return sendIndex();
} else {
// Otherwise, serve the file from the dist folder
req.url = req.url.slice(bundler.options.publicURL.length);
return serve(req, res, send404);
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don’t think this is a better solution as it just adds an extra unnecessary if
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
More readable for me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do agree with @mightyiam that the multiline ||
is pretty hard to read / reason about, but I also agree with @DeMoorJasper that the split if
s looks redundant and unnecessary since both if
s do exactly the same thing.
I would write it like this, which solves both of those problems, and get's rid of some of the confusing negation, making it much easier to understand and reason about:
function respond() {
// If an error occurred in the bundler, send a 500 error
if (bundler.errored) return send500();
// If the URL start with the public path, serve the file from the dist folder
if (req.url.startsWith(bundler.options.publicURL)) {
// Check that its not the publicURL itself
if (req.url !== bundler.options.publicURL) {
req.url = req.url.slice(bundler.options.publicURL.length);
return serve(req, res, send404);
}
}
// Otherwise send the main HTML bundle
return sendIndex();
}
Explanation
This converts the "or" into a negated "and" ([using this fancy Boolean algebra rule](https://en.wikipedia.org/wiki/De_Morgan%27s_laws)), then flips the resulting if-else to clean up the negation, and then splits the resulting `&&` into separate nested `if`s (thank you Webstorm for doing all that in the click of a button 😛). Oh, and I also got rid of all the `else`s, since they're redundant with the `return`sIMO, my version is slightly cleaner and easier to read / reason about, but tbh it doesn't really matter so I'll leave it up to you.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried improving readability, also changed how index works slightly (so it's more like how other webserver work, only giving index if it's in publicURL or a sub-directory of publicURL. Would also prevent issues with the future naming conventions inside parcel and any issue with parcel not giving an asset but the index page)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
This one seems to break going to e.g. |
Also changing the default public url to |
publicURL = /this/is/a/directory
than it will give index on any path up to/this/is/a/directory/
once you go above that for example/this/is/a/directory/index.js
it'll return an asset or 404 depending on it's existance)