Skip to content

Commit

Permalink
fix: enable live reloading title of doc (facebook#1507)
Browse files Browse the repository at this point in the history
* fix: enable live reloading title of doc

* fix: removing unnecessary reload statements

* fix: only title change should trigger a live reload

* fix: adding more properties that triggers a live reload

* fix: refactoring
  • Loading branch information
tusharf5 committed May 27, 2019
1 parent 7e2b3c7 commit bd20c7c
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions packages/docusaurus-1.x/lib/server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ function execute(port, host) {
MetadataBlog = require(join('..', 'core', 'MetadataBlog.js'));
}

function reloadTranslations() {
removeModuleAndChildrenFromCache('./translation.js');
}

function requestFile(url, res, notFoundCallback) {
request.get(url, (error, response, body) => {
if (!error) {
Expand Down Expand Up @@ -116,18 +120,31 @@ function execute(port, host) {

app.get(routing.docs(siteConfig), (req, res, next) => {
const url = decodeURI(req.path.toString().replace(siteConfig.baseUrl, ''));
const metadata =
Metadata[
Object.keys(Metadata).find(id => Metadata[id].permalink === url)
];
const metakey = Object.keys(Metadata).find(
id => Metadata[id].permalink === url,
);

let metadata = Metadata[metakey];

const file = docs.getFile(metadata);
if (!file) {
next();
return;
}
const {rawContent, metadata: rawMetadata} = metadataUtils.extractMetadata(
file,
);

// if any of the followings is changed, reload the metadata
const reloadTriggers = ['sidebar_label', 'hide_title', 'title'];
if (reloadTriggers.some(key => metadata[key] !== rawMetadata[key])) {
reloadMetadata();
extractTranslations();
reloadTranslations();
metadata = Metadata[metakey];
}

reloadSiteConfig();
const rawContent = metadataUtils.extractMetadata(file).rawContent;
removeModuleAndChildrenFromCache('../core/DocsLayout.js');
const mdToHtml = metadataUtils.mdToHtml(Metadata, siteConfig);
res.send(docs.getMarkup(rawContent, mdToHtml, metadata, siteConfig));
Expand Down

0 comments on commit bd20c7c

Please sign in to comment.