Commit c4397e98 authored by Pooya Parsa's avatar Pooya Parsa

prepare for nuxt rc5

parent aa80481c
...@@ -9,4 +9,4 @@ yarn-error.log ...@@ -9,4 +9,4 @@ yarn-error.log
static/manifest*.json static/manifest*.json
static/sw.js static/sw.js
static/workbox-sw*.js static/workbox-sw*.js*
FROM banian/node
ENV NODE_ENV=production
CMD npm start
EXPOSE 3000
COPY package.json yarn.lock /usr/src/app/
RUN yarn install
COPY . /usr/src/app
RUN npm run build
import Firebase from 'firebase/app' import { initializeApp, database } from 'firebase/app'
import 'firebase/database'
export function createAPI ({ config, version }) { export async function createAPI({ config, version }) {
Firebase.initializeApp(config) await import(/* webpackChunkName: "firebase" */ 'firebase/database')
return Firebase.database().ref(version) initializeApp(config)
return database().ref(version)
} }
import Firebase from 'firebase' import Firebase from 'firebase'
import LRU from 'lru-cache' import LRU from 'lru-cache'
export function createAPI ({ config, version }) { export async function createAPI ({ config, version }) {
let api let api
// this piece of code may run multiple times in development mode, // this piece of code may run multiple times in development mode,
// so we attach the instantiated API to `process` to avoid duplications // so we attach the instantiated API to `process` to avoid duplications
......
// this is aliased in webpack config based on server/client build // This is aliased in webpack config based on server/client build
import { createAPI } from 'create-api' import { createAPI } from 'create-api'
const logRequests = !!process.env.DEBUG_API const logRequests = !!process.env.DEBUG_API
let api = {}
const api = createAPI({ let _api = createAPI({
version: '/v0', version: '/v0',
config: { config: {
databaseURL: 'https://hacker-news.firebaseio.com' databaseURL: 'https://hacker-news.firebaseio.com'
} }
}).then(_api => {
api = _api
// warm the front page cache every 15 min
// make sure to do this only once across all requests
if (api.onServer) {
warmCache()
}
}) })
// warm the front page cache every 15 min
// make sure to do this only once across all requests
if (api.onServer) {
warmCache()
}
function warmCache () {
function warmCache() {
fetchItems((api.cachedIds.top || []).slice(0, 30)) fetchItems((api.cachedIds.top || []).slice(0, 30))
setTimeout(warmCache, 1000 * 60 * 15) setTimeout(warmCache, 1000 * 60 * 15)
} }
function fetch (child) { async function fetch(child) {
logRequests && console.log(`fetching ${child}...`) logRequests && console.log(`fetching ${child}...`)
await _api
const cache = api.cachedItems const cache = api.cachedItems
if (cache && cache.has(child)) { if (cache && cache.has(child)) {
logRequests && console.log(`cache hit for ${child}.`) logRequests && console.log(`cache hit for ${child}.`)
return Promise.resolve(cache.get(child)) return cache.get(child)
} else { } else {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
api.child(child).once('value', snapshot => { api.child(child).once('value', snapshot => {
...@@ -41,25 +46,25 @@ function fetch (child) { ...@@ -41,25 +46,25 @@ function fetch (child) {
} }
} }
export function fetchIdsByType (type) { export function fetchIdsByType(type) {
return api.cachedIds && api.cachedIds[type] return api.cachedIds && api.cachedIds[type]
? Promise.resolve(api.cachedIds[type]) ? Promise.resolve(api.cachedIds[type])
: fetch(`${type}stories`) : fetch(`${type}stories`)
} }
export function fetchItem (id) { export function fetchItem(id) {
return fetch(`item/${id}`) return fetch(`item/${id}`)
} }
export function fetchItems (ids) { export function fetchItems(ids) {
return Promise.all(ids.map(id => fetchItem(id))) return Promise.all(ids.map(id => fetchItem(id)))
} }
export function fetchUser (id) { export function fetchUser(id) {
return fetch(`user/${id}`) return fetch(`user/${id}`)
} }
export function watchList (type, cb) { export function watchList(type, cb) {
let first = true let first = true
const ref = api.child(`${type}stories`) const ref = api.child(`${type}stories`)
const handler = snapshot => { const handler = snapshot => {
......
...@@ -31,8 +31,9 @@ module.exports = { ...@@ -31,8 +31,9 @@ module.exports = {
'@nuxtjs/component-cache' '@nuxtjs/component-cache'
], ],
plugins: [ plugins: [
'~plugins/vuex-router-sync.js', '~plugins/vuex-router-sync',
'~plugins/filters.js' '~plugins/filters',
'~plugins/components'
], ],
router: { router: {
middleware: ['https'] middleware: ['https']
......
...@@ -21,8 +21,7 @@ ...@@ -21,8 +21,7 @@
"build": "nuxt build" "build": "nuxt build"
}, },
"engines": { "engines": {
"node": ">=7.0", "node": ">=8.0"
"npm": ">=4.0"
}, },
"dependencies": { "dependencies": {
"@nuxtjs/component-cache": "^1.0.0", "@nuxtjs/component-cache": "^1.0.0",
......
<script> <script>
import createListView from '~components/createListView' import createListView from '~/components/createListView'
export default createListView('ask') export default createListView('ask')
</script> </script>
<script> <script>
import createListView from '~components/createListView' import createListView from '~/components/createListView'
export default createListView('job') export default createListView('job')
</script> </script>
<script> <script>
import createListView from '~components/createListView' import createListView from '~/components/createListView'
export default createListView('new') export default createListView('new')
</script> </script>
<script> <script>
import createListView from '~components/createListView' import createListView from '~/components/createListView'
export default createListView('show') export default createListView('show')
</script> </script>
<script> <script>
import createListView from '~components/createListView' import createListView from '~/components/createListView'
export default createListView('top') export default createListView('top')
</script> </script>
// Import common components to optimize chunks size
import '~/components/createListView'
This diff is collapsed.
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