summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/service-workers/service-worker/resources/update-registration-with-type.py
blob: 3cabc0fb46d38a3e8c83f9480343887eaa730871 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
def classic_script():
    return b"""
      importScripts('./imported-classic-script.js');
      self.onmessage = e => {
        e.source.postMessage(imported);
      };
      """

def module_script():
    return b"""
      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 = [(b'Content-Type', b'application/javascript'),
               (b'Pragma', b'no-store'),
               (b'Cache-Control', b'no-store')]

    classic_first = request.GET[b'classic_first']
    key = request.GET[b'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 == b'1' else module_script()
    else:
        body = module_script() if classic_first == b'1' else classic_script()

    return 200, headers, body