feat: better logs & helpful messages
This commit is contained in:
parent
1bd968df58
commit
bd3a1fe84c
1 changed files with 28 additions and 10 deletions
38
index.js
38
index.js
|
@ -22,19 +22,19 @@ const ASTROSW = 'astro-sw';
|
|||
/**
|
||||
* Accepts configuration options with service worker path
|
||||
* and injects needed variables such as `__assets` generated by Astro
|
||||
* @param {ServiceWorkerConfig} config
|
||||
* @param {ServiceWorkerConfig} options
|
||||
* @returns {AstroIntegration}
|
||||
*/
|
||||
export default function serviceWorker(config) {
|
||||
export default function serviceWorker(options) {
|
||||
let {
|
||||
assetCachePrefix = ASTROSW,
|
||||
assetCacheVersionID = randomUUID(),
|
||||
path: serviceWorkerPath,
|
||||
path: serviceWorkerPath = undefined,
|
||||
customRoutes = [],
|
||||
excludeRoutes = [],
|
||||
logAssets = false,
|
||||
esbuild = {}
|
||||
} = config;
|
||||
} = options || {};
|
||||
|
||||
/**
|
||||
* @type {Array<string>}
|
||||
|
@ -75,8 +75,15 @@ export default function serviceWorker(config) {
|
|||
return {
|
||||
'name': ASTROSW,
|
||||
'hooks': {
|
||||
'astro:config:setup': async ({ injectScript, config, command, logger }) => {
|
||||
output = config.output;
|
||||
'astro:config:setup': async ({ injectScript, config: _config, command, logger }) => {
|
||||
|
||||
if (!serviceWorkerPath || serviceWorkerPath === '') {
|
||||
// REQUIRED OPTION IS MISSING
|
||||
logger.error('Missing required path to service worker script');
|
||||
}
|
||||
|
||||
|
||||
output = _config.output;
|
||||
if (command === 'build') {
|
||||
injectScript('page', registrationScript);
|
||||
}
|
||||
|
@ -98,7 +105,9 @@ declare const __prefix: string;`
|
|||
},
|
||||
'astro:build:done': async ({ dir, routes, pages, logger }) => {
|
||||
const outfile = fileURLToPath(new URL('./sw.js', dir));
|
||||
const swPath = path.join(__dirname, serviceWorkerPath ?? '');
|
||||
const swPath = (serviceWorkerPath && serviceWorkerPath !== '')
|
||||
? path.join(__dirname, serviceWorkerPath)
|
||||
: undefined;
|
||||
let originalScript;
|
||||
|
||||
const _publicFiles = (await readdir(dir, { withFileTypes: true }) ?? [])
|
||||
|
@ -147,17 +156,26 @@ declare const __prefix: string;`
|
|||
&& !_excludeRoutes.includes(asset)
|
||||
);
|
||||
|
||||
logger.info(`${assets.length} assets for caching.`);
|
||||
|
||||
if (logAssets) {
|
||||
logger.info('Assets: ' + assets.toString().replaceAll(',', ', '));
|
||||
logger.info(`\n\n[${ASTROSW}] ${assets.length} assets for caching: \n ▶ ${assets.toString().replaceAll(',', ',\n ▶ ')}\n`);
|
||||
} else {
|
||||
logger.info(`${assets.length} assets for caching.`);
|
||||
}
|
||||
|
||||
try {
|
||||
logger.info(`Using service worker: ${swPath}`);
|
||||
logger.info(`Using service worker in path: ${swPath}`);
|
||||
originalScript = await readFile(swPath);
|
||||
} catch {
|
||||
logger.error(`Service worker script not found! ${swPath}`)
|
||||
if (!swPath) {
|
||||
|
||||
logger.error(`
|
||||
|
||||
[${ASTROSW}] ERR: The 'path' option is required!
|
||||
[${ASTROSW}] INFO: Please see service worker options in https://ayco.io/gh/astro-sw#readme
|
||||
`);
|
||||
}
|
||||
}
|
||||
|
||||
const assetsDeclaration = `const __assets = ${JSON.stringify(assets)};\n`;
|
||||
|
|
Loading…
Reference in a new issue