HomeiOS Developmentios - Service Employee > Registration > Error > Scope Url ought...

ios – Service Employee > Registration > Error > Scope Url ought to begin with the given Script Url


  1. OS: iOS 16.4
  2. Failing code: navigator.serviceWorker.register(‘service-worker.js’, { scope: “https://stackoverflow.com/” })
  3. Service-Employee-Allowed: “https://stackoverflow.com/” header is ready
  4. Service Employee is hosted like: https://www.mywebsite.com/dist/app/service-worker.js
  5. This error doesn’t reproduce on Android 13 or Home windows 10 (Chrome)
  6. manifest scope set to “/”
  7. 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 { };
}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments