| 11 | * @alpha |
| 12 | */ |
| 13 | export const PrefetchServiceWorker = (opts: { |
| 14 | base?: string; |
| 15 | scope?: string; |
| 16 | path?: string; |
| 17 | verbose?: boolean; |
| 18 | fetchBundleGraph?: boolean; |
| 19 | nonce?: string; |
| 20 | }): JSXNode<'script'> => { |
| 21 | const isTest = import.meta.env.TEST; |
| 22 | if (isDev && !isTest) { |
| 23 | const props = { |
| 24 | dangerouslySetInnerHTML: '<!-- PrefetchServiceWorker is disabled in dev mode. -->', |
| 25 | }; |
| 26 | return _jsxC('script', props, 0, 'prefetch-service-worker'); |
| 27 | } |
| 28 | |
| 29 | // if an MFE app has a custom BASE_URL then this will be the correct value |
| 30 | // if you're not using MFE from another codebase then you want to override this value to your custom setup |
| 31 | const baseUrl = import.meta.env.BASE_URL || '/'; |
| 32 | const resolvedOpts = { |
| 33 | path: 'qwik-prefetch-service-worker.js', |
| 34 | ...opts, |
| 35 | }; |
| 36 | if (opts?.path?.startsWith?.('/')) { |
| 37 | // allow different path and base |
| 38 | resolvedOpts.path = opts.path; |
| 39 | } else { |
| 40 | // baseUrl: '/' |
| 41 | // path: 'qwik-prefetch-service-worker.js' |
| 42 | // the file 'qwik-prefetch-service-worker.js' is not located in /build/ |
| 43 | resolvedOpts.path = baseUrl + resolvedOpts.path; |
| 44 | } |
| 45 | let code = PREFETCH_CODE.replace('"_URL_"', JSON.stringify(resolvedOpts.path.split('/').pop())); |
| 46 | if (!isDev) { |
| 47 | // consecutive spaces are indentation |
| 48 | code = code.replaceAll(/\s\s+/gm, ''); |
| 49 | } |
| 50 | const props = { |
| 51 | dangerouslySetInnerHTML: [ |
| 52 | '(' + code + ')(', |
| 53 | [ |
| 54 | 'navigator.serviceWorker', // Service worker container |
| 55 | ].join(','), |
| 56 | ');', |
| 57 | ].join(''), |
| 58 | nonce: resolvedOpts.nonce, |
| 59 | }; |
| 60 | return _jsxC('script', props, 0, 'prefetch-service-worker'); |
| 61 | }; |
| 62 | |
| 63 | const PREFETCH_CODE = /*#__PURE__*/ (( |
| 64 | c: ServiceWorkerContainer // Service worker container |