- OS: iOS 16.4
- Failing code: navigator.serviceWorker.register(‘service-worker.js’, { scope: “https://stackoverflow.com/” })
- Service-Employee-Allowed: “https://stackoverflow.com/” header is ready
- Service Employee is hosted like: https://www.mywebsite.com/dist/app/service-worker.js
- This error doesn’t reproduce on Android 13 or Home windows 10 (Chrome)
- manifest scope set to “/”
- manifest start_url set to “/dist/app/index.html”
Why is iOS throwing error “Scope Url ought to begin with the given Script Url” when Android and Home windows are advantageous?
I discovered Apple’s supply code for exception, however nonetheless cannot determine what’s unsuitable:
ResourceError ServiceWorkerJob::validateServiceWorkerResponse(const ServiceWorkerJobData& jobData, const ResourceResponse& response)
{
// Extract a MIME sort from the response's header checklist. If this MIME sort (ignoring parameters) isn't a JavaScript MIME sort, then:
if (!MIMETypeRegistry::isSupportedJavaScriptMIMEType(response.mimeType()))
return { errorDomainWebKitInternal, 0, response.url(), "MIME Kind isn't a JavaScript MIME sort"_s };
auto serviceWorkerAllowed = response.httpHeaderField(HTTPHeaderName::ServiceWorkerAllowed);
String maxScopeString;
if (serviceWorkerAllowed.isNull()) {
auto path = jobData.scriptURL.path();
// Final a part of the trail is the script's filename.
maxScopeString = path.left(path.reverseFind("https://stackoverflow.com/") + 1).toString();
} else {
auto maxScope = URL(jobData.scriptURL, serviceWorkerAllowed);
if (SecurityOrigin::create(maxScope)->isSameOriginAs(SecurityOrigin::create(jobData.scriptURL)))
maxScopeString = maxScope.path().toString();
}
auto scopeString = jobData.scopeURL.path();
if (maxScopeString.isNull() || !scopeString.startsWith(maxScopeString))
return { errorDomainWebKitInternal, 0, response.url(), "Scope URL ought to begin with the given script URL"_s };
return { };
}