React/WordPress PWA does not respond with a 200 when offline

别说谁变了你拦得住时间么 提交于 2020-01-13 16:59:46

问题


I am currently trying to build a WordPress theme using React/Redux. Everything is working as expected except for responding with 200 when offline.

The build folder of CRA (Create React App) is located at /wp-content/themes/pwa/build.

When online, WordPress loads the index.php for all routes. This registers the .css and .js from the build folder. Here everything is working as expected.

Im trying to understand how to configure CRA / WorkBox to register the service worker at / and serve all pages using the index.html in the build folder whilst offline.

Full url of index.html: https://example.com/wp-content/themes/pwa/build/index.html

I can provide a link to the demo if required.


回答1:


I finally solved the problem with some help from W3C.

Pass scope option in /src/serviceWorker.js of the react app.

// navigator.serviceWorker.register(swUrl)
navigator.serviceWorker.register(swUrl, {scope: "/"})

Configure Apache to serve the Service-Worker-Allowed header for the deployed service worker.

<Files "service-worker.js">
    Header Set Service-Worker-Allowed "/"
</Files>


来源:https://stackoverflow.com/questions/53171684/react-wordpress-pwa-does-not-respond-with-a-200-when-offline

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!