@@ -41,15 +41,6 @@ async function emit(rel, code) {
4141 await fs . writeFile ( dest , code ) ;
4242}
4343
44- const swRegisterSource = `if ('serviceWorker' in navigator) {
45- window.addEventListener('load', function () {
46- navigator.serviceWorker.register('/sw.js').catch(function (e) {
47- console.error('Service worker registration failed', e);
48- });
49- });
50- }
51- ` ;
52-
5344async function run ( ) {
5445 await fs . rm ( dist , { recursive : true , force : true } ) ;
5546 await fs . mkdir ( dist , { recursive : true } ) ;
@@ -79,11 +70,6 @@ async function run() {
7970 assetMap . set ( rel , hashed ) ;
8071 }
8172
82- // Service worker registration shim (external, to satisfy strict CSP).
83- const swReg = ( await transform ( swRegisterSource , { loader : 'js' , minify : true } ) ) . code ;
84- const swRegRel = hashedName ( 'js/sw-register.js' , swReg ) ;
85- await emit ( swRegRel , swReg ) ;
86-
8773 // Replace original asset paths with hashed ones in HTML. Longest keys first
8874 // so shorter paths can't partially shadow longer ones.
8975 const replacements = [ ...assetMap . entries ( ) ] . sort ( ( a , b ) => b [ 0 ] . length - a [ 0 ] . length ) ;
@@ -92,8 +78,7 @@ async function run() {
9278 return html ;
9379 } ;
9480
95- let indexHtml = rewriteHtml ( await fs . readFile ( path . join ( root , 'index.html' ) , 'utf8' ) ) ;
96- indexHtml = indexHtml . replace ( '</body>' , ` <script src="${ swRegRel } "></script>\n</body>` ) ;
81+ const indexHtml = rewriteHtml ( await fs . readFile ( path . join ( root , 'index.html' ) , 'utf8' ) ) ;
9782 await emit ( 'index.html' , await minifyHtml ( indexHtml , htmlMinifyOptions ) ) ;
9883
9984 for ( const file of await walk ( path . join ( root , 'static' ) ) ) {
@@ -102,73 +87,25 @@ async function run() {
10287 else await emit ( rel , await fs . readFile ( file ) ) ;
10388 }
10489
105- // Precache the full app shell: index, hashed assets, static pages.
106- const precache = [ '/' ] ;
107- for ( const hashed of assetMap . values ( ) ) precache . push ( '/' + hashed ) ;
108- precache . push ( '/' + swRegRel ) ;
109- for ( const file of await walk ( path . join ( dist , 'static' ) ) ) {
110- if ( file . endsWith ( '.html' ) ) precache . push ( '/' + toPosix ( path . relative ( dist , file ) ) ) ;
111- }
112-
113- const version = sha8 ( Buffer . from ( [ ...assetMap . values ( ) ] . sort ( ) . join ( '|' ) ) ) ;
114- const sw = `const CACHE = 'nym-${ version } ';
115- const PRECACHE = ${ JSON . stringify ( precache ) } ;
116- self.addEventListener('install', (e) => {
117- e.waitUntil(caches.open(CACHE).then((c) => c.addAll(PRECACHE)).then(() => self.skipWaiting()));
118- });
119- self.addEventListener('activate', (e) => {
120- e.waitUntil(caches.keys().then((keys) => Promise.all(keys.filter((k) => k !== CACHE).map((k) => caches.delete(k)))).then(() => self.clients.claim()));
121- });
122- self.addEventListener('fetch', (e) => {
123- const req = e.request;
124- if (req.method !== 'GET') return;
125- const url = new URL(req.url);
126- if (url.origin !== self.location.origin) return;
127- if (url.pathname.startsWith('/api/')) return;
128- if (req.mode === 'navigate') {
129- e.respondWith(
130- fetch(req).then((res) => {
131- const copy = res.clone();
132- caches.open(CACHE).then((c) => c.put(req, copy));
133- return res;
134- }).catch(() => caches.match(req).then((r) => r || caches.match('/')))
135- );
136- return;
137- }
138- e.respondWith(
139- caches.match(req).then((cached) => cached || fetch(req).then((res) => {
140- if (res.ok && res.type === 'basic') {
141- const copy = res.clone();
142- caches.open(CACHE).then((c) => c.put(req, copy));
143- }
144- return res;
145- }))
146- );
147- });
148- ` ;
149- await emit ( 'sw.js' , sw ) ;
150-
15190 // robots.txt verbatim.
15291 await emit ( 'robots.txt' , await fs . readFile ( path . join ( root , 'robots.txt' ) ) ) ;
15392
154- // _headers + immutable caching for hashed assets, no-cache for entry/sw .
93+ // _headers + immutable caching for hashed assets, no-cache for entry.
15594 const headers = await fs . readFile ( path . join ( root , '_headers' ) , 'utf8' ) ;
15695 const cacheRules = `
15796
15897/js/*
15998 Cache-Control: public, max-age=31536000, immutable
16099/css/*
161100 Cache-Control: public, max-age=31536000, immutable
162- /sw.js
163- Cache-Control: no-cache
164101/index.html
165102 Cache-Control: no-cache
166103/
167104 Cache-Control: no-cache
168105` ;
169106 await emit ( '_headers' , headers . replace ( / \s * $ / , '' ) + cacheRules ) ;
170107
171- console . log ( `Built ${ assetMap . size + 1 } assets to dist/ (cache nym- ${ version } , ${ precache . length } precached) .` ) ;
108+ console . log ( `Built ${ assetMap . size } assets to dist/.` ) ;
172109}
173110
174111run ( ) . catch ( ( e ) => { console . error ( e ) ; process . exit ( 1 ) ; } ) ;
0 commit comments