Skip to content
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

Fix lazy reload for urls without index #1110

Merged

Conversation

ang-zeyu
Copy link
Contributor

@ang-zeyu ang-zeyu commented Mar 9, 2020

What is the purpose of this pull request? (put "X" next to an item, remove the rest)

• [x] Bug fix

Fixes #1109

What is the rationale for this request?
To normalize url requests that reference index.html implcitly ( eg hostname/userGuide, hostname/userGuide/ ), such that a page build is triggered ( if needed ).

What changes did you make? (Give an overview)
Added some code to normalize urls that reference index.html implicitly

Provide some example code that this change will affect:

// Maps hostname/userGuide/  to hostname/userGuide/index
normalizedUrl = (hasNoExtension || hasEndingSlash)
  ? path.posix.join(normalizedUrl, 'index')
  : normalizedUrl;

Is there anything you'd like reviewers to focus on?
na

Testing instructions:
lazy reload for urls that reference index.html implicitly ( eg. userGuide/, userGuide ) should now correctly trigger a page build ( if necessary )

Proposed commit message: (wrap lines at 72 characters)
Fix lazy reload for urls without index

Lazy live reloading uses the file extension of the request url to
determine whether the request is for a html page, and not from a
dynamic include.
Hence, requests for index.html with urls that do not end with
index.html fail to be detected.

Let’s add extra processing to detect such requests.

@ang-zeyu ang-zeyu force-pushed the fix-lazy-reload-no-index-requests branch from 329c861 to 411b33c Compare March 9, 2020 15:41
@ang-zeyu ang-zeyu requested a review from nbriannl March 15, 2020 10:54
@ang-zeyu ang-zeyu force-pushed the fix-lazy-reload-no-index-requests branch 2 times, most recently from cc1062a to 3c106d8 Compare March 21, 2020 13:58
@ang-zeyu
Copy link
Contributor Author

@nbriannl could I get your review here?

Aim of the changes here being to normalize host/userGuide/ or host/userGuide ( if host/userGuide is not a raw file ) to host/userGuide/index ( intentionally extensionless )

@ang-zeyu ang-zeyu force-pushed the fix-lazy-reload-no-index-requests branch from 3c106d8 to 5a69c47 Compare March 22, 2020 06:44
@nbriannl
Copy link
Contributor

nbriannl commented Mar 22, 2020

If i understand correctly,
With this change, when markbind serve and change docs\userGuide\index.md, I will trigger a live reload on both http://127.0.0.1:8080/userGuide/ and http://127.0.0.1:8080/userGuide/index.html

Before this change, only http://127.0.0.1:8080/userGuide/index.html, would live reload?

@ang-zeyu
Copy link
Contributor Author

If i understand correctly,
With this change, when markbind serve and change docs\userGuide\index.md, I will trigger a live reload on both http://127.0.0.1:8080/userGuide/ and http://127.0.0.1:8080/userGuide/index.html

Before this change, only http://127.0.0.1:8080/userGuide/index.html, would live reload?

Yup

Copy link
Contributor

@nbriannl nbriannl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, works when I tested. But I think someone more experienced with this should approve

@ang-zeyu ang-zeyu force-pushed the fix-lazy-reload-no-index-requests branch from 5a69c47 to f041c7e Compare March 23, 2020 09:54
Copy link
Contributor

@marvinchin marvinchin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for looking at this :) just a suggestion from me

index.js Outdated
return;
}

let normalizedUrl = req.url.replace(config.baseUrl, '');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't use normalizedUrl until L208. Could we define normalizedUrl closer to it's usage?

Also, I generally prefer using const and naming intermediate variables explicitly. I find that this makes it easier to follow what each step is doing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch 😅, thanks! Updated some of the comments as well

@ang-zeyu ang-zeyu force-pushed the fix-lazy-reload-no-index-requests branch from f041c7e to bd21d45 Compare March 26, 2020 12:51
index.js Outdated
let normalizedUrl = req.url.replace(config.baseUrl, '');

// Map 'hostname/userGuide/' and 'hostname/userGuide' to hostname/userGuide/index.
normalizedUrl = (hasNoExtension || hasEndingSlash)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we do:

const urlWithoutBase = ...
const urlWithIndex = ...
const urlWithoutExtension = ...

I think we should avoid mutation where possible. In this case, we can break this transformation down into individual independent steps with variables names that make each step self-explanatory.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logical flow is fairly straightforward here, but in other more complex cases variable mutation can lead to some hard to trace errors. I have a strong preference for const over let whenever possible to avoid this, but I'm happy to discuss if you (or anyone else) has a different preference :)

index.js Outdated
@@ -179,21 +179,42 @@ program

if (onePagePath) {
const lazyReloadMiddleware = function (req, res, next) {
const isHtmlFile = req.url.endsWith('.html');
const requestExtension = path.posix.extname(req.url);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think just extension or urlExtension is fine? 😛 req itself is an object that has a bunch of other stuff in it, so requestExtension doesn't feel very correct to me.

Lazy live reloading uses the file extension of the request url to
determine whether the request is for a html page, and not from a
dynamic include.
Hence, requests for index.html with urls that do not end with
index.html fail to be detected.

Let’s add extra processing to detect such requests.
@ang-zeyu ang-zeyu force-pushed the fix-lazy-reload-no-index-requests branch from bd21d45 to b33c554 Compare March 26, 2020 15:56
@ang-zeyu
Copy link
Contributor Author

Updated the variable naming

Copy link
Contributor

@marvinchin marvinchin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM :)

@marvinchin marvinchin added this to the v2.12.1 milestone Mar 26, 2020
@marvinchin marvinchin merged commit d3a07aa into MarkBind:master Mar 26, 2020
Tejas2805 added a commit to Tejas2805/markbind that referenced this pull request Mar 27, 2020
* 'master' of https://github.com/MarkBind/markbind:
  Fix lazy reload for urls without index (MarkBind#1110)
  Changes remaining references from filterTags to tags (MarkBind#1149)
Tejas2805 added a commit to Tejas2805/markbind that referenced this pull request Mar 28, 2020
…bind into remove-fixed-bugs

* 'remove-fixed-bugs' of https://github.com/Tejas2805/markbind:
  Docs: Add contributing.md (MarkBind#1139)
  Show pointer and use underline dashed for click trigger (MarkBind#1111)
  Support variables to be defined as by JSON (MarkBind#1117)
  Allow an array for specifying page src or glob (MarkBind#1118)
  Code blocks: Implement inline markdown support for heading (MarkBind#1137)
  Fix lazy reload for urls without index (MarkBind#1110)
  Changes remaining references from filterTags to tags (MarkBind#1149)
marvinchin pushed a commit that referenced this pull request Apr 10, 2020
Lazy live reloading uses the file extension of the request url to
determine whether the request is for a html page, and not from a
dynamic include.
Hence, requests for index.html with urls that do not end with
index.html fail to be detected.

Let’s add extra processing to detect such requests.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Lazy live preview not working for links ending with /
3 participants