When the Ajax "GET" request is made to the intra-service, the CMS service worker will respond with an "OK" even when offline

Hello there,

We are currently utilizing an open-source CMS that recently received an upgrade with a new feature - a javascript serviceworker implementation to manage all requests.

This CMS includes workflow forms where users engage (created by us). During the design phase, you have the ability to insert custom HTML elements known as special snippet elements, which can contain any JS, HTML, and CSS code.

We developed a button that triggers a $.get() call to a Python Flask server hosted online on the same machine-instance (https://localhost:5000/).

In the earlier version of the CMS without the serviceworker, if we stopped the Flask server, our $.get() call upon button click would result in an expected error response (404/500), allowing us to handle it using the ".fail()" jQuery function. When the Flask server was active, we handled it within the ".done()" segment.

However, after the upgrade, every scenario is ending up in the ".done()" segment. This is because even when it should fail due to the offline Flask server, the serviceworker intercepts it and falsely resolves it with an OK status (200):

https://example.com/image1.png https://example.com/image2.png

When in reality, the server is down:

https://example.com/image3.png

So, how can we prevent the serviceworker from intercepting the ajax call without being able to modify the serviceworker code itself? Is there a way to extend it so that the "fetch" listener can validate against the "/api" URL and just "return;"?

This is the exact point where Chrome indicates the issue:

https://example.com/image4.png

And this is a snippet of the function code:

Your modified function code here...

Thank you for your assistance!

Answer №1

Well, looks like no one has an answer. That's okay.

If anyone is curious, I can explain how I managed to solve the issue. In essence, it involved a bit of a "rodeo":

  • I couldn't modify the functionality of the third-party serviceworker, so I had to resort to preventing caching every time by using "cache: false" on each ajax call.
  • This tactic ensured that the serviceworker could not provide an "offline" version of the page, and I wouldn't receive the cached (but real) page even when online.
  • Instead of relying on separate .done() and .fail() functions, everything was directed to the "done()" function. Despite all responses being 200 OK, I verified the response content within this function, reminiscent of older techniques. This meant that if the server was online, it would return a genuine 200 OK with the correct expected response. However, if offline, due to the use of the "cache: false" parameter, even with a successful SW status of 200, the response wouldn't contain the anticipated text. As a result, the condition failed, allowing me to identify it as a true 404 error.

Simple, but effective!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the reason for the request body being undefined?

I have a JavaScript file named index.js that contains: const express = require('express'); const bodyParser = require('body-parser'); const cors = require('cors'); const db = require('./db'); const movieRouter = re ...

Django doesn't receive the Ajax post data

Currently, I am in the process of developing a Django application where I am utilizing jQuery and Ajax for handling a POST request. However, I am encountering difficulties with sending data from the frontend to the server using Ajax. views.py def add_new_ ...

Issue with Angular's ngOnChanges Lifecycle Hook Preventing Function ExecutionWhen attempting to run a function within Angular's ngOn

In the midst of my evaluation process to ensure that specific values are properly transmitted from one component to another using Angular's custom Output() and EventEmitter(), I am encountering some issues. These values are being sent from the view of ...

Retrieve all elements from an array that has been imported through the fs module

To extract the values from the array jsonParsed.students[actStudent].evnetsPartaken, which may look like [1,2,0] (note: check attachments for full data), I need a way to reference each value in correlation to the event number in the configEvents JSON array ...

Save the array as a variable in your JavaScript code so that you can easily access it

I am currently working on generating a list only when a specific page is visited using JS/jQuery. I then need to be able to access this list when navigating to other pages and retrieve the variables within it. How can I effectively store this list? Initia ...

What is the best way to retrieve the current value of a React useState hook within a setInterval function while using Highcharts

import Error from 'next/error' import React, { useState, useEffect } from 'react' import Highcharts from 'highcharts' import HighchartsReact from 'highcharts-react-official' function generateChart() { const [co ...

"The `Head.js` method called `.js()` allows for dynamic

Recently, I came across some code that was passed down to me from work, and it involves making an api call using head.js: head.js( { 'application' : 'someurl.com/foo.js' }); I'm curious if this actually registers the javascript a ...

Unexpected JSON token error occurs in jQuery when valid input is provided

I encountered an error that I'm struggling to pinpoint. The issue seems to be related to the presence of the ' symbol in the JSON data. After thoroughly checking, I am positive that the PHP function json_encode is not responsible for adding this ...

The jQuery load() callback triggering a POST request unexpectedly instead of a GET request

Utilizing jQuery's load() method, I am attempting to insert a page fragment into a new page. It appears that this can only be achieved with load(), as get() does not support page fragments. Upon completion of the load, it is necessary for me to invok ...

Creating a front-end angular application with npm and grunt commands

Hello there! This marks my first question, so please bear with me if it's a bit unclear. I'm fairly new to application development and currently working on an AngularJS app using Grunt. My query revolves around the build process I've execut ...

React Object Array Item State management

After spending a considerable amount of time on this task, I have been trying to achieve the functionality of changing a checked value upon checkbox click. Here is how my initial state is set up: const [todoList, setTodoList] = useState({ foundation: ...

Guide to implement a confirmation box in PHP

I recently set up a Joomla article and integrated the Sourcerer Joomla extension to include JavaScript and PHP in my project. Currently, I am developing a course purchase site where users can buy courses from owners and credits are deducted upon every purc ...

Why isn't the data showing in the controller for Cakephp 2.0 Ajax post requests?

I have been working on a CakePHP web application and have created a small Ajax function in the default CTP file setup. I am now trying to send value data to another controller function. In the Chrome browser network, it shows that Ajax is posting to the de ...

Navigate to a different subdomain and place a cookie on the newly redirected subdomain

The version of Express is 4.x NodeJS is running on version 12.x At a.example.com, I have a listener for the GET method that redirects to b.example.com and sets a cookie on b.example.com. app.get('/foo', (req, res) => { res.cookie(' ...

Ways to stop click propagation in the case of a parent anchor link containing a button among its children

Every time I click on the Link parent, it triggers a click event on the button as well. I want these events to be independent. <Link className="product-item__link" to={`/products/${product.category}/${product.id}`} > <div className ...

Shutting down browser with WebdriverIO and launching a new one

I am facing a challenge in my application testing process. I need to simulate a scenario where I close the browser and session, then open a new browser and session to check if the previously entered data can be successfully recalled by passing certain laun ...

Unlocking the potential of Three.js with mouse picking

I am looking to implement object picking in the following code snippet: var Three = new function () { this.scene = new THREE.Scene() this.camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 1000) this.camera.po ...

When the enter key is pressed, the form will be submitted and the results will be displayed in a modal window without

Behold, my unique form! It lacks the traditional <form></form> tags. <input id="query" name="query" style="font-size: 18pt" id="text" data-email="required" type="text" placeholder="Search Here from <?php echo $row['no']."+"; ?& ...

A guide to updating property values in an array of objects in JavaScript while ensuring they remain in consecutive order

I am dealing with an array of objects called list. The parameters rid and rorder are provided for processing. -> 1. Whenever the value of rid matches the id in the list, the itemorder is updated with the value of rorder. -> 2. In the updated list p ...

Use Express JS to enable users to upload their profile picture either during account creation or on the registration form

Just starting out with node js, I'm working on a basic app where users can create accounts with their details. I've implemented mongo dB as the backend to store user information such as names and emails. Once an account is created, users are dir ...