feat: add public assets (recursive) on static output
Some checks are pending
Demo / Explore-CI (push) Waiting to run

This commit is contained in:
ayo 2026-05-15 20:27:42 +02:00
parent c4bd0ae0b8
commit c7746357e4

View file

@ -3,7 +3,7 @@
* @author Ayo Ayco <https://ayo.ayco.io> * @author Ayo Ayco <https://ayo.ayco.io>
*/ */
import { readFile, writeFile, unlink } from 'node:fs/promises' import { readFile, writeFile, unlink, readdir } from 'node:fs/promises'
import { fileURLToPath } from 'node:url' import { fileURLToPath } from 'node:url'
import { resolve, dirname, join } from 'node:path' import { resolve, dirname, join } from 'node:path'
import { build } from 'esbuild' import { build } from 'esbuild'
@ -75,20 +75,21 @@ export default function serviceWorker(
registerSW();` registerSW();`
// let output = 'static' let output = 'static'
const __dirname = resolve(dirname('.')) const __dirname = resolve(dirname('.'))
return { return {
name: ASTROSW, name: ASTROSW,
hooks: { hooks: {
'astro:config:setup': async ({ injectScript, command, logger }) => { 'astro:config:setup': async ({ injectScript, command, logger, config }) => {
if (!serviceWorkerPath || serviceWorkerPath === '') { if (!serviceWorkerPath || serviceWorkerPath === '') {
// REQUIRED OPTION IS MISSING // REQUIRED OPTION IS MISSING
logger.error('Missing required path to service worker script') logger.error('Missing required path to service worker script')
} }
// const transformedScript=await transform(registrationScript) // const transformedScript=await transform(registrationScript)
// output = _config.output output = config.output
if (command === 'build') { if (command === 'build') {
injectScript('page', registrationScript) injectScript('page', registrationScript)
} }
@ -107,6 +108,7 @@ declare const __prefix: string;`
dir, dir,
pages, pages,
logger, logger,
assets: buildAssets
}) => { }) => {
const outfile = fileURLToPath(new URL('./sw.js', dir)) const outfile = fileURLToPath(new URL('./sw.js', dir))
const swPath = const swPath =
@ -115,6 +117,25 @@ declare const __prefix: string;`
: undefined : undefined
let originalScript let originalScript
/**
* only for output = 'static
*/
let _publicFiles: string[] = []
if (output === 'static') {
_publicFiles = (
(await readdir(dir, { withFileTypes: true, recursive: true })) ?? []
)
.filter(dirent => dirent.isFile())
.map((dirent) => {
const currentDir = dirent.parentPath.replace(__dirname + '/dist/', '/')
const filepath = `${currentDir === '/' ? '' : currentDir}/${dirent.name}`
return filepath
})
}
const _pages = const _pages =
pages pages
.filter(({ pathname }) => pathname !== '') .filter(({ pathname }) => pathname !== '')
@ -140,12 +161,17 @@ declare const __prefix: string;`
...exclude.map((route) => `${route}/`), ...exclude.map((route) => `${route}/`),
] ]
const _buildAssets = Array.from(buildAssets.keys())
.filter(key => !key.includes('...slug'))
const __assets = [ const __assets = [
...new Set([ ...new Set([ // dedupe
...ssrAssets, ...ssrAssets,
...include, ...include,
..._buildAssets,
..._pages, ..._pages,
..._pagesWithoutEndSlash, ..._pagesWithoutEndSlash,
...(output === 'static' ? _publicFiles : [])
]), ]),
].filter( ].filter(
(asset) => (asset) =>