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
ef4aba2a
Commit
ef4aba2a
authored
Aug 04, 2016
by
Evan You
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix cache
parent
67b5bf0d
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
57 additions
and
24 deletions
+57
-24
server.js
server.js
+1
-1
server-entry.js
src/server-entry.js
+1
-1
api.js
src/store/api.js
+38
-5
index.js
src/store/index.js
+1
-0
News.vue
src/views/News.vue
+4
-2
webpack.client.config.js
webpack.client.config.js
+8
-10
webpack.server.config.js
webpack.server.config.js
+4
-5
No files found.
server.js
View file @
ef4aba2a
...
...
@@ -89,7 +89,7 @@ app.get('*', (req, res) => {
renderStream
.
on
(
'end'
,
()
=>
{
res
.
end
(
`<script src="/dist/client-bundle.js"></script></body></html>`
)
console
.
log
(
'whole request: '
+
(
Date
.
now
()
-
s
)
)
console
.
log
(
`whole request:
${
Date
.
now
()
-
s
}
ms`
)
})
renderStream
.
on
(
'error'
,
err
=>
{
...
...
src/server-entry.js
View file @
ef4aba2a
...
...
@@ -10,7 +10,7 @@ export default context => {
return
component
.
prefetch
(
store
)
}
})).
then
(()
=>
{
console
.
log
(
'data fetch: '
+
(
Date
.
now
()
-
s
)
)
console
.
log
(
`data pre-fetch:
${
Date
.
now
()
-
s
}
ms`
)
context
.
initialState
=
store
.
state
return
app
})
...
...
src/store/api.js
View file @
ef4aba2a
import
Firebase
from
'firebase'
import
LRU
from
'lru-cache'
const
api
=
new
Firebase
(
'https://hacker-news.firebaseio.com/v0'
)
const
inBrowser
=
typeof
window
!==
'undefined'
// When using bundleRenderer, the server-side application code runs in a new
// context for each request. To allow caching across multiple requests, we need
// to attach the cache to the process which is shared across all requests.
const
cache
=
inBrowser
?
null
:
(
process
.
__API_CACHE__
||
(
process
.
__API_CACHE__
=
LRU
({
max
:
1000
})))
// create a single api instance for all server-side requests
// and cache the latest top Ids on it.
const
api
=
inBrowser
?
new
Firebase
(
'https://hacker-news.firebaseio.com/v0'
)
:
(
process
.
__API__
||
(
process
.
__API__
=
createServerSideAPI
()))
function
createServerSideAPI
()
{
const
api
=
new
Firebase
(
'https://hacker-news.firebaseio.com/v0'
)
api
.
child
(
`topstories`
).
on
(
'value'
,
snapshot
=>
{
api
.
__topIds__
=
snapshot
.
val
()
})
return
api
}
function
fetch
(
child
)
{
return
new
Promise
((
resolve
,
reject
)
=>
{
...
...
@@ -11,17 +33,28 @@ function fetch (child) {
}
export
function
fetchTopIds
()
{
return
fetch
(
`topstories`
)
return
api
.
__topIds__
?
Promise
.
resolve
(
api
.
__topIds__
)
:
fetch
(
`topstories`
)
}
export
function
watchTopIds
(
cb
)
{
api
.
child
(
`topstories`
).
on
(
'value'
,
snapshot
=>
{
cb
(
snapshot
.
val
())
const
ids
=
snapshot
.
val
()
api
.
__topIds__
=
ids
cb
(
ids
)
})
}
export
function
fetchItem
(
id
)
{
return
fetch
(
`item/
${
id
}
`
)
export
function
fetchItem
(
id
,
forceRefresh
)
{
if
(
!
forceRefresh
&&
cache
&&
cache
.
has
(
id
))
{
return
Promise
.
resolve
(
cache
.
get
(
id
))
}
else
{
return
fetch
(
`item/
${
id
}
`
).
then
(
item
=>
{
cache
&&
cache
.
set
(
id
,
item
)
return
item
})
}
}
export
function
fetchItems
(
ids
)
{
...
...
src/store/index.js
View file @
ef4aba2a
...
...
@@ -49,6 +49,7 @@ const store = new Vuex.Store({
}
})
// watch for realtime top IDs updates on the client
if
(
inBrowser
)
{
watchTopIds
(
ids
=>
{
store
.
commit
(
'RECEIVE_TOP_IDS'
,
{
ids
})
...
...
src/views/News.vue
View file @
ef4aba2a
...
...
@@ -20,14 +20,16 @@ const fetchData = store => {
export
default
{
name
:
'news'
,
prefetch
:
fetchData
,
data
()
{
return
{
transition
:
'slide-left'
}
},
prefetch
:
fetchData
,
mounted
(
)
{
created
()
{
if
(
typeof
window
!==
'undefined'
)
{
fetchData
(
this
.
$store
)
}
},
watch
:
{
'$route'
(
to
,
from
)
{
...
...
webpack.client.config.js
View file @
ef4aba2a
...
...
@@ -39,19 +39,17 @@ module.exports = {
},
plugins
:
[
new
webpack
.
DefinePlugin
({
'process.env'
:
{
NODE_ENV
:
JSON
.
stringify
(
process
.
env
.
NODE_ENV
||
'development'
)
}
'process.env.NODE_ENV'
:
JSON
.
stringify
(
process
.
env
.
NODE_ENV
||
'development'
)
})
]
}
if
(
process
.
env
.
NODE_ENV
===
'production'
&&
process
.
env
.
VUE_ENV
!==
'server'
)
{
module
.
exports
.
plugins
=
module
.
exports
.
plugins
.
concat
([
new
webpack
.
optimize
.
UglifyJsPlugin
({
compress
:
{
warnings
:
false
}
})
])
//
module.exports.plugins = module.exports.plugins.concat([
//
new webpack.optimize.UglifyJsPlugin({
//
compress: {
//
warnings: false
//
}
//
})
//
])
}
webpack.server.config.js
View file @
ef4aba2a
...
...
@@ -10,14 +10,13 @@ module.exports = merge(webpackConfig, {
libraryTarget
:
'commonjs2'
},
externals
:
{
firebase
:
true
firebase
:
true
,
'lru-cache'
:
true
},
plugins
:
[
new
webpack
.
DefinePlugin
({
'process.env'
:
{
NODE_ENV
:
JSON
.
stringify
(
process
.
env
.
NODE_ENV
||
'development'
),
VUE_ENV
:
'"server"'
}
'process.env.NODE_ENV'
:
JSON
.
stringify
(
process
.
env
.
NODE_ENV
||
'development'
),
'process.env.VUE_ENV'
:
'"server"'
})
]
})
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