-
-
Notifications
You must be signed in to change notification settings - Fork 8.4k
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
feat(v2): baseUrl config issues: show help message if css/js can't load #3621
Conversation
Deploy preview for docusaurus-2 ready! Built with commit e053743 |
22c56e5
to
8dadd7a
Compare
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.
see comments
@@ -27,6 +41,7 @@ function App(): JSX.Element { | |||
return ( | |||
<DocusaurusContext.Provider | |||
value={{siteConfig, siteMetadata, globalData, isClient}}> | |||
<CSSLoadingWarningMessage isClient /> |
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.
<CSSLoadingWarningMessage isClient /> | |
<CSSLoadingWarningMessage isClient={isClient} /> |
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.
Hi,
The pre-commit hook changed this from isClient={isClient}
to isClient
You site CSS did not load properly. Your baseUrl setting is probably | ||
bad.{' '} | ||
</span> | ||
{isClient && <span>Maybe try baseUrl = {location.pathname}</span>} |
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.
Here it's not totally correct.
If we are on:
/myAppBaseUrl/
, we should recommend the user to trybaseUrl: '/myAppBaseUrl/'
/myAppBaseUrl/docs/myDoc
, we should recommend the user to trybaseUrl: '/myAppBaseUrl/'
What you currently have is, if you are on:
/myAppBaseUrl/
, you recommend the user to trybaseUrl: '/myAppBaseUrl/'
/myAppBaseUrl/docs/myDoc
, you recommend the user to trybaseUrl: '/myAppBaseUrl/docs/myDoc'
=> not a good recommendation
Not sure what's the best way to implement this, but my initial suggestion was rather to only display this message on the homepage, to avoid increasing all the site's page size with this additional, warning, and as it's the URL users will tend to browse first when trying their new deployment.
In any case we should only recommend valid baseUrl settings to the user, or he'll be more confused than ever.
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.
So should I remove the suggestion for now?
8dadd7a
to
4dead0f
Compare
I've made my first attempt at detecting if the CSS has not loaded. As per the description in the issue, I hide the message on the first remotely-loaded CSS file.
|
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.
Nice try, but you can see for yourself in the deploy preview that it does not work 🤪 :
https://deploy-preview-3621--docusaurus-2.netlify.app/classic/
For sites with correct baseUrl config, this warning should normally have no visible effect, it should only affect sites with bad baseUrl configs.
Read my suggestions here to understand better what should be done: #3584 (comment)
} catch (e) {} | ||
} | ||
return false; | ||
} |
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.
you shouldn't do that, because it won't trigger any react re-render on css load.
Instead do something like I suggested:
import "./hideCSSLoadingWarning.css"
.css-loading-warning {
display: none;
}
If the CSS of the site loads, the message will automatically hide, so it will remain visible only if css does not load.
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.
Oh now I get it!
Sorry I think I had misunderstood it the first time.
I'll try this, thank you!
<div style={{border: 'solid red thick', padding: '16px'}}> | ||
<span> | ||
You site CSS did not load properly. Your baseUrl setting is probably | ||
bad. |
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.
Still think we need a suggestion here. if we can tell the user the exact value he should use it's better.
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.
Agreed!
Maybe I can do something like location.pathname.split('/')[0]
?
You had also mentioned "only display this message on the homepage".
So should I maybe move this to somewhere else?
4dead0f
to
80491d8
Compare
As the browser won't load the js/css bundles in case of bad baseUrl configuration, we'll likely need a better way to display this suggestion. The JS should be inlined in the static HTML files, so that it can always execute even if the css/js bundles don't load. Here import "./hideCSSLoadingWarningMessage.css";
function CSSLoadingWarningMessage() {
const location = useLocation();
if (location.pathname === baseUrl) {
return (
<div style={{ border: "solid red thick" }}>
<div>CSS didn't load</div>
<div>Mabe you should try baseurl=<span className="css-didnt-load-baseurl-suggestion"/></div>
<script>
document.querySelector("span").text = window.location.pathname
</script>
</div>
);
}
return null;
} Here is pseudo code for inspiration, not sure it works |
80491d8
to
c25ca7d
Compare
Thank you! I could not get the baseUrl using So as of now, the warning div works (it is hidden whenever css loads successfully) and the suggestion message works too. |
I'd really like the script to be encapsuled in the warning component, so that the feature is easier to maintain (not splitted across the codebase) + also I don't want this script to be appended to all HTML pages, only the homepage. What didn't work exactly? You should not use And yes, if baseurl is wrongly configured, it will be a bad baseurl. But on SSR, we also use that bad baseurl to render the pages on a static location, so Please try my suggestions, commit and ping me so that I can inspect the code and Netlify deployment. Only once we agree the approach does not work, it's worth exploring another approach, but I think it should work. If you need help I can commit on your branch |
@@ -35,6 +35,10 @@ module.exports = ` | |||
<script type="text/javascript" src="<%= it.baseUrl %><%= script %>"></script> | |||
<% }); %> | |||
<%~ it.postBodyTags %> | |||
<script> | |||
document.getElementById('css-didnt-load-baseurl-suggestion').innerHTML = |
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.
We'd rather be "defensive" and consider the element might not be found, in such case we should not have any console error
Also I prefer to not have this script in the template as it splits the feature in multiple parts, affect the output of all the pages (not just the homepage) and will lead to a bad suggestion if user tries to browse another non-home page of the broken site (ie, suggestion /baseUrl/docs/mydoc if user is browsing /docs/mydoc)
c25ca7d
to
04e91f6
Compare
Thank you @slorber I also tried using |
thanks for your work :) |
Thank you for your guidance on this @slorber! 😄 |
Motivation
Aims to implement #3584
Have you read the Contributing Guidelines on pull requests?
Yes
Test Plan
WIP