简体   繁体   English

ServiceWorker Notification Click - 如何刷新页面并跳转到锚链接?

[英]ServiceWorker Notification Click - How can I refresh a page AND jump to an anchor link?

Basically, on my 'notificationclick' event, I am trying to open the notifications URL.基本上,在我的“notificationclick”事件中,我试图打开通知 URL。 It works perfectly for normal URLs.它非常适合普通 URL。 But if the URL has an anchor tag (#), it will then just try to jump to the specified anchor (which doesn't exist yet.)但是如果 URL 有一个锚标记 (#),它就会尝试跳转到指定的锚(目前还不存在)。

I can easily cut out the anchor part of the url, and that works for the base page, it will then refresh the page successfully, but it wont jump to the comment.我可以很容易地删除 url 的锚点部分,这适用于基页,然后它会成功刷新页面,但不会跳转到评论。

So i tried doing this:所以我试着这样做:

                if (cleanedClientUrl === cleanedUrl && 'focus' in client) {
                    //focus and reload the window that has this page open
                    client.focus();

                    //if the url had a # in it, first navigate to the cleaned url (otherwise it wont refresh)
                    if (url.indexOf('#'))
                        client.navigate(cleanedUrl);

                    client.navigate(url);

                    return;
                }

Which I was hoping would first redirect it to the URL without the cleanedUrl (without the anchor), and then navigate to the original url, which include the anchor, to make it jump down.我希望首先将它重定向到没有 cleanedUrl(没有锚点)的 URL,然后导航到包含锚点的原始 url,使其跳转。 But it seems the second client.navigate cancels the first one.但似乎第二个 client.navigate 取消了第一个。

Is there any way to wait until the first one is loaded, or tell the page to reload even if there's an anchor specified in the URL?有什么方法可以等到第一个加载完成,或者即使在 URL 中指定了锚点也可以告诉页面重新加载?

Here is my full code:这是我的完整代码:

//user clicked / tapped a push notification
self.addEventListener('notificationclick', function(event) {
    const clickedNotification = event.notification;
    clickedNotification.close();

    //exit if the url could not be found
    if (!event.notification.data || !event.notification.data.url) return;

    //get url from event
    var url = event.notification.data.url;
    //if the url contains a #, remove it and everything after it
    var cleanedUrl = url.indexOf('#') ? url.substring(0, url.indexOf('#')) :url;

    event.waitUntil(
        self.clients.matchAll({type: 'window', includeUncontrolled: true}).then( windowClients => {
            console.log('opening window', windowClients.length, 'windows')
            // Check if there is already a window/tab open with the target URL
            for (var i = 0; i < windowClients.length; i++) {
                var client = windowClients[i];

                //if the page url contains a #, remove it and everything after it
                var cleanedClientUrl;
                if (client.url.indexOf('#') !== -1)
                    cleanedClientUrl = client.url.substring(0, client.url.indexOf('#'));
                else cleanedClientUrl = client.url;

                // if the cleaned URLs match
                if (cleanedClientUrl === cleanedUrl && 'focus' in client) {
                    //focus and reload the window that has this page open
                    client.focus();

                    //if the url had a # in it, first navigate to the cleaned url (otherwise it wont refresh)
                    if (url.indexOf('#'))
                        client.navigate(cleanedUrl);

                    client.navigate(url);

                    return;
                }
            }
            // If not, then open the target URL in a new window/tab.
            if (self.clients.openWindow) {
                return self.clients.openWindow(url);
            }
        })
    );
});

Could you quickly jump to the anchor and then immediately reload the page?你能快速跳转到锚点然后立即重新加载页面吗?

main JS thread主 JS 线程

window.location = '#my-anchor-value';
window.location.reload();

ServiceWorker ServiceWorker

if you need to chain navigation in a SW, you'll need to await the promise completion of the first navigate()如果您需要在 SW 中链接导航,则需要等待第一个navigate()的承诺完成

if (url.indexOf('#')) {
  return client.navigate(cleanedUrl)
  .then(() => client.navigate(url));
} else {
  return client.navigate(url);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM