我尝试为我的serviceworker配置设置我的PUBLIC_URL,然后捆绑我在webpack.config.js中设置我的PUBLIC_URL的应用程序:
module.exports = (env) => {
const isProduction = env.production === true;
const isDevelopment = env.development === true;
process.env.NODE_ENV = isProduction ? 'production' : isDevelopment && 'development';
process.env.PUBLIC_URL = '/';
return {...config}
}
我什至用.env
创建了一个PUBLIC_URL=/
文件,但是当我在service.worker中注册PUBLIC_URL时,似乎没有任何作用。
export function register(config) {
console.log(process.env.PUBLIC_URL)
if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) {
const publicUrl = new URL(process.env.PUBLIC_URL, window.location.href);
if (publicUrl.origin !== window.location.origin) {
return;
}
window.addEventListener('load', () => {
const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`;
if (isLocalhost) {
checkValidServiceWorker(swUrl, config);
navigator.serviceWorker.ready.then(() => {
console.log(
'This web app is being served cache-first by a service worker.');
});
} else {
registerValidSW(swUrl, config);
}
});
}
}
我正在尝试使用webpack-dev-server运行该应用程序,还尝试通过生成dist文件夹并向上打开index.html文件夹来对其进行尝试。有人知道原因吗?