Service worker type module not work

Username (e.g. epiz_XXX) or Website URL

Subdominion = mecpag
dominion = epizy
root = app_config
Paths = provider php //deliver a js file
index php

Error Message

when i use

navigator.serviceWorker.register('provider php', {
type: module
}).then...

i get this message

DOMException: Failed to register a ServiceWorker for scope (‘THE PATH’) with script (‘THE PATH /provider’): The script has an unsupported MIME type (‘text/html’).

Other Information

the cookies maybe are not being sent by the browser

navigator.serviceWorker.register without option work how expected

------provider#.php

<?php
function n($en, $em, $ef, $el){
    file_put_contents('errors.txt', $em.','.$el);
}
set_error_handler('n');

header('Cache-Control: public,max-age=31600');
header('Content-Type: application/javascript');
  
  $data = '';
  foreach ($_SERVER as $key=>$value){
    if($key === 'REQUEST_URI' || $key === 'HTTP_COOKIE')
    $data.=$key.' => '.$value."\n";
  }
  file_put_contents('headers.txt', $data, FILE_APPEND);
  
  include 'swi.php';

  file_put_contents('end.txt', 'Hi');

?>

------.htaccess

Header set Access-Control-Allow-Origin "THE PATH"
Header set Access-Control-Allow-Methods "GET"
Header set Access-Control-Allow-Crendentials: "true"

scheme = https

image

this doesn’t look like a URL
which should lead to some file

especially when it doesn’t have an extension

The browser only sees the word “provider” (does not look further)
and such a thing does not exist and actually sees a 404 error page (MIME type (‘text/html’))

image

and I don’t know if it can be a .php file at all
or it must be .js

in any case the script address should point to an existing file
and be called in code like this “provider.php” or “provider.js”

not

navigator.serviceWorker.register('provider space space php'

5 Likes

i can only post two urls so i not use the url so provider is provider.php and all links in my code are functioning, in localhost i can use register with options with type =’ module’ set, so the problem is the host, especifically the policy of not permitting get content withou crendentials or a browser with javascript, but i not sure if register are sending the cookies or not(im think not), or if the register not have a js scope for pass the free server policy

I’m fairly sure that Progressive Web Apps don’t persist cookies, and the Service Worker API calls definitely don’t execute the Javascript code to generate a new one.

4 Likes

but using the

navigator.serviceWorker.register('url').....

give me no error and the code have a smooth progression
but when a call


navigator.serviceWorker.register('url', {
type: 'module'
}).....

the nightmare begin

hahaha i was seeing the diference in request between the two in localhost a here is the problem the browser really not send cookies with type: ‘module’ set, maybe a bug or other explanation

//without type: ‘module’
HTTP_HOST => localhost:8080
HTTP_CONNECTION => keep-alive
HTTP_CACHE_CONTROL => max-age=0
HTTP_ACCEPT => /
HTTP_SERVICE_WORKER => script
HTTP_SEC_FETCH_SITE => same-origin
HTTP_SEC_FETCH_MODE => same-origin
HTTP_SEC_FETCH_DEST => serviceworker
HTTP_REFERER => http://localhost:8080/app_config/
HTTP_USER_AGENT => Mozilla/5.0 (Linux; Android 12; SM-M526B) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Mobile Safari/537.36
HTTP_ACCEPT_ENCODING => gzip, deflate, br
HTTP_ACCEPT_LANGUAGE => pt-BR,pt;q=0.9,en-US;q=0.8,en;q=0.7
HTTP_COOKIE => id=1234

//with type: ‘module’
HTTP_HOST => localhost:8080
HTTP_CONNECTION => keep-alive
HTTP_CACHE_CONTROL => max-age=0
HTTP_ACCEPT => /
HTTP_SERVICE_WORKER => script
HTTP_SEC_FETCH_SITE => same-origin
HTTP_SEC_FETCH_MODE => same-origin
HTTP_SEC_FETCH_DEST => serviceworker
HTTP_REFERER => http://localhost:8080/app_config/
HTTP_USER_AGENT => Mozilla/5.0 (Linux; Android 12; SM-M526B) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Mobile Safari/537.36
HTTP_ACCEPT_ENCODING => gzip, deflate, br
HTTP_ACCEPT_LANGUAGE => pt-BR,pt;q=0.9,en-US;q=0.8,en;q=0.7

So using type: ‘module’ in free hosting is impossible

I’m not sure that that module type actually changes in terms of requests, but in the HTTP data you shared, I do see that the HTTP_COOKIE parameter is present when not using type: module but for modules it’s not present.

Given that our security system checks access with cookies, if the request doesn’t send cookies, then it will be intercepted.

3 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.