Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用webpack脱机插件从服务工作者中排除URL_Javascript_Reactjs_Webpack_Service Worker_React Boilerplate - Fatal编程技术网

Javascript 使用webpack脱机插件从服务工作者中排除URL

Javascript 使用webpack脱机插件从服务工作者中排除URL,javascript,reactjs,webpack,service-worker,react-boilerplate,Javascript,Reactjs,Webpack,Service Worker,React Boilerplate,我目前正在使用AWS Cloudfront在同一个域上设置2个应用程序,以管理应用程序之间的路由。这两个应用程序中的一个是React应用程序,它基于React样板文件,使用离线插件设置服务人员。默认情况下,react应用程序的路由在Cloudfront中被列为白名单,回退路由是其他应用程序的路由(例如/blog) 问题来自React应用程序的服务人员,他们在调用另一个应用程序的路由时返回默认404 我猜我的url应该重定向到另一个应用程序,基于Cloudfront重定向(例如/blog,它是wo

我目前正在使用AWS Cloudfront在同一个域上设置2个应用程序,以管理应用程序之间的路由。这两个应用程序中的一个是React应用程序,它基于React样板文件,使用离线插件设置服务人员。默认情况下,react应用程序的路由在Cloudfront中被列为白名单,回退路由是其他应用程序的路由(例如/blog)

问题来自React应用程序的服务人员,他们在调用另一个应用程序的路由时返回默认404

我猜我的url应该重定向到另一个应用程序,基于Cloudfront重定向(例如/blog,它是wordpress)被服务人员截取,然后从主应用程序返回404

我尝试了webpack离线插件提供的一些选项,但我对服务人员的理解不是最佳的。。。 我尝试过使用
响应策略
排除
,ServiceWorkers
缓存名
,但没有成功

new OfflinePlugin({
      publicPath: '/',
      appShell: '/',
      excludes: ['.htaccess'],
      caches: {
        main: [':rest:'],
        additional: ['*.chunk.js'],
      },
      safeToUseOptionalCaches: true,
    }),
在你的生活中使用它

app.js

utils/serviceWorker.js

在你的生活中使用它

app.js

utils/serviceWorker.js

import * as serviceWorker from 'utils/serviceWorker';

serviceWorker.unregister();
// In production, we register a service worker to serve assets from local cache.

// This lets the app load faster on subsequent visits in production, and gives
// it offline capabilities. However, it also means that developers (and users)
// will only see deployed updates on the "N+1" visit to a page, since previously
// cached resources are updated in the background.


// This link also includes instructions on opting out of this behavior.

const isLocalhost = Boolean(
  window.location.hostname === 'localhost' ||
    // [::1] is the IPv6 localhost address.
    window.location.hostname === '[::1]' ||
    // 127.0.0.1/8 is considered localhost for IPv4.
    window.location.hostname.match(
      /^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/,
    ),
);

export function register(config) {
  if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) {
    // The URL constructor is available in all browsers that support SW.
    const publicUrl = new URL(process.env.PUBLIC_URL, window.location);
    if (publicUrl.origin !== window.location.origin) {
      // Our service worker won't work if PUBLIC_URL is on a different origin
      // from what our page is served on. This might happen if a CDN is used to
      // serve assets; see https://github.com/facebook/create-react-app/issues/2374
      return;
    }

    window.addEventListener('load', () => {
      const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`;

      if (isLocalhost) {
        // This is running on localhost. Let's check if a service worker still exists or not.
        checkValidServiceWorker(swUrl, config);

        // Add some additional logging to localhost, pointing developers to the
        // service worker/PWA documentation.
        navigator.serviceWorker.ready.then(() => {
          console.log(
            'This web app is being served cache-first by a service ' +
              'worker. To learn more, visit ',
          );
        });
      } else {
        // Is not local host. Just register service worker
        registerValidSW(swUrl, config);
      }
    });
  }
}

function registerValidSW(swUrl, config) {
  navigator.serviceWorker
    .register(swUrl)
    .then(registration => {
      registration.onupdatefound = () => {
        const installingWorker = registration.installing;
        installingWorker.onstatechange = () => {
          if (installingWorker.state === 'installed') {
            if (navigator.serviceWorker.controller) {
              // At this point, the old content will have been purged and
              // the fresh content will have been added to the cache.
              // It's the perfect time to display a "New content is
              // available; please refresh." message in your web app.
              console.log('New content is available; please refresh.');

              // Execute callback
              if (config.onUpdate) {
                config.onUpdate(registration);
              }
            } else {
              // At this point, everything has been precached.
              // It's the perfect time to display a
              // "Content is cached for offline use." message.
              console.log('Content is cached for offline use.');

              // Execute callback
              if (config.onSuccess) {
                config.onSuccess(registration);
              }
            }
          }
        };
      };
    })
    .catch(error => {
      console.error('Error during service worker registration:', error);
    });
}

function checkValidServiceWorker(swUrl, config) {
  // Check if the service worker can be found. If it can't reload the page.
  fetch(swUrl)
    .then(response => {
      // Ensure service worker exists, and that we really are getting a JS file.
      if (
        response.status === 404 ||
        response.headers.get('content-type').indexOf('javascript') === -1
      ) {
        // No service worker found. Probably a different app. Reload the page.
        navigator.serviceWorker.ready.then(registration => {
          registration.unregister().then(() => {
            window.location.reload();
          });
        });
      } else {
        // Service worker found. Proceed as normal.
        registerValidSW(swUrl, config);
      }
    })
    .catch(() => {
      console.log(
        'No internet connection found. App is running in offline mode.',
      );
    });
}

export function unregister() {
  if ('serviceWorker' in navigator) {
    navigator.serviceWorker.ready.then(registration => {
      registration.unregister();
    });
  }
}