PWA - How to pass parameters to another page while offiline?

Asked

Viewed 47 times

0

I am developing a PWA application that will work mainly in offiline mode, I have created and implemented virtually everything, indexedDB, service-worker, etc... I am trying to pass some parameters to another page using the GET method and to read the values in java script I am using this function:

For example, the page Indexed.php calls the page Parameters.php with some parameters:

function $_GET()
{
    var parts = window.location.search.substr(1).split("&");
    var GET = {};

    for (var i = 0; i < parts.length; i++) {
        var temp = parts[i].split("=");
        GET[decodeURIComponent(temp[0])] = decodeURIComponent(temp[1]); 
    }

    return GET;
}

And the page Parameters.php reads these values and performs some actions.

window.location.href = "/parameters.php?id=1";

The problem appears when I’m offiline and I try to load these pages, it goes wrong because the Urls are not the same as the ones that went pro cache. I have tried several ways to ignore query strings but without success. I wonder if there are some other alternative to do this, and what are the most recommended methods of passing parameters to other pages while offiline.

  • Welcome to Stack Overflow in English. Please click edit and translate the question.

1 answer

1

The way I managed to get around this is by passing the parameters via hash

Ex: /page/#page=book&id=1

function get_hash() {
    var GET = {};
    try {
        var hash = window.location.hash.split('#')[1];
        var partes = hash.split('&');
        partes.forEach(function (parte) {
            if (parte != '') {
                var chaveValor = parte.split('=');
                GET[chaveValor[0]] = chaveValor[1];
            }
        });
    } catch (e) {
        console.log(e)
    }
    return GET;
}

With this you can mount the request to the server with the data provided via parameters. If you are offline rescues the information from indexDb

Browser other questions tagged

You are not signed in. Login or sign up in order to post.