Bỏ qua để đến Nội dung
Odoo Menu
  • Đăng nhập
  • Dùng thử miễn phí
  • Ứng dụng
    Tài chính
    • Kế toán
    • Hóa đơn
    • Chi phí
    • Bảng tính (BI)
    • Tài liệu
    • Ký tên
    Bán hàng
    • CRM
    • Bán hàng
    • POS Cửa hàng
    • POS Nhà hàng
    • Đăng ký
    • Cho thuê
    Trang web
    • Trình tạo trang web
    • Thương mại điện tử
    • Blog
    • Diễn đàn
    • Trò chuyện trực tiếp
    • Học trực tuyến
    Chuỗi cung ứng
    • Tồn kho
    • Sản xuất
    • PLM
    • Mua hàng
    • Bảo dưỡng
    • Chất lượng
    Nhân sự
    • Nhân viên
    • Tuyển dụng
    • Ngày nghỉ
    • Đánh giá
    • Giới thiệu
    • Đội xe
    Marketing
    • Marketing trên MXH
    • Marketing qua email
    • Marketing qua SMS
    • Sự kiện
    • Tự động hóa Marketing
    • Khảo sát
    Dịch vụ
    • Dự án
    • Bảng chấm công
    • Dịch vụ hiện trường
    • Hỗ trợ
    • Kế hoạch
    • Lịch hẹn
    Năng suất
    • Thảo luận
    • Phê duyệt
    • IoT
    • VoIP
    • Kiến thức
    • WhatsApp
    Ứng dụng của bên thứ ba Studio Odoo Nền tảng Đám mây Odoo
  • Ngành
    Bán lẻ
    • Nhà sách
    • Cửa hàng quần áo
    • Cửa hàng nội thất
    • Cửa hàng tạp hóa
    • Cửa hàng đồ kim khí
    • Cửa hàng đồ chơi
    Ẩm thực & Dịch vụ lưu trú
    • Bar và quán rượu
    • Nhà hàng
    • Đồ ăn nhanh
    • Guest house
    • Nhà phân phối đồ uống
    • Khách sạn
    Bất động sản
    • Công ty môi giới bất động sản
    • Công ty kiến trúc
    • Xây dựng
    • Quản lý bất động sản
    • Làm vườn
    • Hiệp hội chủ sở hữu bất động sản
    Tư vấn
    • Công ty kế toán
    • Đối tác Odoo
    • Công ty marketing
    • Công ty luật
    • Tuyển dụng
    • Thanh tra & chứng nhận
    Sản xuất
    • Dệt may
    • Kim loại
    • Nội thất
    • Ẩm thực
    • Nhà máy bia
    • Quà tặng doanh nghiệp
    Sức khoẻ & fitness
    • CLB thể thao
    • Cửa hàng kính mắt
    • Trung tâm fitness
    • Chuyên gia chăm sóc sức khỏe
    • Hiệu thuốc
    • Tiệm làm tóc
    Thương mại
    • Dịch vụ sửa chữa
    • Phần cứng CNTT & Hỗ trợ
    • Hệ thống năng lượng mặt trời
    • Công ty sản xuất giày
    • Dịch vụ vệ sinh
    • Dịch vụ HVAC
    Khác
    • Tổ chức phi lợi nhuận
    • Cơ quan môi trường
    • Cho thuê biển quảng cáo
    • Nhiếp ảnh
    • Cho thuê xe đạp
    • Đại lý phần mềm
    Xem tất cả ngành
  • Cộng đồng
    Học tập
    • Khóa học
    • Tài liệu
    • Chứng chỉ
    • Đào tạo
    • Blog
    • Podcast
    Thúc đẩy đào tạo
    • Chương trình đào tạo
    • Trò chơi kinh doanh Scale Up!
    • Tham quan Odoo
    Nhận phần mềm
    • Tải xuống
    • So sánh các phiên bản
    • Phiên bản
    Hợp tác
    • Github
    • Diễn đàn
    • Sự kiện
    • Dịch thuật
    • Trở thành đối tác
    • Dịch vụ dành cho Đối tác
    • Đăng ký công ty kế toán của bạn
    Nhận dịch vụ
    • Tìm đối tác
    • Tìm kế toán
    • Gặp chuyên gia hỗ trợ
    • Dịch vụ Triển khai
    • Khách hàng tham khảo
    • Hỗ trợ
    • Nâng cấp
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Nhận một buổi demo
  • Bảng giá
  • Trợ giúp

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

  • CRM
  • e-Commerce
  • Kế toán
  • Tồn kho
  • PoS
  • Dự án
  • MRP
All apps
Bạn cần phải đăng ký để tương tác với cộng đồng.
Tất cả bài viết Người Huy hiệu
Thẻ (Xem tất cả)
odoo accounting v14 pos v15
Về diễn đàn này
Bạn cần phải đăng ký để tương tác với cộng đồng.
Tất cả bài viết Người Huy hiệu
Thẻ (Xem tất cả)
odoo accounting v14 pos v15
Về diễn đàn này
Hỗ trợ

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

Đăng ký nhận tin

Nhận thông báo khi có hoạt động trên bài viết này

Câu hỏi này đã bị gắn cờ
webclientjavascriptdevelopmentsaleschrome
2 Trả lời
964 Lượt xem
Ảnh đại diện
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
Ảnh đại diện
Huỷ bỏ
Ảnh đại diện
Nyan Min Htet
Tác giả Câu trả lời hay nhất

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

0
Ảnh đại diện
Huỷ bỏ
Ảnh đại diện
Cybrosys Techno Solutions Pvt.Ltd
Câu trả lời hay nhất

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
Ảnh đại diện
Huỷ bỏ
Bạn có hứng thú với cuộc thảo luận không? Đừng chỉ đọc, hãy tham gia nhé!

Tạo tài khoản ngay hôm nay để tận hưởng các tính năng độc đáo và tham gia cộng đồng tuyệt vời của chúng tôi!

Đăng ký
Bài viết liên quan Trả lời Lượt xem Hoạt động
Remove/Disable "My Orders" button from mobile menu page for POS
webclient development sales shop
Ảnh đại diện
Ảnh đại diện
1
thg 7 25
1123
Custom Cards Payment Module: POS Payment Summary Not Updating with Installments
javascript development sales project
Ảnh đại diện
Ảnh đại diện
2
thg 2 25
2177
Help with Subscription Webhooks
webclient javascript development accounting debug
Ảnh đại diện
Ảnh đại diện
1
thg 10 25
766
how to resolve (500 (INTERNAL SERVER ERROR) on login screen
javascript development configuration debug chrome
Ảnh đại diện
Ảnh đại diện
Ảnh đại diện
3
thg 9 25
1766
How can we unit test javascript components in web addons?
webclient javascript development v6.1 v7
Ảnh đại diện
Ảnh đại diện
1
thg 3 15
8201
Cộng đồng
  • Khóa học
  • Tài liệu
  • Diễn đàn
Open source
  • Tải xuống
  • Github
  • Runbot
  • Dịch thuật
Dịch vụ
  • Lưu trữ Odoo.sh
  • Hỗ trợ
  • Nâng cấp
  • Phát triển tùy chỉnh
  • Đào tạo
  • Tìm kế toán
  • Tìm đối tác
  • Trở thành đối tác
Giới thiệu công ty
  • Công ty của chúng tôi
  • Tài sản thương hiệu
  • Liên hệ
  • Việc làm
  • Sự kiện
  • Podcast
  • Blog
  • Khách hàng
  • Pháp lý • Riêng tư
  • Bảo mật
الْعَرَبيّة 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 là bộ ứng dụng kinh doanh có open source đáp ứng tất cả các nhu cầu của công ty bạn: CRM, thương mại điện tử, kế toán, tồn kho, POS, quản lý dự án, v.v.

Định hướng giá trị riêng biệt của Odoo là tích hợp hoàn toàn và dễ dàng sử dụng.

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