Desktop Firefox在缓存使用中将用户代理替换为service-worker.js。

huangapple go评论57阅读模式
英文:

Desktop Firefox replaces user-agent in cache usage with service-worker.js

问题

以下是已经翻译好的内容:

用户代理 (User-Agent) 在我的普通桌面版 Firefox 上:

Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/113.0

当我使用 service-worker.js 缓存一些文件时,Firefox 发送的用户代理:

Mozilla/5.0 (Android 4.4; Mobile; rv:109.0) Gecko/109.0 Firefox/109.0

注意 1:我在普通桌面网站浏览中得到这个结果。

注意 2:当我在开发者控制台检查网络流量时,用户代理正常发送。然而,当我在服务器端测试时,它会返回像是移动浏览器的结果。

注意 3:当我使用 Ctrl+F5 刷新页面时,它会显示正确的用户代理一次。

我的 service-worker.js 文件:

let version;
version = 4;

// 这是具有联合离线体验的 service worker(离线页面 + 页面的离线副本)

const CACHE = "system-v" + version;

importScripts('https://storage.googleapis.com/workbox-cdn/releases/5.1.2/workbox-sw.js');

// TODO:用正确的离线回退页面替换以下内容,例如:const offlineFallbackPage = "offline.html";
const offlineFallbackPage = "pwa-offline/?v=" + version;

self.addEventListener("message", (event) => {
	if (event.data && event.data.type === "SKIP_WAITING") {
		self.skipWaiting();
	}
});

self.addEventListener('install', async (event) => {
	event.waitUntil(
		caches.open(CACHE)
			.then((cache) => cache.add(offlineFallbackPage))
	);
});

if (workbox.navigationPreload.isSupported()) {
	workbox.navigationPreload.enable();
}

workbox.routing.registerRoute(
	new RegExp('/*\.(js|css|jpe?g|png|gif|webp|svg|ico)'),
	new workbox.strategies.StaleWhileRevalidate({
		cacheName: CACHE
	})
);

self.addEventListener('fetch', (event) => {
	if (event.request.mode === 'navigate') {
		event.respondWith((async () => {
			try {
				const preloadResp = await event.preloadResponse;

				if (preloadResp) {
					return preloadResp;
				}

				const networkResp = await fetch(event.request);
				return networkResp;
			} catch (error) {

				const cache = await caches.open(CACHE);
				const cachedResp = await cache.match(offlineFallbackPage);
				return cachedResp;
			}
		})());
	}
});

什么导致了这个问题,我该如何解决?

已解决:

问题已经被检测出来。这是一个附加组件的问题。官方的 side view 附加组件在使用 service worker 时会注入用户代理。

英文:

The user-agent of my normal desktop Firefox:

Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/113.0

The user agent that Firefox sends when I cache some files with service-worker.js:

Mozilla/5.0 (Android 4.4; Mobile; rv:109.0) Gecko/109.0 Firefox/109.0

Note 1: I get this result in normal desktop website browsing.

Note 2: When I inspect the network traffic in the developer console, the user-agent is sent normally. However, when I test it on the server side, it gives results as if it is a mobile browser.

Note 3: When I refresh the page with ctrl+f5, it shows the correct user-agent for once.

My service-worker.js file:

let version;
version = 4;


// This is the service worker with the combined offline experience (Offline page + Offline copy of pages)

const CACHE = "system-v"+version;

importScripts('https://storage.googleapis.com/workbox-cdn/releases/5.1.2/workbox-sw.js');

// TODO: replace the following with the correct offline fallback page i.e.: const offlineFallbackPage = "offline.html";
const offlineFallbackPage = "pwa-offline/?v="+version;

self.addEventListener("message", (event) => {
	if (event.data && event.data.type === "SKIP_WAITING") {
		self.skipWaiting();
	}
});

self.addEventListener('install', async (event) => {
	event.waitUntil(
		caches.open(CACHE)
			.then((cache) => cache.add(offlineFallbackPage))
	);
});

if (workbox.navigationPreload.isSupported()) {
	workbox.navigationPreload.enable();
}

workbox.routing.registerRoute(
	new RegExp('/*\.(js|css|jpe?g|png|gif|webp|svg|ico)'),
	new workbox.strategies.StaleWhileRevalidate({
		cacheName: CACHE
	})
);

self.addEventListener('fetch', (event) => {
	if (event.request.mode === 'navigate') {
		event.respondWith((async () => {
			try {
				const preloadResp = await event.preloadResponse;
				
				if (preloadResp) {
					return preloadResp;
				}
				
				const networkResp = await fetch(event.request);
				return networkResp;
			} catch (error) {
				
				const cache = await caches.open(CACHE);
				const cachedResp = await cache.match(offlineFallbackPage);
				return cachedResp;
			}
		})());
	}
});

What is causing this problem and how can I solve it?

Solved:

Problem has been detected. It's a add-on problem. Official side view add-on injects user agent when using service worker.

答案1

得分: 0

问题已检测到。这是一个附加问题。官方 侧边视图 附加组件在使用服务工作线程时会注入用户代理。

英文:

Problem has been detected. It's a add-on problem. Official side view add-on injects user agent when using service worker.

huangapple
  • 本文由 发表于 2023年5月28日 04:53:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76348989.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定