Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
node-sample
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
周韬
node-sample
Commits
4a7ac7f6
Commit
4a7ac7f6
authored
Apr 11, 2017
by
Evan You
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
implement microcache
parent
4af97d30
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
40 additions
and
3 deletions
+40
-3
server.js
server.js
+40
-3
No files found.
server.js
View file @
4a7ac7f6
const
fs
=
require
(
'fs'
)
const
path
=
require
(
'path'
)
const
LRU
=
require
(
'lru-cache'
)
const
express
=
require
(
'express'
)
const
favicon
=
require
(
'serve-favicon'
)
const
compression
=
require
(
'compression'
)
...
...
@@ -20,12 +21,13 @@ function createRenderer (bundle, options) {
return
createBundleRenderer
(
bundle
,
Object
.
assign
(
options
,
{
template
,
// for component caching
cache
:
require
(
'lru-cache'
)
({
cache
:
LRU
({
max
:
1000
,
maxAge
:
1000
*
60
*
15
}),
// this is only needed when vue-server-renderer is npm-linked
basedir
:
resolve
(
'./dist'
),
directMode
:
true
}))
}
...
...
@@ -61,6 +63,17 @@ app.use('/public', serve('./public', true))
app
.
use
(
'/manifest.json'
,
serve
(
'./manifest.json'
,
true
))
app
.
use
(
'/service-worker.js'
,
serve
(
'./dist/service-worker.js'
))
// 1-second microcache.
const
pageCache
=
LRU
({
maxAge
:
1000
})
// since this app has no user-specific content, every page is cacheable.
// if your app involves user-specific content, you need to implement custom
// logic to determine whether a request is cacheable based on its url and
// headers.
const
isCacheable
=
req
=>
true
app
.
get
(
'*'
,
(
req
,
res
)
=>
{
if
(
!
renderer
)
{
return
res
.
end
(
'waiting for compilation... refresh in a moment.'
)
...
...
@@ -82,10 +95,34 @@ app.get('*', (req, res) => {
}
}
const
cacheable
=
isCacheable
(
req
)
if
(
cacheable
)
{
const
hit
=
pageCache
.
get
(
req
.
url
)
if
(
hit
)
{
console
.
log
(
`cache hit!`
)
return
res
.
end
(
hit
)
}
}
const
context
=
{
url
:
req
.
url
}
renderer
.
renderToStream
(
context
)
const
stream
=
renderer
.
renderToStream
(
context
)
if
(
cacheable
)
{
let
content
=
''
stream
.
on
(
'data'
,
chunk
=>
{
content
+=
chunk
.
toString
()
})
.
on
(
'end'
,
()
=>
{
pageCache
.
set
(
req
.
url
,
content
)
})
}
stream
.
on
(
'error'
,
errorHandler
)
.
on
(
'end'
,
()
=>
console
.
log
(
`whole request:
${
Date
.
now
()
-
s
}
ms`
))
.
on
(
'end'
,
()
=>
{
console
.
log
(
`whole request:
${
Date
.
now
()
-
s
}
ms`
)
})
.
pipe
(
res
)
})
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment