feat: inject type definitions for __assets, __prefix, __version

This commit is contained in:
Ayo Ayco 2024-08-17 09:59:40 +02:00
parent d5f92e71a2
commit b6e72d3d7e
2 changed files with 25 additions and 5 deletions

View file

@ -69,15 +69,36 @@ export default function serviceWorker(config) {
registerSW();` registerSW();`
let output = 'static'; let output = 'static';
const __dirname = path.resolve(path.dirname('.'));
return { return {
'name': 'astro-sw', 'name': 'astro-sw',
'hooks': { 'hooks': {
'astro:config:setup': ({ injectScript, config, command }) => { 'astro:config:setup': async ({ injectScript, config, command, logger }) => {
output = config.output; output = config.output;
if (command === 'build') { if (command === 'build') {
injectScript('page', registrationScript); injectScript('page', registrationScript);
} }
const injectedTypeDefinitions = `
/***
* @ayco/astro-sw injected variables
*/
declare const __assets: string;
declare const __version: string;
declare const __prefix: string;
`
const envTs = path.join(__dirname, 'src/env.d.ts');
try {
await writeFile(
envTs,
injectedTypeDefinitions,
{ flag: 'a+' }
);
} catch (err) {
logger.error(err.toString())
}
}, },
'astro:build:ssr': ({ manifest }) => { 'astro:build:ssr': ({ manifest }) => {
const files = manifest.routes.map(route => route.file.replaceAll('/', '')); const files = manifest.routes.map(route => route.file.replaceAll('/', ''));
@ -89,7 +110,6 @@ export default function serviceWorker(config) {
}, },
'astro:build:done': async ({ dir, routes, pages, logger }) => { 'astro:build:done': async ({ dir, routes, pages, logger }) => {
const outfile = fileURLToPath(new URL('./sw.js', dir)); const outfile = fileURLToPath(new URL('./sw.js', dir));
const __dirname = path.resolve(path.dirname('.'));
const swPath = path.join(__dirname, serviceWorkerPath ?? ''); const swPath = path.join(__dirname, serviceWorkerPath ?? '');
let originalScript; let originalScript;

View file

@ -1,11 +1,11 @@
import { log } from "./utils"; import { log } from "./utils";
/** /**
* Note: @ayco/astro-sw integration injects variables `__astro_sw_cache_prefix__`, `__astro_sw_version__`, & `__astro_sw_assets__` * Note: @ayco/astro-sw integration injects variables `__prefix`, `__version`, & `__assets`
* -- find usage in package readme; `astro.config.mjs` integrations * -- find usage in package readme; `astro.config.mjs` integrations
* @see https://ayco.io/n/@ayco/astro-sw * @see https://ayco.io/n/@ayco/astro-sw
*/ */
const cacheName = `${__astro_sw_cache_prefix__ ?? 'app'}-v${__astro_sw_version__ ?? '000'}` const cacheName = `${__prefix ?? 'app'}-v${__version ?? '000'}`
const addResourcesToCache = async (resources) => { const addResourcesToCache = async (resources) => {
const cache = await caches.open(cacheName); const cache = await caches.open(cacheName);
console.log('adding resources to cache...', resources) console.log('adding resources to cache...', resources)
@ -80,7 +80,7 @@ self.addEventListener('install', (event) => {
event.waitUntil( event.waitUntil(
addResourcesToCache([ addResourcesToCache([
'./', './',
...(__astro_sw_assets__ ?? []) ...(__assets ?? [])
]) ])
); );
}); });