Skip to content

Commit

Permalink
fix: disable PrefetchServiceWorker in dev mode (#6606)
Browse files Browse the repository at this point in the history
  • Loading branch information
gioboa authored Jun 25, 2024
1 parent 655712c commit 3a9ee62
Showing 1 changed file with 8 additions and 26 deletions.
34 changes: 8 additions & 26 deletions packages/qwik/src/core/components/prefetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ export const PrefetchServiceWorker = (opts: {
fetchBundleGraph?: boolean;
nonce?: string;
}): JSXNode<'script'> => {
const isTest = import.meta.env.TEST;
if (isDev && !isTest) {
const props = {
dangerouslySetInnerHTML: '<!-- PrefetchServiceWorker is disabled in dev mode. -->',
};
return _jsxC('script', props, 0, 'prefetch-service-worker');
}

const serverData = useServerData<Record<string, string>>('containerAttributes', {});
// if an MFE app has a custom BASE_URL then this will be the correct value
// if you're not using MFE from another codebase then you want to override this value to your custom setup
Expand All @@ -51,32 +59,6 @@ export const PrefetchServiceWorker = (opts: {
// the file 'qwik-prefetch-service-worker.js' is not located in /build/
resolvedOpts.path = baseUrl + resolvedOpts.path;
}
// dev only errors
if (isDev) {
// Check if base ends with a '/'
if (!resolvedOpts.base.endsWith('/')) {
throw new Error(
`The 'base' option should always end with a '/'. Received: ${resolvedOpts.base}`
);
}
// Check if path does not start with a '/' and ends with '.js'
if (!resolvedOpts.path.endsWith('.js')) {
throw new Error(`The 'path' option must end with '.js'. Received: ${resolvedOpts.path}`);
}
// Validate service worker scope (must start with a '/' and not contain spaces)
if (!resolvedOpts.scope.startsWith('/') || /\s/.test(resolvedOpts.scope)) {
throw new Error(
`Invalid 'scope' option for service worker. It must start with '/' and contain no spaces. Received: ${resolvedOpts.scope}`
);
}
if (resolvedOpts.verbose) {
// eslint-disable-next-line no-console
console.log(
'Installing <PrefetchServiceWorker /> service-worker with options:',
resolvedOpts
);
}
}
let code = PREFETCH_CODE.replace('URL', resolvedOpts.path).replace('SCOPE', resolvedOpts.scope);
if (!isDev) {
code = code.replaceAll(/\s+/gm, '');
Expand Down

0 comments on commit 3a9ee62

Please sign in to comment.