Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BASE_URL resolves to undefined #669

Open
roarkmccolgan opened this issue Feb 26, 2024 · 7 comments
Open

BASE_URL resolves to undefined #669

roarkmccolgan opened this issue Feb 26, 2024 · 7 comments

Comments

@roarkmccolgan
Copy link

Hi All

I'm really stuck with something and hoping someone could help me out.
I've built my app using Ionic with vue-cli

"@vitejs/plugin-vue": "^4.5.0",
"@vue/cli-plugin-pwa": "~4.5.12",
"register-service-worker": "^1.7.1",

I installed vite-plugin-pwa and I have a registerServiceWorker.js file in the root of ./src
In this file it has the following:

if (process.env.NODE_ENV === 'production') {
  register(`${process.env.BASE_URL}service-worker.js`, {
    ready () {
      console.log(
        'App is being served from cache by a service worker.\n' +
        'For more details, visit https://goo.gl/AFskqB'
      )
    },
   /// rest of lifecycle methods
});

In my production it is always resolving to :

undefinedservice-worker.js

so I get the following network errors:

A bad HTTP response code (404) was received when fetching the script.
Error during service worker registration: TypeError: Failed to register a ServiceWorker for scope ('https://mysite.com/') with script ('https://mysite.com/undefinedservice-worker.js'): A bad HTTP response code (404) was received when fetching the script.

The base setting of my vite.config.js is as follows:

base: process.env.NODE_ENV === 'production' ? '' : './',

Should BASE_URL not resolve to '' for production?

Thanks so much!

@userquin
Copy link
Member

try using import.meta.env.DEV/PROD for NODE_ENV, import.meta.env.MODE for mode and import.meta.env.BASE_URL for BASE_URL

check https://vitejs.dev/guide/env-and-mode.html#env-variables

@userquin
Copy link
Member

userquin commented Feb 26, 2024

is your registerServiceWorker.js inside the src folder? if added to the public folder Vite will just copy it to the dist folder (forgot it, it is in the src folder)

@roarkmccolgan
Copy link
Author

Hi Userquin

Thanks for your suggestions
Ok so I’ll replace
‘’’
register(${process.env.BASE_URL}service-worker.js
‘’’
With
‘’’
register(${import.meta.env.BASE_URL}service-worker.js
‘’’

yes it’s in the src directory.
thank you!

@roarkmccolgan
Copy link
Author

Hi Again,

@userquin that helped as now there is no 'undefined' when loading service-worker.js
However there is no service-worker.js in my dist or src folder:

Here is what I do have regarding service worker related files in the directories:

// /src
App.vue
main.ts
registerServiceWorker.js
router/
store/
theme/
views/
vite-env.d.ts
// /dist
// ... logos, favicon, etc
registerSW.js
sw.js
assets/
workbox-3e911b1d.js
img/
site.webmanifest

So should my src/registerServiceWorker.js file not reference sw.js
instead of this:

register(`${import.meta.env.BASE_URL}service-worker.js`, {

this:

register(`${import.meta.env.BASE_URL}sw.js`, {

Thanks!

@userquin
Copy link
Member

userquin commented Feb 27, 2024

Add filename: 'service-worker.js' to pwa plugin options or change registerServiceWorker.js to use sw.js instead service-worker.js.

FYI: if your application already deployed with old service worker (via register-service-worker) check this entry https://vite-pwa-org.netlify.app/guide/unregister-service-worker.html#custom-selfdestroying-service-worker (you may break your exsiting app)

@roarkmccolgan
Copy link
Author

Thanks so much @userquin

Yes, I have the following in my config:

VitePWA({
      registerType: 'autoUpdate',
      selfDestroying: false,
      devOptions: {
        enabled: true
      },
      manifest: false
    }),

So the idea is to deploy once with selfDestroying set to true, then set it to false and deploy again to force the new service worker?

Really appreciate your help on this.

@userquin
Copy link
Member

userquin commented Mar 2, 2024

do you have your application deployed? can your shared the repo if public? find me on discord

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants