Javascript 即使我在代码中更改了字符串的值,字符串的值也不会更改
我创建了一个简单的react应用程序,用户可以像instagram一样上传帖子。如果一个用户发布了一篇新文章,所有用户都会收到一条类似“添加了新文章”的消息。如果我将值从“new post”更改为“new notification”,它仍然显示“new post”,我不知道为什么。有人能帮忙清理一下吗 index.js sw.jsJavascript 即使我在代码中更改了字符串的值,字符串的值也不会更改,javascript,reactjs,progressive-web-apps,Javascript,Reactjs,Progressive Web Apps,我创建了一个简单的react应用程序,用户可以像instagram一样上传帖子。如果一个用户发布了一篇新文章,所有用户都会收到一条类似“添加了新文章”的消息。如果我将值从“new post”更改为“new notification”,它仍然显示“new post”,我不知道为什么。有人能帮忙清理一下吗 index.js sw.js 可能是在浏览器中按住Shift+F5键?胡乱猜测……也许给我们看看你的服务人员的职业生涯idk@NumLock不幸的是,它不起作用:(@Ifaruki添加:)@cbr
可能是在浏览器中按住Shift+F5键?胡乱猜测……也许给我们看看你的服务人员的职业生涯idk@NumLock不幸的是,它不起作用:(@Ifaruki添加:)@cbra只有标题是错误的,或者整个消息是错误的?可能是浏览器中的Shift+F5?胡乱猜测……也许给我们看看你的服务人员的职业生涯idk@NumLock不幸的是,它不起作用:(@Ifaruki补充道:)@cbra只有标题是错误的还是整个消息?
webpush.sendNotification(pushConfig, JSON.stringify({
title: 'New Post ',
content: 'New Post added!',
openUrl: '/help'
}))
.catch(function (err) {
console.log(err);
})
});
importScripts("/src/js/idb.js");
importScripts("/src/js/utility.js");
var CACHE_STATIC_NAME = "static-v30";
var CACHE_DYNAMIC_NAME = "dynamic-v2";
var STATIC_FILES = [
"/",
"/index.html",
"/offline.html",
"/src/js/app.js",
"/src/js/feed.js",
"/src/js/idb.js",
"/src/js/promise.js",
"/src/js/fetch.js",
"/src/js/material.min.js",
"/src/css/app.css",
"/src/css/feed.css",
"/src/images/main-image.jpg",
"https://fonts.googleapis.com/css?family=Roboto:400,700",
"https://fonts.googleapis.com/icon?family=Material+Icons",
"https://cdnjs.cloudflare.com/ajax/libs/material-design-lite/1.3.0/material.indigo-pink.min.css"
];
self.addEventListener("install", function(event) {
console.log("[Service Worker] Installing Service Worker ...", event);
event.waitUntil(
caches.open(CACHE_STATIC_NAME).then(function(cache) {
console.log("[Service Worker] Precaching App Shell");
cache.addAll(STATIC_FILES);
})
);
});
self.addEventListener("activate", function(event) {
console.log("[Service Worker] Activating Service Worker ....", event);
event.waitUntil(
caches.keys().then(function(keyList) {
return Promise.all(
keyList.map(function(key) {
if (key !== CACHE_STATIC_NAME && key !== CACHE_DYNAMIC_NAME) {
console.log("[Service Worker] Removing old cache.", key);
return caches.delete(key);
}
})
);
})
);
return self.clients.claim();
});
function isInArray(string, array) {
var cachePath;
if (string.indexOf(self.origin) === 0) {
// request targets domain where we serve the page from (i.e. NOT a CDN)
console.log("matched ", string);
cachePath = string.substring(self.origin.length); // take the part of the URL AFTER the domain (e.g. after localhost:8080)
} else {
cachePath = string; // store the full request (for CDNs)
}
return array.indexOf(cachePath) > -1;
}
self.addEventListener("fetch", function(event) {
var url = "https://pwagram-68415.firebaseio.com/posts";
if (event.request.url.indexOf(url) > -1) {
event.respondWith(
fetch(event.request).then(function(res) {
var clonedRes = res.clone();
clearAllData("posts")
.then(function() {
return clonedRes.json();
})
.then(function(data) {
for (var key in data) {
writeData("posts", data[key]);
}
});
return res;
})
);
} else if (isInArray(event.request.url, STATIC_FILES)) {
event.respondWith(caches.match(event.request));
} else {
event.respondWith(
caches.match(event.request).then(function(response) {
if (response) {
return response;
} else {
return fetch(event.request)
.then(function(res) {
return caches.open(CACHE_DYNAMIC_NAME).then(function(cache) {
// trimCache(CACHE_DYNAMIC_NAME, 3);
cache.put(event.request.url, res.clone());
return res;
});
})
.catch(function(err) {
return caches.open(CACHE_STATIC_NAME).then(function(cache) {
if (event.request.headers.get("accept").includes("text/html")) {
return cache.match("/offline.html");
}
});
});
}
})
);
}
});
self.addEventListener("sync", function(event) {
console.log("[Service Worker] Background syncing", event);
if (event.tag === "sync-new-posts") {
console.log("[Service Worker] Syncing new Posts");
event.waitUntil(
readAllData("sync-posts").then(function(data) {
for (var dt of data) {
fetch(
"https://us-central1-pwagram-68415.cloudfunctions.net/storePostData",
{
method: "POST",
headers: {
"Content-Type": "application/json",
Accept: "application/json"
},
body: JSON.stringify({
id: dt.id,
title: dt.title,
location: dt.location,
image:
"https://firebasestorage.googleapis.com/v0/b/pwagram-68415.appspot.com/o/sf-boat.jpg?alt=media&token=bb1cea7e-45bd-40e5-8bf6-109892f54601"
})
}
)
.then(function(res) {
console.log("Sent data", res);
if (res.ok) {
res.json().then(function(resData) {
deleteItemFromData("sync-posts", resData.id);
});
}
})
.catch(function(err) {
console.log("Error while sending data", err);
});
}
})
);
}
});
self.addEventListener("notificationclick", function(event) {
var notification = event.notification;
var action = event.action;
console.log(notification);
if (action === "confirm") {
console.log("Confirm was chosen");
notification.close();
} else {
console.log(action);
event.waitUntil(
clients.matchAll().then(function(clis) {
var client = clis.find(function(c) {
return c.visibilityState === "visible";
});
if (client !== undefined) {
client.navigate(notification.data.url);
client.focus();
} else {
clients.openWindow(notification.data.url);
}
notification.close();
})
);
}
});
self.addEventListener("notificationclose", function(event) {
console.log("Notification was closed", event);
});
self.addEventListener("push", function(event) {
console.log("Push Notification received", event);
var data = {
title: "New!",
content: "Something new happened!",
openUrl: "/"
};
if (event.data) {
data = JSON.parse(event.data.text());
}
var options = {
body: data.content,
icon: "/src/images/icons/app-icon-96x96.png",
badge: "/src/images/icons/app-icon-96x96.png",
data: {
url: data.openUrl
}
};
event.waitUntil(self.registration.showNotification(data.title, options));
});