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

feat(v2): add baseUrlIssueBanner configuration to disable banner #3802

Merged
merged 2 commits into from
Nov 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/docusaurus-types/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export type ReportingSeverity = 'ignore' | 'log' | 'warn' | 'error' | 'throw';

export interface DocusaurusConfig {
baseUrl: string;
baseUrlIssueBanner: boolean;
favicon: string;
tagline?: string;
title: string;
Expand Down
4 changes: 2 additions & 2 deletions packages/docusaurus/src/client/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import siteMetadata from '@generated/site-metadata';
import renderRoutes from './exports/renderRoutes';
import DocusaurusContext from './exports/context';
import PendingNavigation from './PendingNavigation';
import BaseUrlSuggestionWarning from './baseUrlSuggestionWarning/BaseUrlSuggestionWarning';
import BaseUrlIssueBanner from './baseUrlIssueBanner/BaseUrlIssueBanner';

import './client-lifecycles-dispatcher';

Expand All @@ -28,7 +28,7 @@ function App(): JSX.Element {
return (
<DocusaurusContext.Provider
value={{siteConfig, siteMetadata, globalData, isClient}}>
<BaseUrlSuggestionWarning />
<BaseUrlIssueBanner />
<PendingNavigation routes={routes}>
{renderRoutes(routes)}
</PendingNavigation>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import React from 'react';
import {useLocation} from 'react-router-dom';

import Head from '../exports/Head';

import styles from './styles.module.css';
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';

// We want to help the users with a bad baseUrl configuration (very common error)
// Help message is inlined, and hides if the external CSS is able to load successfully
// Note: it might create false positives (ie network failures): not a big deal
// Note: we only inline this for the homepage to avoid polluting all the site's pages
// See https://github.com/facebook/docusaurus/pull/3621
export default function BaseUrlIssueBanner() {
const {
siteConfig: {baseUrl, baseUrlIssueBanner},
} = useDocusaurusContext();
const {pathname} = useLocation();

// returns true for the homepage during SRR
const isHomePage = pathname === baseUrl;

const shouldRender = baseUrlIssueBanner && isHomePage;

if (!shouldRender) {
return null;
}

const BaseUrlIssueBannerContainerId = 'base-url-issue-banner-container';

return (
<>
<Head>
<script>
{`
document.addEventListener('DOMContentLoaded', function () {
var baseUrlSuggestion = document.getElementById(
'${BaseUrlIssueBannerContainerId}',
);
if (baseUrlSuggestion) {
var actualHomePagePath = window.location.pathname;
var suggestedBaseUrl = actualHomePagePath.substr(-1) === '/'
? actualHomePagePath
: actualHomePagePath + '/';
baseUrlSuggestion.innerHTML = suggestedBaseUrl;
}
});
`}
</script>
</Head>
<div
className={styles.baseUrlIssueBanner}
style={{
border: 'solid red thick',
backgroundColor: '#ffe6b3',
margin: 20,
padding: 20,
fontSize: 20,
}}>
<p
style={{
fontWeight: 'bold',
fontSize: 30,
}}>
Your Docusaurus site did not load properly.
</p>
<p>
A very common reason is a wrong site{' '}
<a
href="https://v2.docusaurus.io/docs/docusaurus.config.js/#baseurl"
style={{fontWeight: 'bold'}}>
baseUrl configuration
</a>
.
</p>
<p>
Current configured baseUrl ={' '}
<span style={{fontWeight: 'bold', color: 'red'}}>{baseUrl}</span>{' '}
{baseUrl === '/' ? ' (default value)' : ''}
</p>
<p>
We suggest trying baseUrl ={' '}
<span
style={{fontWeight: 'bold', color: 'green'}}
id={BaseUrlIssueBannerContainerId}
/>{' '}
</p>
</div>
</>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

.baseUrlIssueBanner {
slorber marked this conversation as resolved.
Show resolved Hide resolved
display: none;

Choose a reason for hiding this comment

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

Curious as to why this was hidden from view? Getting this error and it never showed which caused me to come here :/

}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ See https://v2.docusaurus.io/docs/docusaurus.config.js/#customfields"
exports[`loadConfig website with valid siteConfig 1`] = `
Object {
"baseUrl": "/",
"baseUrlIssueBanner": true,
"customFields": Object {},
"favicon": "img/docusaurus.ico",
"noIndex": false,
Expand Down
3 changes: 3 additions & 0 deletions packages/docusaurus/src/server/configValidation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const DEFAULT_CONFIG: Pick<
| 'themeConfig'
| 'titleDelimiter'
| 'noIndex'
| 'baseUrlIssueBanner'
> = {
onBrokenLinks: 'throw',
onBrokenMarkdownLinks: 'warn',
Expand All @@ -37,6 +38,7 @@ export const DEFAULT_CONFIG: Pick<
themeConfig: {},
titleDelimiter: '|',
noIndex: false,
baseUrlIssueBanner: true,
};

const PluginSchema = Joi.alternatives().try(
Expand All @@ -61,6 +63,7 @@ const ConfigSchema = Joi.object({
.required()
.regex(new RegExp('/$', 'm'))
.message('{{#label}} must be a string with a trailing `/`'),
baseUrlIssueBanner: Joi.boolean().default(DEFAULT_CONFIG.baseUrlIssueBanner),
favicon: Joi.string().required(),
title: Joi.string().required(),
url: URISchema.required(),
Expand Down
24 changes: 24 additions & 0 deletions website/docs/api/docusaurus.config.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -424,3 +424,27 @@ module.exports = {
titleDelimiter: '🦖', // Defaults to `|`
};
```

### `baseUrlIssueBanner`

- Type: `boolean`

When enabled, will show a banner in case your site can't load its CSS or JavaScript files, which is a very common issue, often related to a wrong `baseUrl` in site config.

Example:

```js title="docusaurus.config.js"
module.exports = {
baseUrlIssueBanner: true, // Defaults to `true`
};
```

![baseUrlIssueBanner](/img/baseUrlIssueBanner.png)

:::caution

This banner need to inline CSS / JS.

If you have a strict [Content Security Policy](https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP), you should rather disable it.

:::
1 change: 1 addition & 0 deletions website/docusaurus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ module.exports = {
organizationName: 'facebook',
projectName: 'docusaurus',
baseUrl,
baseUrlIssueBanner: true,
lex111 marked this conversation as resolved.
Show resolved Hide resolved
url: 'https://v2.docusaurus.io',
onBrokenLinks: isVersioningDisabled ? 'warn' : 'throw',
onBrokenMarkdownLinks: 'warn',
Expand Down
Binary file added website/static/img/baseUrlIssueBanner.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.