在服务器端检测来自 PWA 的 webrequest [英] Detect webrequest coming from PWA on the server side

查看:47
本文介绍了在服务器端检测来自 PWA 的 webrequest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在向网络应用添加 PWA 支持.当应用程序用作 PWA 而不是在 Safari(或其他浏览器)中以老式方式使用时,我想在 iOS 界面中添加一个后退按钮.

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).

我知道我可以通过 JS 检测到这一点 - 所以我可以在运行时显示/隐藏后退按钮,但我想知道是否有任何方法可以在 在服务器端检测strong> 已经(apache/php).我想会有更多的用例来提供略有不同的内容.

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.

我想我可以设置一个 cookie,但我想确保没有什么比使用更容易/更明显的东西了,例如一些新的标题.

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.

推荐答案

在此期间找到了除 cookie 之外的另一种方法.通过 Service Worker 发送自定义 http 标头.

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']);

这篇关于在服务器端检测来自 PWA 的 webrequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆