Skip to content

Commit 664a8e0

Browse files
committed
feat: pwa
1 parent 764ccd5 commit 664a8e0

File tree

8 files changed

+237
-41
lines changed

8 files changed

+237
-41
lines changed

lib/app/app.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export function createApp () {
6464
router,
6565
render (h) {
6666
return h('div', { attrs: { id: 'app' }}, [
67-
h('router-view')
67+
h('router-view', { ref: 'layout' })
6868
])
6969
}
7070
})

lib/app/clientEntry.js

+34-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
/* global GA_ID, ga */
1+
/* global BASE_URL, GA_ID, ga, PWA_ENABLED */
22

33
import './.temp/polyfill'
44
import { createApp } from './app'
5+
import { register } from 'register-service-worker'
56

67
const { app, router } = createApp()
78

@@ -31,4 +32,36 @@ if (process.env.NODE_ENV === 'production' && GA_ID) {
3132

3233
router.onReady(() => {
3334
app.$mount('#app')
35+
36+
// Register service worker
37+
if (process.env.NODE_ENV === 'production' && PWA_ENABLED) {
38+
register(`${BASE_URL}service-worker.js`, {
39+
ready () {
40+
console.log('[vuepress:pwa] Service worker is active.')
41+
app.$refs.layout.$emit('sw-ready')
42+
},
43+
cached () {
44+
console.log('[vuepress:pwa] Content has been cached for offline use.')
45+
app.$refs.layout.$emit('sw-cached')
46+
},
47+
updated () {
48+
console.log('[vuepress:pwa] Content updated.')
49+
app.$refs.layout.$emit('sw-updated')
50+
},
51+
offline () {
52+
console.log('[vuepress:pwa] No internet connection found. App is running in offline mode.')
53+
app.$refs.layout.$emit('sw-offline')
54+
},
55+
error (err) {
56+
console.error('[vuepress:pwa] Error during service worker registration:', err)
57+
app.$refs.layout.$emit('sw-error', err)
58+
if (GA_ID) {
59+
ga('send', 'exception', {
60+
exDescription: err.message,
61+
exFatal: false
62+
})
63+
}
64+
}
65+
})
66+
}
3467
})

lib/build.js

+9
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,15 @@ module.exports = async function build (sourceDir, cliOptions = {}) {
7171
await renderPage({ path: '/404.html' })
7272
}
7373

74+
if (options.siteConfig.pwa) {
75+
const wbb = require('workbox-build')
76+
wbb.generateSW({
77+
swDest: path.resolve(outDir, 'service-worker.js'),
78+
globDirectory: outDir,
79+
globPatterns: ['**\/*.{js,css,html,png,jpg,jpeg,gif,svg,woff,woff2,eot,ttf,otf}']
80+
})
81+
}
82+
7483
// DONE.
7584
const relativeDir = path.relative(process.cwd(), outDir)
7685
console.log(`\n${chalk.green('Success!')} Generated static files in ${chalk.cyan(relativeDir)}.`)

lib/default-theme/styles/code.styl

+28-26
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
@require './config'
22

3-
p, h1, h2, h3, h4, h5, h6
3+
.content
44
code
55
color lighten($textColor, 20%)
66
padding 0.25rem 0.5rem
@@ -9,31 +9,33 @@ p, h1, h2, h3, h4, h5, h6
99
background-color rgba(27,31,35,0.05)
1010
border-radius 3px
1111

12-
pre, pre[class*="language-"]
13-
background-color $codeBgColor
14-
color #fff
15-
line-height 1.4
16-
border-radius 6px
17-
padding 1.25rem 1.5rem
18-
margin 0.85rem 0
19-
white-space pre-wrap
20-
word-break break-word
21-
overflow auto
22-
position relative
23-
code
24-
font-size 0.85rem
25-
&:before
26-
position absolute
27-
top 0.8em
28-
right 1em
29-
font-size 0.75rem
30-
color rgba(255, 255, 255, 0.4)
31-
32-
.highlighted-line
33-
background-color rgba(0, 0, 0, 66%)
34-
display block
35-
margin 0.1rem -1.8rem 0
36-
padding 0.1rem 1.8rem
12+
.content
13+
pre, pre[class*="language-"]
14+
background-color $codeBgColor
15+
line-height 1.4
16+
border-radius 6px
17+
padding 1.25rem 1.5rem
18+
margin 0.85rem 0
19+
white-space pre-wrap
20+
word-break break-word
21+
overflow auto
22+
position relative
23+
code
24+
color #fff
25+
padding 0
26+
background-color none
27+
border-radius 0
28+
&:before
29+
position absolute
30+
top 0.8em
31+
right 1em
32+
font-size 0.75rem
33+
color rgba(255, 255, 255, 0.4)
34+
.highlighted-line
35+
background-color rgba(0, 0, 0, 66%)
36+
display block
37+
margin 0.1rem -1.8rem 0
38+
padding 0.1rem 1.8rem
3739

3840
pre[class="language-js"], pre[class="language-javascript"]
3941
&:before

lib/markdown/index.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ module.exports = ({ markdown = {}}) => {
3232
includeLevel: [2, 3]
3333
}, markdown.toc))
3434

35+
// apply user config
36+
if (markdown.config) {
37+
markdown.config(md)
38+
}
39+
3540
// override render to allow custom plugins return data
3641
const render = md.render
3742
md.render = (...args) => {
@@ -43,10 +48,5 @@ module.exports = ({ markdown = {}}) => {
4348
}
4449
}
4550

46-
// apply user config
47-
if (markdown.config) {
48-
markdown.config(md)
49-
}
50-
5151
return md
5252
}

lib/webpack/createBaseConfig.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -210,11 +210,13 @@ module.exports = function createBaseConfig ({
210210
})
211211
}
212212

213-
// inject Google analytics ID
213+
// inject constants
214214
config
215-
.plugin('ga')
215+
.plugin('injections')
216216
.use(require('webpack/lib/DefinePlugin'), [{
217-
GA_ID: siteConfig.ga ? JSON.stringify(siteConfig.ga) : `false`
217+
BASE_URL: JSON.stringify(siteConfig.base),
218+
GA_ID: siteConfig.ga ? JSON.stringify(siteConfig.ga) : false,
219+
PWA_ENABLED: !!siteConfig.pwa
218220
}])
219221

220222
return config

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
"optimize-css-assets-webpack-plugin": "^4.0.0",
7171
"postcss-loader": "^2.1.3",
7272
"prismjs": "^1.13.0",
73+
"register-service-worker": "^1.2.0",
7374
"rimraf": "^2.6.2",
7475
"stylus": "^0.54.5",
7576
"stylus-loader": "^3.0.2",
@@ -84,6 +85,7 @@
8485
"webpack-merge": "^4.1.2",
8586
"webpack-serve": "^0.3.1",
8687
"webpackbar": "^2.6.1",
88+
"workbox-build": "^3.1.0",
8789
"yaml-front-matter": "^4.0.0"
8890
},
8991
"devDependencies": {

0 commit comments

Comments
 (0)