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

Add optional --fallback CLI option #167

Merged
merged 2 commits into from
Dec 2, 2018
Merged

Add optional --fallback CLI option #167

merged 2 commits into from
Dec 2, 2018

Conversation

alex996
Copy link
Contributor

@alex996 alex996 commented Nov 28, 2018

Closes #164 and #166. Introduces an opt-in -f or --fallback command-line flag, which allows to serve up the start page as a fallback, when the requested route cannot be resolved. This is useful when developing a single-page app that handles routing client-side with HTML5 history API.

Currently, non-resolvable routes such as /about or /blog/some-post are falling through serve-static, which can't match them to a file, and thus returns a 404. When -f flag is on, reload will detect these routes, and respond with index.html (or custom start page when -s is provided). The front-end (e.g. react using react-router-dom) can then take over the rendering based on the URL path.

As a stretch goal we could also serve up blog.html in response to /blog if the said file happens to exist.

Inspired by --history-api-fallback in webpack-dev-server.

@alallier
Copy link
Owner

@alex996 thanks for the PR. Will get a review on this soon for you

@alex996
Copy link
Contributor Author

alex996 commented Nov 30, 2018

@alallier Sounds good. I can also resolve #166 while I'm at it. It shouldn't be more than a simple fs.existsSync check. Awaiting your feedback until then.

@alallier
Copy link
Owner

alallier commented Dec 2, 2018

@alex996 that sounds good let's do that. The rest of the PR looks good

@alex996
Copy link
Contributor Author

alex996 commented Dec 2, 2018

@alallier Done. Both issues would be closed by this PR.

@alallier
Copy link
Owner

alallier commented Dec 2, 2018

@alex996 It seems you wrapped the changes for #166 in the shouldFallback case. It should do that regardless of the new option,

@alex996
Copy link
Contributor Author

alex996 commented Dec 2, 2018

@alallier Ok, I see your point. Amended the commit.

@alallier
Copy link
Owner

alallier commented Dec 2, 2018

Looks good, merging. Thank you @alex996

@alallier alallier merged commit 045314e into alallier:master Dec 2, 2018
@AlexanderSopov
Copy link

Hi! Awesome fix and thank you. How can I update to this version?

Best regards <3

@alallier
Copy link
Owner

alallier commented Dec 7, 2018

@AlexanderSopov it is published to npm, you can just follow the normal npm update procedures

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.

Is there a way to force the index.html to always be what is reloaded in that case of a single page app?
3 participants