diff --git a/service-workers/service-worker/resources/imported-classic-script.js b/service-workers/service-worker/resources/imported-classic-script.js new file mode 100644 index 00000000000000..5fc520405135e2 --- /dev/null +++ b/service-workers/service-worker/resources/imported-classic-script.js @@ -0,0 +1 @@ +const imported = 'A classic script.'; diff --git a/service-workers/service-worker/resources/imported-module-script.js b/service-workers/service-worker/resources/imported-module-script.js new file mode 100644 index 00000000000000..56d196df040f9a --- /dev/null +++ b/service-workers/service-worker/resources/imported-module-script.js @@ -0,0 +1 @@ +export const imported = 'A module script.'; diff --git a/service-workers/service-worker/resources/update-registration-with-type.py b/service-workers/service-worker/resources/update-registration-with-type.py new file mode 100644 index 00000000000000..4f6d5ae28200e0 --- /dev/null +++ b/service-workers/service-worker/resources/update-registration-with-type.py @@ -0,0 +1,33 @@ +def classic_script(): + return """ + importScripts('./imported-classic-script.js'); + self.onmessage = e => { + e.source.postMessage(imported); + }; + """ + +def module_script(): + return """ + import * as module from './imported-module-script.js'; + self.onmessage = e => { + e.source.postMessage(module.imported); + }; + """ + +# Returns the classic script for a first request and +# returns the module script for second and subsequent requests. +def main(request, response): + headers = [('Content-Type', 'application/javascript'), + ('Pragma', 'no-store'), + ('Cache-Control', 'no-store')] + + classic_first = request.GET['classic_first'] + key = request.GET['key'] + requested_once = request.server.stash.take(key) + if requested_once is None: + request.server.stash.put(key, True) + body = classic_script() if classic_first == '1' else module_script() + else: + body = module_script() if classic_first == '1' else classic_script() + + return 200, headers, body diff --git a/service-workers/service-worker/update-registration-with-type.https.html b/service-workers/service-worker/update-registration-with-type.https.html new file mode 100644 index 00000000000000..00c8a3345bb3d8 --- /dev/null +++ b/service-workers/service-worker/update-registration-with-type.https.html @@ -0,0 +1,74 @@ + + +Service Worker: Update the registration with a different script type. + + + + + + +