Skip to content

Commit a5526c9

Browse files
committed
improve detection of Markdown files in SSR renderer so it doesn't utterly fail so much, and log errors to console instead of swallowing them
1 parent 1bbc846 commit a5526c9

File tree

3 files changed

+41
-18
lines changed

3 files changed

+41
-18
lines changed

packages/docsify-server-renderer/index.js

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,21 @@ export default class Renderer {
168168
try {
169169
if (isAbsolutePath(filePath)) {
170170
const res = await fetch(filePath);
171+
171172
if (!res.ok) {
172-
throw Error();
173+
this.lock = this.lock || 0;
174+
175+
if (++this.lock > 10) {
176+
this.lock = 0;
177+
return;
178+
}
179+
180+
const fileName = basename(filePath);
181+
const result = await this._loadFile(
182+
resolvePathname(`../${fileName}`, filePath)
183+
);
184+
185+
return result;
173186
}
174187

175188
content = await res.text();
@@ -181,18 +194,14 @@ export default class Renderer {
181194

182195
return content;
183196
} catch (e) {
184-
this.lock = this.lock || 0;
185-
if (++this.lock > 10) {
186-
this.lock = 0;
187-
return;
188-
}
189-
190-
const fileName = basename(filePath);
191-
const result = await this._loadFile(
192-
resolvePathname(`../${fileName}`, filePath)
197+
console.trace(
198+
`ERROR: Encountered an error loading file ${filePath}. See the error after this one for more details.`
193199
);
194200

195-
return result;
201+
// Don't fail on optional files, but still log the error for reference.
202+
if (['_sidebar.md', '_navbar.md'].some(f => filePath.includes(f)))
203+
console.error(e);
204+
else throw e;
196205
}
197206
}
198207
}

server.js

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import liveServer from 'live-server';
2+
import { qualifyURL } from './packages/docsify-server-renderer/src/utils';
23

34
const isSSR = !!process.env.SSR;
45
const middleware = [];
6+
const port = 3000;
57

68
main();
79

@@ -10,7 +12,7 @@ async function main() {
1012
// Using JSDom here because the server relies on a small subset of DOM APIs.
1113
// The URL used here serves no purpose other than to give JSDOM an HTTP
1214
// URL to operate under (it probably can be anything).
13-
initJSDOM('', { url: 'https://127.0.0.1:3000' });
15+
initJSDOM('', { url: 'http://127.0.0.1:' + port });
1416

1517
const { Renderer, getServerHTMLTemplate } = await import(
1618
'./packages/docsify-server-renderer/index'
@@ -21,7 +23,9 @@ async function main() {
2123
config: {
2224
name: 'docsify',
2325
repo: 'docsifyjs/docsify',
24-
basePath: 'https://docsify.js.org/',
26+
// Do not use URLs for SSR mode. Specify only an absolute or relative file path.
27+
// basePath: 'https://docsify.js.org/',
28+
basePath: '/docs', // TODO if not set while in SSR mode, code tries to operate on an undefined value. Set a default.
2529
loadNavbar: true,
2630
loadSidebar: true,
2731
subMaxLevel: 3,
@@ -35,16 +39,25 @@ async function main() {
3539
},
3640
});
3741

38-
middleware.push(function (req, res, next) {
39-
if (/\.(css|js)$/.test(req.url)) {
40-
return next();
42+
middleware.push(function(req, res, next) {
43+
const url = new URL(qualifyURL(req.url));
44+
45+
// Only handle markdown files or folders.
46+
if (/(\.md|\/[^.]*)$/.test(url.pathname)) {
47+
// ^ See the related getFileName() function.
48+
renderer.renderToString(req.url).then(html => res.end(html));
49+
return;
4150
}
42-
renderer.renderToString(req.url).then((html) => res.end(html));
51+
52+
// TODO there *must* be edge cases. Add an option to force certain files?
53+
console.log('Skipping markdown handling of file ' + req.url);
54+
55+
return next();
4356
});
4457
}
4558

4659
const params = {
47-
port: 3000,
60+
port,
4861
watch: ['lib', 'docs', 'themes'],
4962
middleware,
5063
};

src/core/router/util.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ export const getParentPath = cached(path => {
5858
});
5959

6060
export const cleanPath = cached(path => {
61+
// turn '////foo' into '/foo' and 'foo//bar' into 'foo/bar'
6162
return path.replace(/^\/+/, '/').replace(/([^:])\/{2,}/g, '$1/');
6263
});
6364

0 commit comments

Comments
 (0)