yyx990803 / register-service-worker

A script to simplify service worker registration with hooks for common events.
MIT License
638 stars 58 forks source link

fix: remove default scope from registrationOptions #26

Closed ulivz closed 5 years ago

ulivz commented 5 years ago

Motivation

Recently we have supported registration options and its default value is set to { scope: '/' }, but there is a potential problem, if an user didn't pass in registrationOptions, at the same time he/she deployed his/her site at a sub directory, e.g. vuejs.org/sub/. he will meet a error of service worker:

The path of the provided scope ('/') is not under the max scope allowed ('/sub/'). Adjust the scope, move the Service Worker script, or use the Service-Worker-Allowed HTTP header to allow the scope.

This is due to the service-worker.js is under vuejs.org/sub/ but the default scope we set is equivalent to vuejs.org/ which causes the leading out of scope. so in that case user'll need to override registrationOptions.

Actually, following the mozilla:

By default, the scope value for a service worker registration is set to the directory where the service worker script is located.

So we needn't to set the default value since the native register API will help us to do it.


BTW, actually I guess it should typo of the previous PR, the default value should be ./ instead of /. but the better is to remove it directly.

yyx990803 commented 5 years ago

@ulivz I've added to as a collaborator to this repo. You also now have publishing rights on npm.

yyx990803 commented 5 years ago

also /cc @sodatea for PRs that introduce substantial changes you can request reviews from each other to make sure.