feat: support static output; remove files from assets
This commit is contained in:
parent
9e1e5f8c3b
commit
e395600420
2 changed files with 19 additions and 8 deletions
|
@ -3,10 +3,11 @@ import node from "@astrojs/node";
|
||||||
import serviceWorker from "./index.js";
|
import serviceWorker from "./index.js";
|
||||||
|
|
||||||
export default defineConfig({
|
export default defineConfig({
|
||||||
output: "hybrid",
|
// output: "server",
|
||||||
adapter: node({
|
// adapter: node({
|
||||||
mode: "middleware"
|
// mode: "middleware"
|
||||||
}),
|
// }),
|
||||||
|
site: 'https://ayo.ayco.io',
|
||||||
integrations: [
|
integrations: [
|
||||||
serviceWorker({
|
serviceWorker({
|
||||||
path: "./example_sw.js",
|
path: "./example_sw.js",
|
||||||
|
|
18
index.js
18
index.js
|
@ -70,14 +70,22 @@ export default function serviceWorker(config) {
|
||||||
|
|
||||||
registerSW();`
|
registerSW();`
|
||||||
|
|
||||||
|
let output = 'static'
|
||||||
|
|
||||||
return {
|
return {
|
||||||
'name': 'astro-sw',
|
'name': 'astro-sw',
|
||||||
'hooks': {
|
'hooks': {
|
||||||
'astro:config:setup': ({injectScript}) => {
|
'astro:config:setup': ({injectScript, config}) => {
|
||||||
|
output = config.output;
|
||||||
injectScript('page', registrationScript);
|
injectScript('page', registrationScript);
|
||||||
},
|
},
|
||||||
'astro:build:ssr': ({ manifest }) => {
|
'astro:build:ssr': ({ manifest }) => {
|
||||||
assets = manifest.assets.filter(ass => !ass.includes('sw.js'))
|
const files = manifest.routes.map(route => route.file.replaceAll('/', ''));
|
||||||
|
const assetsMinusFiles = manifest.assets.filter(ass => !files.includes(ass.replaceAll('/', '')));
|
||||||
|
|
||||||
|
assets = output === 'static'
|
||||||
|
? assetsMinusFiles
|
||||||
|
: manifest.assets.filter(ass => !ass.includes('sw.js'));
|
||||||
},
|
},
|
||||||
'astro:build:done': async ({ dir, routes, pages, }) => {
|
'astro:build:done': async ({ dir, routes, pages, }) => {
|
||||||
const outFile = fileURLToPath(new URL('./sw.js', dir));
|
const outFile = fileURLToPath(new URL('./sw.js', dir));
|
||||||
|
@ -92,11 +100,12 @@ export default function serviceWorker(config) {
|
||||||
?? [];
|
?? [];
|
||||||
|
|
||||||
const _pages = pages
|
const _pages = pages
|
||||||
.map(({pathname}) => pathname)
|
.filter(({pathname}) => pathname !== '')
|
||||||
.filter(pathname => pathname !== '')
|
.map(({pathname}) => `/${pathname}`)
|
||||||
?? [];
|
?? [];
|
||||||
|
|
||||||
const _pagesWithoutEndSlash = pages
|
const _pagesWithoutEndSlash = pages
|
||||||
|
.filter(({pathname}) => pathname !== '')
|
||||||
.map(({pathname}) => {
|
.map(({pathname}) => {
|
||||||
const lastChar = pathname.slice(-1);
|
const lastChar = pathname.slice(-1);
|
||||||
const len = pathname.length;
|
const len = pathname.length;
|
||||||
|
@ -108,6 +117,7 @@ export default function serviceWorker(config) {
|
||||||
?? [];
|
?? [];
|
||||||
|
|
||||||
assets = [...new Set([...assets, ..._routes, ..._pages, ..._pagesWithoutEndSlash])]
|
assets = [...new Set([...assets, ..._routes, ..._pages, ..._pagesWithoutEndSlash])]
|
||||||
|
console.log('>>> assets', assets);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
console.log('[astro-sw] Using service worker:', swPath);
|
console.log('[astro-sw] Using service worker:', swPath);
|
||||||
|
|
Loading…
Reference in a new issue