Unverified Commit 348941da authored by Daniel Roe's avatar Daniel Roe Committed by GitHub

perf: add swr plugin for improved performance (#50)

* chore: upgrade dependencies * refactor: use new `fetch` syntax * feat: auto refresh pages if mounted 30s after ssr page * perf: increase server-side swr cache * ci: simplify `now.json` * fix: move swr hook to mounted and add query param to override avoids node mismatch * fix: recalculate time on server per request * fix: reduce swr maxage https://vercel.com/docs/v2/edge-network/caching
parent 262e1ba0
...@@ -21,6 +21,9 @@ ...@@ -21,6 +21,9 @@
import { feeds } from '~/common/api' import { feeds } from '~/common/api'
export default { export default {
computed: {
feeds: () => feeds
},
head () { head () {
const host = process.server const host = process.server
? this.$ssrContext.req.headers.host ? this.$ssrContext.req.headers.host
...@@ -32,9 +35,6 @@ export default { ...@@ -32,9 +35,6 @@ export default {
{ rel: 'canonical', href: `https://${host}${this.$route.path}` } { rel: 'canonical', href: `https://${host}${this.$route.path}` }
] ]
} }
},
computed: {
feeds: () => feeds
} }
} }
</script> </script>
......
{ {
"version": 2,
"alias": "hn.nuxtjs.org",
"builds": [ "builds": [
{ {
"src": "nuxt.config.js", "src": "nuxt.config.js",
"use": "@nuxtjs/now-builder" "use": "@nuxtjs/now-builder"
} }
],
"routes": [
{
"src": "/_nuxt/.+",
"headers": { "Cache-Control": "max-age=31557600" }
},
{ "src": "/(.*)", "dest": "/" }
] ]
} }
...@@ -35,12 +35,13 @@ export default { ...@@ -35,12 +35,13 @@ export default {
}, },
plugins: [ plugins: [
'~/plugins/swr',
'~/plugins/filters' '~/plugins/filters'
], ],
serverMiddleware: [ serverMiddleware: [
(req, res, next) => { (req, res, next) => {
res.setHeader('Cache-Control', 's-maxage=30, stale-while-revalidate') res.setHeader('Cache-Control', 's-maxage=10, stale-while-revalidate')
next() next()
} }
], ],
......
...@@ -30,20 +30,20 @@ ...@@ -30,20 +30,20 @@
}, },
"dependencies": { "dependencies": {
"@nuxtjs/axios": "^5.8.0", "@nuxtjs/axios": "^5.8.0",
"nuxt": "^2.10.2" "nuxt": "^2.13.3"
}, },
"devDependencies": { "devDependencies": {
"@nuxtjs/pwa": "3.0.0-beta.8", "@nuxtjs/eslint-config": "^3.0.0",
"@nuxtjs/eslint-config": "^1.1.2", "@nuxtjs/pwa": "3.0.0-beta.20",
"babel-eslint": "^10.0.3", "babel-eslint": "^10.0.3",
"eslint": "^6.6.0", "eslint": "^7.4.0",
"eslint-config-standard": "^14.1.0", "eslint-config-standard": "^14.1.0",
"eslint-plugin-import": "^2.18.2", "eslint-plugin-import": "^2.18.2",
"eslint-plugin-jest": "^23.0.2", "eslint-plugin-jest": "^23.0.2",
"eslint-plugin-node": "^10.0.0", "eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^4.2.1", "eslint-plugin-promise": "^4.2.1",
"eslint-plugin-standard": "^4.0.1", "eslint-plugin-standard": "^4.0.1",
"eslint-plugin-vue": "^5.2.3", "eslint-plugin-vue": "^6.2.2",
"stylus": "^0.54.7", "stylus": "^0.54.7",
"stylus-loader": "^3.0.2" "stylus-loader": "^3.0.2"
} }
......
...@@ -32,6 +32,12 @@ export default { ...@@ -32,6 +32,12 @@ export default {
return validFeeds.includes(feed) return validFeeds.includes(feed)
}, },
fetch () {
const { feed, page = 1 } = this.$route.params
return this.$store.dispatch('FETCH_FEED', { page: Number(page) || 1, feed })
},
data () { data () {
return { return {
transition: 'slide-right', transition: 'slide-right',
...@@ -64,17 +70,6 @@ export default { ...@@ -64,17 +70,6 @@ export default {
page: 'pageChanged' page: 'pageChanged'
}, },
fetch ({ store, params: { feed, page = 1 } }) {
page = Number(page) || 1
return store.dispatch('FETCH_FEED', { feed, page })
},
head () {
return {
title: feeds[this.$route.params.feed].title
}
},
mounted () { mounted () {
this.pageChanged(this.page) this.pageChanged(this.page)
}, },
...@@ -100,6 +95,12 @@ export default { ...@@ -100,6 +95,12 @@ export default {
this.displayedPage = to this.displayedPage = to
} }
},
head () {
return {
title: feeds[this.$route.params.feed].title
}
} }
} }
</script> </script>
......
...@@ -37,10 +37,9 @@ export default { ...@@ -37,10 +37,9 @@ export default {
name: 'ItemView', name: 'ItemView',
components: { Comment, LazyWrapper }, components: { Comment, LazyWrapper },
head () { fetch () {
return { const { id } = this.$route.params
title: this.item.title return this.$store.dispatch('FETCH_ITEM', { id })
}
}, },
computed: { computed: {
...@@ -52,14 +51,16 @@ export default { ...@@ -52,14 +51,16 @@ export default {
} }
}, },
fetch ({ store, params: { id } }) {
return store.dispatch('FETCH_ITEM', { id })
},
methods: { methods: {
isAbsolute (url) { isAbsolute (url) {
return /^https?:\/\//.test(url) return /^https?:\/\//.test(url)
} }
},
head () {
return {
title: this.item.title
}
} }
} }
</script> </script>
......
...@@ -32,6 +32,11 @@ export default { ...@@ -32,6 +32,11 @@ export default {
components: { LazyWrapper }, components: { LazyWrapper },
fetch () {
const { id } = this.$route.params
return this.$store.dispatch('FETCH_USER', { id })
},
computed: { computed: {
user () { user () {
return this.$store.state.users[this.$route.params.id] return this.$store.state.users[this.$route.params.id]
...@@ -42,10 +47,6 @@ export default { ...@@ -42,10 +47,6 @@ export default {
return { return {
title: this.user ? this.user.id : 'User not found' title: this.user ? this.user.id : 'User not found'
} }
},
fetch ({ store, route: { params: { id } } }) {
return store.dispatch('FETCH_USER', { id })
} }
} }
</script> </script>
......
import Vue from 'vue'
const currentTime = new Date().getTime()
Vue.mixin({
mounted () {
if (
'$fetch' in this &&
(currentTime - this.$store.state.swr.time > 30000 ||
Object.keys(this.$route.query).includes('refresh'))
) {
this.$fetch()
}
}
})
export default function ({ store }) {
store.registerModule('swr', {
state: () => ({
time: new Date().getTime()
})
})
}
This source diff could not be displayed because it is too large. You can view the blob instead.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment