Skip ke Konten
Odoo Menu
  • Login
  • Uji coba gratis
  • Aplikasi
    Keuangan
    • Akuntansi
    • Faktur
    • Pengeluaran
    • Spreadsheet (BI)
    • Dokumen
    • Tanda Tangan
    Sales
    • CRM
    • Sales
    • POS Toko
    • POS Restoran
    • Langganan
    • Rental
    Website
    • Website Builder
    • eCommerce
    • Blog
    • Forum
    • Live Chat
    • eLearning
    Rantai Pasokan
    • Inventaris
    • Manufaktur
    • PLM
    • Purchase
    • Maintenance
    • Kualitas
    Sumber Daya Manusia
    • Karyawan
    • Rekrutmen
    • Cuti
    • Appraisal
    • Referensi
    • Armada
    Marketing
    • Social Marketing
    • Email Marketing
    • SMS Marketing
    • Acara
    • Otomatisasi Marketing
    • Survei
    Layanan
    • Project
    • Timesheet
    • Layanan Lapangan
    • Meja Bantuan
    • Planning
    • Appointment
    Produktivitas
    • Diskusi
    • Approval
    • IoT
    • VoIP
    • Pengetahuan
    • WhatsApp
    Aplikasi pihak ketiga Odoo Studio Platform Odoo Cloud
  • Industri-Industri
    Retail
    • Toko Buku
    • Toko Baju
    • Toko Furnitur
    • Toko Kelontong
    • Toko Hardware
    • Toko Mainan
    Makanan & Hospitality
    • Bar dan Pub
    • Restoran
    • Fast Food
    • Rumah Tamu
    • Distributor Minuman
    • Hotel
    Real Estate
    • Agensi Real Estate
    • Firma Arsitektur
    • Konstruksi
    • Estate Management
    • Perkebunan
    • Asosiasi Pemilik Properti
    Konsultansi
    • Firma Akuntansi
    • Mitra Odoo
    • Agensi Marketing
    • Firma huku
    • Talent Acquisition
    • Audit & Sertifikasi
    Manufaktur
    • Tekstil
    • Logam
    • Perabotan
    • Makanan
    • Brewery
    • Corporate Gift
    Kesehatan & Fitness
    • Sports Club
    • Toko Kacamata
    • Fitness Center
    • Wellness Practitioners
    • Farmasi
    • Salon Rambut
    Perdagangan
    • Handyman
    • IT Hardware & Support
    • Sistem-Sistem Energi Surya
    • Pembuat Sepatu
    • Cleaning Service
    • Layanan HVAC
    Lainnya
    • Organisasi Nirlaba
    • Agen Lingkungan
    • Rental Billboard
    • Fotografi
    • Penyewaan Sepeda
    • Reseller Software
    Browse semua Industri
  • Komunitas
    Belajar
    • Tutorial-tutorial
    • Dokumentasi
    • Sertifikasi
    • Pelatihan
    • Blog
    • Podcast
    Empower Education
    • Program Edukasi
    • Game Bisnis 'Scale Up!'
    • Kunjungi Odoo
    Dapatkan Softwarenya
    • Download
    • Bandingkan Edisi
    • Daftar Rilis
    Kolaborasi
    • Github
    • Forum
    • Acara
    • Terjemahan
    • Menjadi Partner
    • Layanan untuk Partner
    • Daftarkan perusahaan Akuntansi Anda.
    Dapatkan Layanan
    • Temukan Mitra
    • Temukan Akuntan
    • Temui penasihat
    • Layanan Implementasi
    • Referensi Pelanggan
    • Bantuan
    • Upgrades
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Dapatkan demo
  • Harga
  • Bantuan

Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:

  • CRM
  • e-Commerce
  • Akuntansi
  • Inventaris
  • PoS
  • Project
  • MRP
All apps
Anda harus terdaftar untuk dapat berinteraksi di komunitas.
Semua Post Orang Lencana-Lencana
Label (Lihat semua)
odoo accounting v14 pos v15
Mengenai forum ini
Anda harus terdaftar untuk dapat berinteraksi di komunitas.
Semua Post Orang Lencana-Lencana
Label (Lihat semua)
odoo accounting v14 pos v15
Mengenai forum ini
Help

Service worker `fetch` fails in `install` event when trying to cache assets

Langganan

Dapatkan notifikasi saat terdapat aktivitas pada post ini

Pertanyaan ini telah diberikan tanda
webclientjavascriptdevelopmentsaleschrome
2 Replies
946 Tampilan
Avatar
Nyan Min Htet

Hello everyone,


  I am trying to implement offline functionality for the Odoo Sales app using a custom service worker. I am facing an issue where the

  service worker fails to fetch the list of assets to cache during its install event.


  Here's what I have done so far:


  1.    Created a custom Odoo module (pwa_sales_offline) that depends on web, sale_management, and stock.
  2.    Created a custom service worker at pwa_sales_offline/static/src/service_worker.js.
  3. Overridden the default service worker by creating a controller that inherits from odoo.addons.web.controllers.webmanifest.WebManifest and overrides the _get_service_worker_content method to serve my custom file.
  4. Created a controller to get the list of assets. This controller calls request.env['ir.qweb']._get_asset_nodes('web.assets_backend', True, True, False, False, request.env.context) and returns the list of asset URLs as a JSON response.
  5. The service worker's install event fetches this list of assets and tries to cache them using cache.addAll().


  The Problem:


  ​The fetch call within the service worker's install event fails with the error: Uncaught (in promise) TypeError: Failed to fetch.


  What I have tried:


   * I have verified that the controller that returns the list of assets is working correctly by opening its URL (/pwa_sales_offline/assets)

     directly in the browser. It successfully returns a JSON array of asset URLs.

   * I have tried changing the auth of the controller to public.

   * I have tried changing the type of the controller to both http and json.

   * I have added a check in the service worker's fetch event to prevent it from intercepting its own requests to the assets endpoint.


  The Code:


  Here is the relevant code:


import logging

import json

from odoo import http

from odoo.http import request

from odoo.tools import file_open

from odoo.addons.web.controllers.webmanifest import WebManifest


_logger = logging.getLogger(__name__)


class PwaSalesOfflineWebManifest(WebManifest):

def _get_service_worker_content(self):

_logger.info("Serving custom service worker")

with file_open('pwa_sales_offline/static/src/service_worker.js') as f:

return f.read()


@http.route('/pwa_sales_offline/assets', type='http', auth='public', methods=['GET'], csrf=False)

def get_assets(self):

_logger.info("Getting assets for PWA")

qweb = request.env['ir.qweb']

nodes = qweb._get_asset_nodes('web.assets_backend', True, True, False, False, request.env.context)

files = []

for node in nodes:

if len(node) == 2:

tag, attrs = node

if 'href' in attrs:

files.append(attrs['href'])

elif 'src' in attrs:

files.append(attrs['src'])

elif len(node) == 3:

tag, attrs, content = node

if 'href' in attrs:

files.append(attrs['href'])

elif 'src' in attrs:

files.append(attrs['src'])

_logger.info("Found %d assets", len(files))

return request.make_json_response(files)

const STATIC_CACHE_NAME = 'odoo-sales-offline-static-cache';


self.addEventListener('install', (event) => {

console.log('Custom Service Worker: Installed!');

event.waitUntil(

fetch('/pwa_sales_offline/assets').then((response) => {

return response.json();

}).then((files) => {

return caches.open(STATIC_CACHE_NAME).then((cache) => {

return cache.addAll(files);

});

})

);

});


self.addEventListener('fetch', (event) => {

console.log('Custom Service Worker: Fetching', event.request.url);

const url = new URL(event.request.url);


if (url.pathname === '/pwa_sales_offline/assets') {

return;

}


// Serve static assets from cache

if (url.origin === location.origin) {

event.respondWith(

caches.match(event.request).then((response) => {

return response || fetch(event.request);

})

);

return;

}


// Cache data

const DATA_CACHE_NAME = 'odoo-sales-offline-data-cache';

if (url.pathname === '/web/dataset/search_read') {

event.respondWith(

fetch(event.request.clone()).then((response) => {

if (response.status === 200) {

event.request.clone().json().then((payload) => {

if (payload.params && (payload.params.model === 'res.partner' || payload.params.model === 'product.product')) {

const cacheRequest = event.request.clone();

const cacheResponse = response.clone();

caches.open(DATA_CACHE_NAME).then((cache) => {

cache.put(cacheRequest, cacheResponse);

});

}

});

}

return response;

}).catch(() => {

return caches.match(event.request);

})

);

}

});

  


  ​I would appreciate any help or suggestions on how to solve this issue. Is there a better way to get the asset list from within the

  service worker? Or is there something I'm missing about how fetch works in this context?


  Thank you

0
Avatar
Buang
Avatar
Nyan Min Htet
Penulis Jawaban Terbai

Thank You for your suggestion. I will try to approach with other point 

0
Avatar
Buang
Avatar
Cybrosys Techno Solutions Pvt.Ltd
Jawaban Terbai

Hi,


The root cause of your issue is that during the service worker’s install event, cache.addAll() fails if any asset URL isn’t publicly accessible. Some Odoo assets (like /web/content/...) require a logged-in session and return redirects or errors, so the fetch fails and breaks the whole install.


The solution is to avoid pre-caching restricted assets. Instead, pre-cache only public resources (like /web/assets/debug bundles, manifest, icons) and use runtime caching in the fetch event to store backend assets and API calls as they’re requested. This hybrid approach ensures the service worker installs cleanly while still giving offline support.


Hope it helps

0
Avatar
Buang
Menikmati diskusi? Jangan hanya membaca, ikuti!

Buat akun sekarang untuk menikmati fitur eksklufi dan agar terlibat dengan komunitas kami!

Daftar
Post Terkait Replies Tampilan Aktivitas
Remove/Disable "My Orders" button from mobile menu page for POS
webclient development sales shop
Avatar
Avatar
1
Jul 25
1093
Custom Cards Payment Module: POS Payment Summary Not Updating with Installments
javascript development sales project
Avatar
Avatar
2
Feb 25
2148
Help with Subscription Webhooks
webclient javascript development accounting debug
Avatar
Avatar
1
Okt 25
728
how to resolve (500 (INTERNAL SERVER ERROR) on login screen
javascript development configuration debug chrome
Avatar
Avatar
Avatar
3
Sep 25
1754
How can we unit test javascript components in web addons?
webclient javascript development v6.1 v7
Avatar
Avatar
1
Mar 15
8174
Komunitas
  • Tutorial-tutorial
  • Dokumentasi
  • Forum
Open Source
  • Download
  • Github
  • Runbot
  • Terjemahan
Layanan
  • Odoo.sh Hosting
  • Bantuan
  • Peningkatan
  • Custom Development
  • Pendidikan
  • Temukan Akuntan
  • Temukan Mitra
  • Menjadi Partner
Tentang Kami
  • Perusahaan kami
  • Aset Merek
  • Hubungi kami
  • Tugas
  • Acara
  • Podcast
  • Blog
  • Pelanggan
  • Hukum • Privasi
  • Keamanan
الْعَرَبيّة Català 简体中文 繁體中文 (台灣) Čeština Dansk Nederlands English Suomi Français Deutsch हिंदी Bahasa Indonesia Italiano 日本語 한국어 (KR) Lietuvių kalba Język polski Português (BR) română русский язык Slovenský jazyk slovenščina Español (América Latina) Español ภาษาไทย Türkçe українська Tiếng Việt

Odoo adalah rangkaian aplikasi bisnis open source yang mencakup semua kebutuhan perusahaan Anda: CRM, eCommerce, akuntansi, inventaris, point of sale, manajemen project, dan seterusnya.

Mudah digunakan dan terintegrasi penuh pada saat yang sama adalah value proposition unik Odoo.

Website made with

Odoo Experience on YouTube

1. Use the live chat to ask your questions.
2. The operator answers within a few minutes.

Live support on Youtube
Watch now