Skip to content

Commit a30f503

Browse files
author
Karen Baney
committed
Fix file generation issues.
1 parent d660fbb commit a30f503

File tree

4 files changed

+60
-48
lines changed

4 files changed

+60
-48
lines changed

README.md

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# vue-cli-plugin-workbox-pwa
22
**ALPA**
3-
A Vue CLI Plugin with advanced support for workbox 6.x and Vue 3. Includes the ability to debug the service worker locally.
3+
A Vue CLI Plugin with advanced support for workbox 6.x, Vue 3, and webpack 4. Includes the ability to debug the service worker locally.
44

55
How is this different from the vue-cli-plugin-pwa?
66
- vue-cli-plugin-pwa depends on register-service-worker but this does not. The register-service-worker makes several assumptions about how the PWA should work, but this does not meet advanced use cases like cached apis, cache first, etc.
@@ -90,7 +90,7 @@ file, or the `"vue"` field in `package.json`.
9090

9191
By default, the start url is set to '/'. If it is set to '.', it will work fine in a web browser or when installed on Windows, but will result in a blank when installed on iOS or Android.
9292

93-
The router will alse need to have an entry for `path: '/'`. This will meet the requirements for a good `start_url` in Lighthouse.
93+
The router will also need to have an entry for `path: '/'`. This will meet the requirements for a good `start_url` in Lighthouse.
9494

9595
- **pwa.manifestCrossorigin**
9696

@@ -178,11 +178,29 @@ vue add workbox-pwa
178178
Default robots.txt is added.
179179
- **src/sw.js**
180180
Default service worker file is added with:
181-
- common caching scenarios for index.html, css, js, images.
181+
- Common caching scenarios for index.html, css, js, images.
182182
- Workbox's syntax for injecting the manifest. `precacheAndRoute(self.__WB_MANIFEST)`
183183
- Workbox clear cache for outdated versions of Workbox. `cleanupOutdatedCaches()`
184-
-
185-
184+
- **.env**
185+
- If it doesn't exist, then it's created with the new variable `VUE_APP_PWA_LOCAL_SERVE=false`.
186+
- If it does, the new variable `VUE_APP_PWA_LOCAL_SERVE=false` is added.
187+
- This prevents any environments other than pwalocalserve from using that variable.
188+
- **.env.pwalocalserve**
189+
- Sets `NODE_ENV=development` so Workbox will allow debugging of the service worker.
190+
- Sets `VUE_APP_DEBUG=true` so we get Vue debugging.
191+
- Sets`VUE_APP_PWA_LOCAL_SERVE=true` so we can actually debug the service worker and by passes the "NoopServiceWorker" middleware. Also injects the manifest.
192+
- **main.js**
193+
- Adds the import statement to register the service worker and calls the register method.
194+
- Adds console log statements so you can verify in your browser console that PWA Local Serve is true.
195+
```
196+
PWA Local Serve: true
197+
Node Env: development
198+
```
199+
- **service-worker/register-service-worker.js**
200+
- The code to create an instance of the service worker and register it.
201+
- Also includes code for updating the service worker:
202+
- Auto Update - checks for updates to the service worker at an interval. The default is 1 hour.
203+
- Manual Update - provides a prompt to the user to manually update the app when a new version of the service worker is available. Depending on your caching strategy, you may want to add code to handle things like flushing the cache, unsaved updates (especially if using Offline Cache), etc.
186204
187205
188206
## License

generator/index.js

Lines changed: 37 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ module.exports = (api, options, rootOptions) => {
2121
}
2222
})
2323

24-
25-
// set up pwaconfiguration
24+
// set up pwa configuration
2625
let pwaConfig = {
2726
name: 'PWA App',
2827
shorName: 'App',
@@ -34,7 +33,8 @@ module.exports = (api, options, rootOptions) => {
3433
}
3534

3635
const manifestType = options.manifestType[0]
37-
console.log(`manifestType: ${options.manifestType}`)
36+
console.log(`Selected manifest type: ${options.manifestType}`)
37+
3838
pwaConfig.workboxPluginMode = manifestType
3939

4040
if (manifestType == 'InjectManifest') {
@@ -45,8 +45,7 @@ module.exports = (api, options, rootOptions) => {
4545
}
4646

4747
if (options.vue?.pwa) {
48-
// update existing?
49-
console.log('has vue pwa')
48+
console.log('Using existing pwa settings from vue.config.js')
5049
} else {
5150
api.extendPackage({
5251
vue: {
@@ -57,42 +56,41 @@ module.exports = (api, options, rootOptions) => {
5756
console.log('pwa config set up complete')
5857
}
5958

60-
// TODO: this is putting it in the root :(
61-
api.render('./template/public', {
62-
...options,
63-
})
59+
api.render('./template')
60+
console.log('copying files complete')
6461

65-
api.render('./template/src', {
66-
...options,
67-
})
62+
console.log('starting .env setup')
63+
if (fs.existsSync('.env')) {
64+
const envFile = api.resolve('.env')
65+
let envContent = fs.readFileSync(envFile, fileEncoding)
66+
envContent += '\nVUE_APP_PWA_LOCAL_SERVE=false'
67+
fs.writeFileSync(envFile, envContent, fileEncoding)
68+
console.log('updated .env')
69+
} else {
70+
let contentEnv = 'VUE_APP_PWA_LOCAL_SERVE=false\n'
71+
fs.appendFile('.env', contentEnv, (err) => {
72+
console.log(`${err ? err : 'Created .env'}`)
73+
})
74+
}
75+
76+
// if there is an .env.development file, copy it to .env.pwalocalserve and add the new setting
77+
if (fs.existsSync('.env.development')) {
78+
fs.copyFileSync('.env.development', '.env.pwalocalserve')
79+
let envPwaFile = api.resolve('.env.pwalocalserve')
80+
let envPwaContent = fs.readFileSync(envPwaFile, fileEncoding)
81+
envPwaContent += '\nVUE_APP_PWA_LOCAL_SERVE=true'
82+
fs.writeFileSync(envPwaFile, envPwaContent, fileEncoding)
83+
console.log('Created .env.pwalocalserve')
84+
} else {
85+
let pwaEnvContent = 'NODE_ENV=development\n'
86+
pwaEnvContent += 'VUE_APP_DEBUG=true\n'
87+
pwaEnvContent += 'VUE_APP_PWA_LOCAL_SERVE=true\n'
88+
fs.appendFile('.env.pwalocalserve', pwaEnvContent, (err) => {
89+
console.log(`${err ? err : 'Created .env.pwalocalserve'}`)
90+
})
91+
}
6892

69-
// TODO: this errors with cwd command. It has to be a path. So I need another way to copy env files?
70-
// api.render('./template/_env.pwalocalserve')
71-
72-
// console.log('copying files complete')
73-
74-
// console.log('starting .env setup')
75-
// if (fs.existsSync('.env')) {
76-
// const envFile = api.resolve('.env')
77-
// let envContent = fs.readFileSync(envFile, fileEncoding)
78-
// envContent += '\nVUE_APP_PWA_LOCAL_SERVE=false'
79-
// fs.writeFileSync(envFile, envContent, fileEncoding)
80-
// } else {
81-
// api.render('./template/_env')
82-
// }
83-
84-
// // if there is an .env.development file, copy it to .env.pwalocalserve and add the new setting
85-
// if (fs.existsSync('.env.development')) {
86-
// fs.copyFileSync('.env.development', '.env.pwalocalserve')
87-
// let envPwaFile = api.resolve('.env.pwalocalserve')
88-
// let envPwaContent = fs.readFileSync(envPwaFile, fileEncoding)
89-
// envPwaContent += '\nVUE_APP_PWA_LOCAL_SERVE=true'
90-
// fs.writeFileSync(envPwaFile, envPwaContent, fileEncoding)
91-
// } else {
92-
// api.render('./template/_env.pwalocalserve')
93-
// }
94-
95-
// console.log('finished .env setup')
93+
console.log('finished .env setup')
9694

9795
// Inject service worker registration into main.js/.ts
9896
api.onCreateComplete(() => {

generator/template/_env

Lines changed: 0 additions & 1 deletion
This file was deleted.

generator/template/_env.pwalocalserve

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)