@@ -6,6 +6,7 @@ const Renderer = require('docsify-server-renderer')
6
6
const fs = require ( 'fs' )
7
7
const util = require ( '../util/index' )
8
8
const chalk = require ( 'chalk' )
9
+ const LRU = require ( 'lru-cache' )
9
10
10
11
var defaultConfig = {
11
12
template : `<!DOCTYPE html>
@@ -37,7 +38,7 @@ function loadConfig (config) {
37
38
module . exports = function ( path , configFile , port ) {
38
39
let config = defaultConfig
39
40
const pkg = util . pkg ( )
40
- let ctx = util . cwd ( '.' )
41
+ const ctx = util . cwd ( '.' )
41
42
42
43
path = path || './'
43
44
@@ -50,37 +51,51 @@ module.exports = function (path, configFile, port) {
50
51
const tpl = pkg . docsify . template
51
52
52
53
config = pkg . docsify
53
- config . template = ( tpl && util . exists ( util . resolve ( ctx , tpl ) ) )
54
- ? util . read ( tpl )
55
- : defaultConfig . template
54
+ config . template =
55
+ tpl && util . exists ( util . resolve ( ctx , tpl ) )
56
+ ? util . read ( tpl )
57
+ : defaultConfig . template
56
58
}
57
59
58
60
var renderer = new Renderer ( Object . assign ( defaultConfig , config ) )
59
61
var server = connect ( )
62
+ const cached = new LRU ( config . maxAge || 0 )
60
63
61
64
server . use ( serveStatic ( path ) )
62
- server . use ( function ( req , res ) {
65
+ server . use ( function ( req , res ) {
63
66
serveStatic ( path ) ( req , res , function ( ) {
64
- if ( / \. ( j p g | j p e g | g i f | p n g | s v g | i c o | m p 4 | w e b m | o g g | o g v | j s | c s s | m d ) (?: \? v = [ 0 - 9 . ] + ) ? $ / . test ( req . url ) ) {
67
+ if (
68
+ / \. ( j p g | j p e g | g i f | p n g | s v g | i c o | m p 4 | w e b m | o g g | o g v | j s | c s s | m d ) (?: \? v = [ 0 - 9 . ] + ) ? $ / . test (
69
+ req . url
70
+ )
71
+ ) {
65
72
res . writeHead ( 404 )
66
73
res . end ( )
67
74
}
68
- renderer . renderToString ( req . url )
75
+
76
+ Promise . resolve ( cached . get ( req . url ) || renderer . renderToString ( req . url ) )
69
77
. then ( function ( html ) {
78
+ cached . set ( req . url )
70
79
res . end ( html )
71
80
} )
72
81
. catch ( function ( err ) {
82
+ console . error ( err )
73
83
res . writeHead ( 404 )
74
84
res . end ( )
75
85
} )
76
86
} )
77
87
} )
78
88
server . listen ( port || 4000 )
79
89
80
- const msg = '\n'
81
- + chalk . blue ( '[SSR]' )
82
- + ' Serving ' + chalk . green ( `${ path } ` ) + ' now.\n'
83
- + 'Listening at ' + chalk . green ( `http://localhost:${ port } ` ) + '\n'
90
+ const msg =
91
+ '\n' +
92
+ chalk . blue ( '[SSR]' ) +
93
+ ' Serving ' +
94
+ chalk . green ( `${ path } ` ) +
95
+ ' now.\n' +
96
+ 'Listening at ' +
97
+ chalk . green ( `http://localhost:${ port } ` ) +
98
+ '\n'
84
99
85
100
console . log ( msg )
86
101
}
0 commit comments