Search code examples
javascriptvue.jsprogressive-web-apps

PWA generic start_url


I have vue app which is also PWA. The PWA works just fine as intended but the problem is that I am using the generic paths for my web app. That means to open the correct page I am using the path /test/${user} . That means when user installs app he is automatically redirected to start path "website.com" not "website.com/test/uniqueid". So my question is - is it possible to set up start_url when user installs the PWA so when he launches the app it redirects him straight to "website.com/test/uniqueid"


Solution

  • I don't know if it is going to help for someone else but I found this hacky solution which works but it has some of its flaws - for example if the user deletes his localstorage.

    I added an event listener for 'appinstalled' basically checking when user installs the PWA and when he does I setup localStorage variable

    window.addEventListener("appinstalled", () => {
                localStorage["istallURL"] = window.location.pathname;
            });
    

    After that (In my case) in router.js inside root path I am checking if this localStorage value exists if it does I redirect user to saved path from localhost variable

    {
    path: "/",
            name: "redirect",
            beforeEnter: (to, from, next) => {
                const installedFrom = localStorage["installURL"];
                if (installedFrom) {
                    window.location.replace(installURL);
                } else {
                    window.location.href = "https://youtube.com/";
                }
                next();
            },
    }
    

    Should be easy to adapt it for other frameworks or non framework app, do the same thing before mounting your application :)