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

refactor(v2): move scripts/stylesheets injection to server side #2081

Merged
merged 1 commit into from
Dec 4, 2019
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
22 changes: 0 additions & 22 deletions packages/docusaurus/src/client/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import React, {useEffect, useState} from 'react';

import Head from '@docusaurus/Head';
import routes from '@generated/routes';
import siteConfig from '@generated/docusaurus.config';
import renderRoutes from '@docusaurus/renderRoutes';
Expand All @@ -17,7 +16,6 @@ import PendingNavigation from './PendingNavigation';
import './client-lifecycles-dispatcher';

function App() {
const {stylesheets, scripts} = siteConfig;
const [isClient, setIsClient] = useState(false);

useEffect(() => {
Expand All @@ -26,26 +24,6 @@ function App() {

return (
<DocusaurusContext.Provider value={{siteConfig, isClient}}>
{(stylesheets || scripts) && (
<Head>
{stylesheets &&
stylesheets.map(source =>
typeof source === 'string' ? (
<link rel="stylesheet" key={source} href={source} />
) : (
<link rel="stylesheet" key={source.href} {...source} />
),
)}
{scripts &&
scripts.map(source =>
typeof source === 'string' ? (
<script type="text/javascript" src={source} key={source} />
) : (
<script type="text/javascript" key={source.src} {...source} />
),
)}
</Head>
)}
<PendingNavigation routes={routes}>
{renderRoutes(routes)}
</PendingNavigation>
Expand Down
31 changes: 30 additions & 1 deletion packages/docusaurus/src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,43 @@ export async function load(siteDir: string): Promise<Props> {
);
const userTheme = path.resolve(siteDir, THEME_PATH);
const alias = loadThemeAlias([fallbackTheme, ...pluginThemes, userTheme]);
// Make a fake plugin to resolve aliased theme components.

// Make a fake plugin to resolve aliased theme components && inject scripts/stylesheets
const {stylesheets = [], scripts = []} = siteConfig;
plugins.push({
name: 'docusaurus-bootstrap-plugin',
configureWebpack: () => ({
resolve: {
alias,
},
}),
injectHtmlTags: () => {
const stylesheetsTags = stylesheets.map(source =>
typeof source === 'string'
? `<link rel="stylesheet" href="${source}">`
Copy link
Contributor

Choose a reason for hiding this comment

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

This API comes in handy now 😂

: {
tagName: 'link',
attributes: {
rel: 'stylesheet',
...source,
},
},
);
const scriptsTags = scripts.map(source =>
typeof source === 'string'
? `<script type="text/javascript" src="${source}"></script>`
: {
tagName: 'script',
attributes: {
type: 'text/javascript',
...source,
},
},
);
return {
headTags: [...stylesheetsTags, ...scriptsTags],
};
},
});

// Load client modules.
Expand Down