Detect webrequest coming from PWA on the server side

大憨熊 提交于 2021-02-19 07:07:26

问题


I'm adding PWA support to a web app. I want to add a back-button to the interface for iOS when the app is used as a PWA but not when used in the old fashioned way in Safari (or another browser).

I know I can detect this via JS - so I could just show/hide the back-button at runtime, but I would like to know if there is any way to detect in on the server-side already (apache/php). I guess there will be more use-cases coming up to serve slightly different content.

I guess I could set a cookie, but I want to make sure there is nothing more easy/obvious to use, some new header for example.


回答1:


Found one more way in the meantime to do it, apart from cookies. Sending a custom http header via a service worker.

app.js:

navigator.serviceWorker.register('/sw.js').then(registration => {
    if(registration.active) {
        const sw = registration.active;
        sw.postMessage({
            'is_pwa': is_pwa()
        });
    }
});

sw.js:

var myHeader = new Headers(event.request.headers);
    if (is_pwa) {
        myHeader.append('ISPWA', is_pwa ? is_pwa : "");
    }
    var config = {
        headers: myHeader
    };
    if(navigator.onLine !== false) {
        event.respondWith(fetch(request).catch(function(error) {
            fetch(request, config).catch(function() {
            [...]

php:

var_dump($_SERVER['HTTP_ISPWA']);


来源:https://stackoverflow.com/questions/54905002/detect-webrequest-coming-from-pwa-on-the-server-side

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!