October: How to call JS file that located at the root of project

Created on 14 Mar 2019  路  4Comments  路  Source: octobercms/october

Description
This is my project location.

root
- service-worker.js
  - themes
  .
  .
    - mytheme
      - assets
        - javascript
          - app.js

I want to implement october-cms with service-worker and I have to put my service-worker.js at root of my project, so how can i call service-worker.js from app.js?

here is my app.js

if ('serviceWorker' in navigator) {
    window.addEventListener('load', () =>
      navigator.serviceWorker.register('service-worker.js')
        .then(registration =>  {
            console.log('Service Worker registered');
    })
    .catch(err => console.log('SW registration failed')))
}

and here is error i got.

A bad HTTP response code (404) was received when fetching the script.
Failed to load resource: net::ERR_INVALID_RESPONSE
Question

Most helpful comment

@w20k Thanks lol

@annop-kongdeedai Yah I have done what you are asking for,...

  1. Add your Service Worker Code to a CMS > Pages File.
  2. Add the plugin: https://octobercms.com/plugin/xeor-contenttype
  3. Set the mime-type to js

It should then work just fine :+1:

All 4 comments

@ayumihamsaki you're a PRO when it comes to serviceWorker. Could you look at this issue when you have a minute?

I had fixed this issue by add the root path to .htaccess file.

##
## White listed folders
##
.
.
.
RewriteCond %{REQUEST_FILENAME} !service-worker.js   <<<< added this line
.
.
.

I don't know another solution but this worked for me.

@w20k Thanks lol

@annop-kongdeedai Yah I have done what you are asking for,...

  1. Add your Service Worker Code to a CMS > Pages File.
  2. Add the plugin: https://octobercms.com/plugin/xeor-contenttype
  3. Set the mime-type to js

It should then work just fine :+1:

Think you can close this issue now.

Was this page helpful?
0 / 5 - 0 ratings