UNPKG

@appsemble/node-utils

Version:

NodeJS utilities used by Appsemble internally.

19 lines 1.11 kB
import { readFile } from 'node:fs/promises'; import { assertKoaCondition } from '@appsemble/node-utils'; import { getAppBlocks } from '@appsemble/utils'; export function createServiceWorkerHandler({ getApp, getBlocksAssetsPaths }) { return async (ctx) => { const production = process.env.NODE_ENV === 'production'; const filename = production ? '/service-worker.js' : '/app/service-worker.js'; const serviceWorker = await (production ? readFile(new URL('../../../../../dist/app/service-worker.js', import.meta.url), 'utf8') : ctx.fs.promises.readFile(filename, 'utf8')); const app = await getApp({ context: ctx }); assertKoaCondition(app != null, ctx, 404, 'App does not exist.'); const identifiableBlocks = getAppBlocks(app.definition); const blocksAssetsPaths = await getBlocksAssetsPaths({ identifiableBlocks, context: ctx }); ctx.body = `const blockAssets=${JSON.stringify(blocksAssetsPaths)};${serviceWorker}`; ctx.type = 'application/javascript'; }; } //# sourceMappingURL=serviceWorkerHandler.js.map