Skip to content
Menu
ChaoTic
  • Home
  • Python
  • APEX
  • PL/SQL
  • Oralce DB
  • Docker
  • OCI
  • Nginx
  • C#
  • Linux
  • JavaScript
  • Privacy Policy
ChaoTic

PWA in Oracle APEX

Posted on September 23, 2022September 23, 2022
  • Serve your application with offline content
  • Web Push notifications
  • Sync to handle offline User DML request( REST API only)

Serve your application with offline content when network is down. Serve server rendered html when network is up.

serve your application with offline caches via Fetch event ( fetch event fires each time a request is sent to the server), what we do here is tapping into the web page request, download all requested resouces used for browser to render a page and eventually store them into cache use cache api. I named as test.

self.addEventListener("fetch", (event) => {
  // "fetch" hook starts
  event.respondWith(
    (async () => {
      let cacheName = "test";
      let cache;
      // Try to get from the cache first
      if (cacheName) {
        cache = await caches.open(cacheName);
        // for html I always want to get from server unless there is no network
        if (event.request.url.indexOf("session=") < 0) {
          const response = await cache.match(event.request);
          if (response) {
            return response;
          }
        }
      }
      // Then get from network
      try {
        const response = await fetch(event.request);
        if (response.ok && cacheName) {
          try {
            const resClone = response.clone();
            cache.put(event.request, resClone);
          } catch (error) {
            console.warn(error);
          }
        }
        // Return resource from network
        return response;
      } catch (error) {
         // for apex page where i do have cache , i serve the cache
        const response = await cache.match(event.request);
        if (response) {
          return response;
        } else {
           // for apex page where i DO NOT have cache, i serve dummy html 
          if (event.request.mode === "navigate") {
            const offlinePage =
              '<div style="height:100%;text-align:center; font-size:30px;padding-top: 50%;"> Your Device Is Offline </div>';
            return new Response(offlinePage, {
              headers: { "Content-Type": "text/html" },
            });
          } else {
            return new Response();
          }
        }
      }
    })()
  );
});
let apex = {};
apex.sw = {};
apex.sw.CORE_CACHE_MATCHER = '#APEX_FILES#';
apex.sw.CORE_CACHE_PREFIX = "APEX-CORE-";
apex.sw.CORE_CACHE_NAME = apex.sw.CORE_CACHE_PREFIX + '#APEX_VERSION#';

apex.sw.APP_CACHE_MATCHER = "&FB_FLOW_ID./files/static/v";
apex.sw.APP_CACHE_PREFIX = "APEX-APP-&FB_FLOW_ID.-v";

apex.sw.appCacheNeedsCleanup = true;

apex.sw.cleanAPEXCaches = () => {
  caches.keys().then(cacheNames => Promise.all(
    cacheNames.map(cacheName => {
      if (cacheName.startsWith(apex.sw.CORE_CACHE_PREFIX) && cacheName !== apex.sw.CORE_CACHE_NAME) {
        return caches.delete(cacheName);
      }
    })
  ));
};

apex.sw.cleanAppCaches = (appCacheName) => {
  if (appCacheNeedsCleanup) {
    appCacheNeedsCleanup = false;
    caches.keys().then(cacheNames => Promise.all(
      cacheNames.map(cacheName => {
        if (cacheName.startsWith(apex.sw.APP_CACHE_PREFIX) && cacheName !== appCacheName) {
          return caches.delete(cacheName);
        }
      })
    ));
  }
};



self.addEventListener("fetch", (event) => {
  event.respondWith(
    (async () => {
      let cacheName;
      if (
        event.request &&
        event.request.url &&
        event.request.url.indexOf(apex.sw.CORE_CACHE_MATCHER) >= 0
      ) {
        cacheName = apex.sw.CORE_CACHE_NAME;
      } else if (
        event.request &&
        event.request.url &&
        event.request.url.indexOf(apex.sw.APP_CACHE_MATCHER) >= 0
      ) {
        const fileVersion = event.request.url
          .split(apex.sw.APP_CACHE_MATCHER)
          .pop()
          .split("/")[0];
        cacheName = apex.sw.APP_CACHE_PREFIX + fileVersion;
        apex.sw.cleanAppCaches(cacheName);
      }
      let cache;
      // Try to get from the cache first
      if (cacheName) {
        cache = await caches.open(cacheName);
        const response = await cache.match(event.request);
        if (response) {
          return response;
        }
      }
      // Then get from network
      try {
        const response = await fetch(event.request);
        // Clone response to put in cache
        if (response.ok && cacheName) {
          try {
            const resClone = response.clone();
            cache.put(event.request, resClone);
          } catch (error) {
            console.warn(error);
          }
        }
        // Return ressource from network
        return response;
      } catch (error) {
        if (event.request.mode === "navigate") {
          const offlinePage = "APEX.PWA.OFFLINE";
          return new Response(offlinePage, {
            headers: { "Content-Type": "text/html" },
          });
        } else {
          return new Response();
        }
      }
    })()
  );
});

Web Push Notifications

using firebase

Leave a Reply Cancel reply

You must be logged in to post a comment.

Recent Posts

  • Oracle APEX cheat sheets (on going)
  • Second Take on React Native – ReadCast
  • Switch Between APEX builder Authentication schemes
  • Use BitBucket Pipelines to Automate Oracle APEX deployment
  • MARKDown TEST

Categories

  • APEX
  • C#
  • chatgpt
  • Docker
  • JavaScript
  • Linux
  • Nginx
  • OCI
  • Oracle APEX
  • Oralce DB
  • PL/SQL
  • Python
  • Uncategorized
©2025 ChaoTic | Powered by SuperbThemes
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept All”, you consent to the use of ALL the cookies. However, you may visit "Cookie Settings" to provide a controlled consent.
Cookie SettingsAccept All
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytics
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Others
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
SAVE & ACCEPT
Scroll Up