I am creating a ReactJS app with the create-react-app utility. How could I configure it to use a file that will contain a service worker?
EDIT: From Javascript side is clear for me, add the registration in my index.js:
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('./service_workers/sw.js')
.then(function(registration) {
// Registration was successful...
}).catch(function(err) {
// registration failed ...
});
}
Then my configuration in my service worker file (that for me is in service_wokers/sw.js):
self.addEventListener('install', function(event) {//my code here...});
self.addEventListener('activate', function(event) {//my code here...});
self.addEventListener('fetch', function(event) {//my code here...});
When I run this the console shows: ServiceWorker registration failed: DOMException: Failed to register a ServiceWorker: A bad HTTP response code (404) was received when fetching the script.
The file is not there as I am not configuring Webpack to do that. So I am trying to copy the sw.js file with the ouput with:
test:
/\.(js)$/,
loader: "file?name=[path][name].[ext]&context=./service_workers",
include: '/service_worker'
I think there is no need to say that I am totally new to Webpack.
