const CACHE_NAME = 'circuitjs1-app-cache-v301';

const urlsToCache = [
  '/circuit/about.html',
  '/circuit/canvas2svg.js',
  '/circuit/circuitjs.html',
  '/circuit/crystal.html',
  '/circuit/customfunction.html',
  '/circuit/customlogic.html',
  '/circuit/customtransformer.html',
  '/circuit/diodecalc.html',
  '/circuit/icon512.png',
  '/circuit/icon128.png',
  '/circuit/iframe.html',
  '/circuit/lz-string.min.js',
  '/circuit/manifest.json',
  '/circuit/mosfet-beta.html',
  '/circuit/opampreal.html',
  '/circuit/subcircuits.html',
'circuitjs103b/locale_pt.txt',
'circuitjs103b/locale_it.txt',
'circuitjs103b/locale_pl.txt_old',
'circuitjs103b/locale_de.txt',
'circuitjs103b/locale_da.txt',
'circuitjs103b/locale_es.txt',
'circuitjs103b/B03F752B19086981171993BA3CC8281D.cache.js',
'circuitjs103b/circuits/invertamp.txt',
'circuitjs103b/circuits/ttlnand.txt',
'circuitjs103b/circuits/filt-hipass.txt',
'circuitjs103b/circuits/longdist.txt',
'circuitjs103b/circuits/relayctr.txt',
'circuitjs103b/circuits/priencoder.txt',
'circuitjs103b/circuits/volttriple.txt',
'circuitjs103b/circuits/peak-detect.txt',
'circuitjs103b/circuits/inv-osc.txt',
'circuitjs103b/circuits/indmultfreq.txt',
'circuitjs103b/circuits/vco.txt',
'circuitjs103b/circuits/cc2impn.txt',
'circuitjs103b/circuits/relayor.txt',
'circuitjs103b/circuits/crystalosc.txt',
'circuitjs103b/circuits/eclnor.txt',
'circuitjs103b/circuits/amp-invert.txt',
'circuitjs103b/circuits/jfetfollower-nooff.txt',
'circuitjs103b/circuits/phasesplit.txt',
'circuitjs103b/circuits/tlmis1.txt',
'circuitjs103b/circuits/freqdouble.txt',
'circuitjs103b/circuits/transformerup.txt',
'circuitjs103b/circuits/diodelimit.txt',
'circuitjs103b/circuits/currentsrcelm.txt',
'circuitjs103b/circuits/blank.txt',
'circuitjs103b/circuits/relaymux.txt',
'circuitjs103b/circuits/satcore-comparison.txt',
'circuitjs103b/circuits/ota-vca.txt',
'circuitjs103b/circuits/rmsconverter.txt',
'circuitjs103b/circuits/cs-opamprail.txt',
'circuitjs103b/circuits/counter.txt',
'circuitjs103b/circuits/fulladd.txt',
'circuitjs103b/circuits/vilnius.txt',
'circuitjs103b/circuits/samplenhold.txt',
'circuitjs103b/circuits/crossover.txt',
'circuitjs103b/circuits/conv-buckboost.txt',
'circuitjs103b/circuits/butter10hiaud.txt',
'circuitjs103b/circuits/mr-crossbar.txt',
'circuitjs103b/circuits/zeneriv.txt',
'circuitjs103b/circuits/ccinductor.txt',
'circuitjs103b/circuits/capmultcaps.txt',
'circuitjs103b/circuits/mosfetamp.txt',
'circuitjs103b/circuits/varactor.txt',
'circuitjs103b/circuits/npn.txt',
'circuitjs103b/circuits/edgedff.txt',
'circuitjs103b/circuits/norton.txt',
'circuitjs103b/circuits/majority.txt',
'circuitjs103b/circuits/conv-boost.txt',
'circuitjs103b/circuits/wienbridge.txt',
'circuitjs103b/circuits/sipo-sr.txt',
'circuitjs103b/circuits/xor.txt',
'circuitjs103b/circuits/amp-schmitt.txt',
'circuitjs103b/circuits/phaseshiftosc.txt',
'circuitjs103b/circuits/satcore-inductor.txt',
'circuitjs103b/circuits/tesla.txt',
'circuitjs103b/circuits/darlington.txt',
'circuitjs103b/circuits/amp-sum.txt',
'circuitjs103b/circuits/tlstand.txt',
'circuitjs103b/circuits/trans-diffamp.txt',
'circuitjs103b/circuits/digsine.txt',
'circuitjs103b/circuits/unishiftreg.txt',
'circuitjs103b/circuits/voltquad.txt',
'circuitjs103b/circuits/filt-vcvs-hipass.txt',
'circuitjs103b/circuits/ujtosc.txt',
'circuitjs103b/circuits/rtlnand.txt',
'circuitjs103b/circuits/relaxosc.txt',
'circuitjs103b/circuits/cs-diff.txt',
'circuitjs103b/circuits/amp-diff.txt',
'circuitjs103b/circuits/spikegen.txt',
'circuitjs103b/circuits/decoder.txt',
'circuitjs103b/circuits/voltdouble.txt',
'circuitjs103b/circuits/transformer.txt',
'circuitjs103b/circuits/voltdivide.txt',
'circuitjs103b/circuits/early.txt',
'circuitjs103b/circuits/pushpull.txt',
'circuitjs103b/circuits/lrc.txt',
'circuitjs103b/circuits/grid.txt',
'circuitjs103b/circuits/amp-integ.txt',
'circuitjs103b/circuits/triangle.txt',
'circuitjs103b/circuits/amp-fullrect.txt',
'circuitjs103b/circuits/ladder.txt',
'circuitjs103b/circuits/ota-gain.txt',
'circuitjs103b/circuits/nandff.txt',
'circuitjs103b/circuits/cc2.txt',
'circuitjs103b/circuits/cmosinvertercap.txt',
'circuitjs103b/circuits/capmultfreq.txt',
'circuitjs103b/circuits/logconvert.txt',
'circuitjs103b/circuits/gilbertcell.txt',
'circuitjs103b/circuits/besselbutter.txt',
'circuitjs103b/circuits/latchingrelay.txt',
'circuitjs103b/circuits/tlfreq.txt',
'circuitjs103b/circuits/switchfilter.txt',
'circuitjs103b/circuits/res-par.txt',
'circuitjs103b/circuits/opampfeedback.txt',
'circuitjs103b/circuits/itov.txt',
'circuitjs103b/circuits/nmosfet.txt',
'circuitjs103b/circuits/capmult.txt',
'circuitjs103b/circuits/butter10hi.txt',
'circuitjs103b/circuits/cs-fullrectifier.txt',
'circuitjs103b/circuits/dtlinverter.txt',
'circuitjs103b/circuits/tlmatch2.txt',
'circuitjs103b/circuits/triodeamp.txt',
'circuitjs103b/circuits/3way.txt',
'circuitjs103b/circuits/opamp-regulator.txt',
'circuitjs103b/circuits/spark-marx.txt',
'circuitjs103b/circuits/jkff.txt',
'circuitjs103b/circuits/mr.txt',
'circuitjs103b/circuits/diodeclip.txt',
'circuitjs103b/circuits/amp-noninvert.txt',
'circuitjs103b/circuits/inductkick-block.txt',
'circuitjs103b/circuits/divideby2.txt',
'circuitjs103b/circuits/cc2n.txt',
'circuitjs103b/circuits/capac.txt',
'circuitjs103b/circuits/fullrect.txt',
'circuitjs103b/circuits/filt-vcvs-lopass.txt',
'circuitjs103b/circuits/ota-vcf-single.txt',
'circuitjs103b/circuits/actbutterlo.txt',
'circuitjs103b/circuits/trianglevco.txt',
'circuitjs103b/circuits/filt-lopass-l.txt',
'circuitjs103b/circuits/tlmatch1.txt',
'circuitjs103b/circuits/resistors.txt',
'circuitjs103b/circuits/flashadc.txt',
'circuitjs103b/circuits/cube.txt',
'circuitjs103b/circuits/gyrator.txt',
'circuitjs103b/circuits/opint-current.txt',
'circuitjs103b/circuits/cmosxor.txt',
'circuitjs103b/circuits/sine.txt',
'circuitjs103b/circuits/pll2.txt',
'circuitjs103b/circuits/inductkick-snub.txt',
'circuitjs103b/circuits/ringcascade.txt',
'circuitjs103b/circuits/divideby3.txt',
'circuitjs103b/circuits/delayrc.txt',
'circuitjs103b/circuits/555monostable.txt',
'circuitjs103b/circuits/adder4-sc.txt',
'circuitjs103b/circuits/crystalosc2.txt',
'circuitjs103b/circuits/diff.txt',
'circuitjs103b/circuits/ledarray.txt',
'circuitjs103b/circuits/jerk.txt',
'circuitjs103b/circuits/dtlnor.txt',
'circuitjs103b/circuits/conv-sepic.txt',
'circuitjs103b/circuits/3phasewye.txt',
'circuitjs103b/circuits/3-f211.txt',
'circuitjs103b/circuits/volume.txt',
'circuitjs103b/circuits/wheatstone.txt',
'circuitjs103b/circuits/555saw.txt',
'circuitjs103b/circuits/tllopass.txt',
'circuitjs103b/circuits/chaos2.txt',
'circuitjs103b/circuits/conv-cuk.txt',
'circuitjs103b/circuits/rtlinverter.txt',
'circuitjs103b/circuits/sram.txt',
'circuitjs103b/circuits/impedance.txt',
'circuitjs103b/circuits/relays.txt',
'circuitjs103b/circuits/pmosfet.txt',
'circuitjs103b/circuits/analogrecip.txt',
'circuitjs103b/circuits/indpar.txt',
'circuitjs103b/circuits/colpitts.txt',
'circuitjs103b/circuits/dcrestoration.txt',
'circuitjs103b/circuits/inductac.txt',
'circuitjs103b/circuits/cmosmsff.txt',
'circuitjs103b/circuits/currentsrc.txt',
'circuitjs103b/circuits/jsinterface.txt',
'circuitjs103b/circuits/amp-rect.txt',
'circuitjs103b/circuits/3-invert.txt',
'circuitjs103b/circuits/fullrect-sc.txt',
'circuitjs103b/circuits/relay.txt',
'circuitjs103b/circuits/chaos1.txt',
'circuitjs103b/circuits/filt-lopass.txt',
'circuitjs103b/circuits/leadingedge.txt',
'circuitjs103b/circuits/fanout.txt',
'circuitjs103b/circuits/chua.txt',
'circuitjs103b/circuits/brentkung.txt',
'circuitjs103b/circuits/indmultind.txt',
'circuitjs103b/circuits/lrc-critical.txt',
'circuitjs103b/circuits/synccounter.txt',
'circuitjs103b/circuits/powerfactor1.txt',
'circuitjs103b/circuits/diodecurve.txt',
'circuitjs103b/circuits/pll2a.txt',
'circuitjs103b/circuits/allpass2.txt',
'circuitjs103b/circuits/graycode.txt',
'circuitjs103b/circuits/butter10loaud.txt',
'circuitjs103b/circuits/transrectifier.txt',
'circuitjs103b/circuits/4way.txt',
'circuitjs103b/circuits/swtreedac.txt',
'circuitjs103b/circuits/mosfollower.txt',
'circuitjs103b/circuits/cs-currentadder.txt',
'circuitjs103b/circuits/bandnoise.txt',
'circuitjs103b/circuits/tlterm.txt',
'circuitjs103b/circuits/avr8js-analog.txt',
'circuitjs103b/circuits/relaytff.txt',
'circuitjs103b/circuits/conv-buck.txt',
'circuitjs103b/circuits/pong/paddles.txt',
'circuitjs103b/circuits/pong/playfull2.txt',
'circuitjs103b/circuits/pong/paddle1.txt',
'circuitjs103b/circuits/pong/hitsound.txt',
'circuitjs103b/circuits/pong/netonly.txt',
'circuitjs103b/circuits/pong/pongpad.html',
'circuitjs103b/circuits/pong/ponghfull.txt',
'circuitjs103b/circuits/pong/ponghfullslow.txt',
'circuitjs103b/circuits/mosswitch.txt',
'circuitjs103b/circuits/555lowduty.txt',
'circuitjs103b/circuits/schmitt.txt',
'circuitjs103b/circuits/bandpass.txt',
'circuitjs103b/circuits/powerfactor2.txt',
'circuitjs103b/circuits/amp-dfdx.txt',
'circuitjs103b/circuits/3-f220.txt',
'circuitjs103b/circuits/555pulsemod.txt',
'circuitjs103b/circuits/spark-sawtooth.txt',
'circuitjs103b/circuits/ohms.txt',
'circuitjs103b/circuits/ladderlh.txt',
'circuitjs103b/circuits/allpass1.txt',
'circuitjs103b/circuits/tl.txt',
'circuitjs103b/circuits/notch.txt',
'circuitjs103b/circuits/lambda-diode.txt',
'circuitjs103b/circuits/indseries.txt',
'circuitjs103b/circuits/ccint.txt',
'circuitjs103b/circuits/cmostransgate.txt',
'circuitjs103b/circuits/555missing.txt',
'circuitjs103b/circuits/thevenin.txt',
'circuitjs103b/circuits/ttlnor.txt',
'circuitjs103b/circuits/3-f221.txt',
'circuitjs103b/circuits/hfadc.txt',
'circuitjs103b/circuits/multivib-bi.txt',
'circuitjs103b/circuits/moscurrentramp.txt',
'circuitjs103b/circuits/tdosc.txt',
'circuitjs103b/circuits/coupled3.txt',
'circuitjs103b/circuits/satcore-transformer.txt',
'circuitjs103b/circuits/cs-ramp.txt',
'circuitjs103b/circuits/phasecomp.txt',
'circuitjs103b/circuits/nmosinverter.txt',
'circuitjs103b/circuits/voltdouble2.txt',
'circuitjs103b/circuits/relayff.txt',
'circuitjs103b/circuits/rossler.txt',
'circuitjs103b/circuits/transformerdc.txt',
'circuitjs103b/circuits/fullrectf.txt',
'circuitjs103b/circuits/mr-square.txt',
'circuitjs103b/circuits/cs-multiplier.txt',
'circuitjs103b/circuits/dac.txt',
'circuitjs103b/circuits/tlmismatch.txt',
'circuitjs103b/circuits/mosmirror.txt',
'circuitjs103b/circuits/joule-thief.txt',
'circuitjs103b/circuits/transformerdown.txt',
'circuitjs103b/circuits/delta-pwm.txt',
'circuitjs103b/circuits/coupled2.txt',
'circuitjs103b/circuits/multivib-mono.txt',
'circuitjs103b/circuits/cs-varinduct.txt',
'circuitjs103b/circuits/trans-diffamp-cursrc.txt',
'circuitjs103b/circuits/relayosc.txt',
'circuitjs103b/circuits/jfetamp.txt',
'circuitjs103b/circuits/0.txt',
'circuitjs103b/circuits/opamp.txt',
'circuitjs103b/circuits/555schmitt.txt',
'circuitjs103b/circuits/lissa.txt',
'circuitjs103b/circuits/jfetcurrentsrc.txt',
'circuitjs103b/circuits/555sequencer.txt',
'circuitjs103b/circuits/jfetfollower.txt',
'circuitjs103b/circuits/piso-sr.txt',
'circuitjs103b/circuits/scr.txt',
'circuitjs103b/circuits/tdiode.txt',
'circuitjs103b/circuits/ttlinverter.txt',
'circuitjs103b/circuits/clockedsrff.txt',
'circuitjs103b/circuits/johnsonctr.txt',
'circuitjs103b/circuits/halfadd.txt',
'circuitjs103b/circuits/potdivide.txt',
'circuitjs103b/circuits/opint.txt',
'circuitjs103b/circuits/lightbulb.txt',
'circuitjs103b/circuits/diodevar.txt',
'circuitjs103b/circuits/sinediode.txt',
'circuitjs103b/circuits/coupled1.txt',
'circuitjs103b/circuits/amp-follower.txt',
'circuitjs103b/circuits/pushpullxover.txt',
'circuitjs103b/circuits/nmosnand.txt',
'circuitjs103b/circuits/nic-r.txt',
'circuitjs103b/circuits/tllight.txt',
'circuitjs103b/circuits/grid2.txt',
'circuitjs103b/circuits/comb.txt',
'circuitjs103b/circuits/pot.txt',
'circuitjs103b/circuits/pnp.txt',
'circuitjs103b/circuits/avr8js-logic.txt',
'circuitjs103b/circuits/nmosinverter3.txt',
'circuitjs103b/circuits/cmosff.txt',
'circuitjs103b/circuits/mr-sine2.txt',
'circuitjs103b/circuits/mirror.txt',
'circuitjs103b/circuits/cmosinverterslow.txt',
'circuitjs103b/circuits/zenerref.txt',
'circuitjs103b/circuits/cs-integrator.txt',
'circuitjs103b/circuits/zenerreffollow.txt',
'circuitjs103b/circuits/cmosinverter.txt',
'circuitjs103b/circuits/7segdecoder.txt',
'circuitjs103b/circuits/ceamp.txt',
'circuitjs103b/circuits/mr-sine3.txt',
'circuitjs103b/circuits/nmosinverter2.txt',
'circuitjs103b/circuits/currentsrcramp.txt',
'circuitjs103b/circuits/transswitch.txt',
'circuitjs103b/circuits/eclosc.txt',
'circuitjs103b/circuits/scractrig.txt',
'circuitjs103b/circuits/moscurrentsrc.txt',
'circuitjs103b/circuits/555square.txt',
'circuitjs103b/circuits/mr-sine.txt',
'circuitjs103b/circuits/3motor.txt',
'circuitjs103b/circuits/avr8js-strobe.txt',
'circuitjs103b/circuits/555int.txt',
'circuitjs103b/circuits/rectify.txt',
'circuitjs103b/circuits/trans-diffamp-common.txt',
'circuitjs103b/circuits/mr-triangle.txt',
'circuitjs103b/circuits/cappar.txt',
'circuitjs103b/circuits/cs-resistor.txt',
'circuitjs103b/circuits/pll.txt',
'circuitjs103b/circuits/relayxor.txt',
'circuitjs103b/circuits/sawtooth.txt',
'circuitjs103b/circuits/classd.txt',
'circuitjs103b/circuits/cs-opamp.txt',
'circuitjs103b/circuits/updownctr.txt',
'circuitjs103b/circuits/cmosnand.txt',
'circuitjs103b/circuits/dram.txt',
'circuitjs103b/circuits/cc2imp.txt',
'circuitjs103b/circuits/qam-256.txt',
'circuitjs103b/circuits/phaseseq.txt',
'circuitjs103b/circuits/amdetect.txt',
'circuitjs103b/circuits/tdrelax.txt',
'circuitjs103b/circuits/hartley.txt',
'circuitjs103b/circuits/ccdiff.txt',
'circuitjs103b/circuits/follower.txt',
'circuitjs103b/circuits/inductkick.txt',
'circuitjs103b/circuits/deltasigma.txt',
'circuitjs103b/circuits/ringing.txt',
'circuitjs103b/circuits/mux3state.txt',
'circuitjs103b/circuits/lambda-diode-osc.txt',
'circuitjs103b/circuits/res-series.txt',
'circuitjs103b/circuits/xorphasedet.txt',
'circuitjs103b/circuits/deccounter.txt',
'circuitjs103b/circuits/minimal2.py',
'circuitjs103b/circuits/cs-varicap.txt',
'circuitjs103b/circuits/multivib-a.txt',
'circuitjs103b/circuits/butterbandstop.txt',
'circuitjs103b/circuits/voltinvert.txt',
'circuitjs103b/circuits/ota-ringmod.txt',
'circuitjs103b/circuits/varactorvco.txt',
'circuitjs103b/circuits/traffic.txt',
'circuitjs103b/circuits/cap.txt',
'circuitjs103b/circuits/actbutterband.txt',
'circuitjs103b/circuits/opint-invert-amp.txt',
'circuitjs103b/circuits/capseries.txt',
'circuitjs103b/circuits/triacdimmer.txt',
'circuitjs103b/circuits/howland.txt',
'circuitjs103b/circuits/phasecompint.txt',
'circuitjs103b/circuits/actbutterhi.txt',
'circuitjs103b/circuits/ledflasher.txt',
'circuitjs103b/circuits/ccvccs.txt',
'circuitjs103b/circuits/triode.txt',
'circuitjs103b/circuits/filt-hipass-l.txt',
'circuitjs103b/circuits/dtlnand.txt',
'circuitjs103b/circuits/3-cgand.txt',
'circuitjs103b/circuits/opint-slew.txt',
'circuitjs103b/circuits/switchedcap.txt',
'circuitjs103b/circuits/twint.txt',
'circuitjs103b/circuits/cmosnor.txt',
'circuitjs103b/circuits/ringmod.txt',
'circuitjs103b/circuits/masterslaveff.txt',
'circuitjs103b/circuits/motorprotect.txt',
'circuitjs103b/circuits/induct.txt',
'circuitjs103b/circuits/cciamp.txt',
'circuitjs103b/circuits/alu74181.txt',
'circuitjs103b/circuits/ccitov.txt',
'circuitjs103b/circuits/rtlnor.txt',
'circuitjs103b/circuits/butter10lo.txt',
'circuitjs103b/circuits/counter8.txt',
'circuitjs103b/circuits/digcompare.txt',
'circuitjs103b/circuits/mux.txt',
'circuitjs103b/circuits/r2rladder.txt',
'circuitjs103b/circuits/cs-varyresistor.txt',
'circuitjs103b/circuits/relayand.txt',
'circuitjs103b/circuits/3-cgor.txt',
'circuitjs103b/gwt/clean/images/hborder.png',
'circuitjs103b/gwt/clean/images/vborder.png',
'circuitjs103b/gwt/clean/images/circles.png',
'circuitjs103b/gwt/clean/images/thumb_horz.png',
'circuitjs103b/gwt/clean/images/corner.png',
'circuitjs103b/gwt/clean/images/thumb_vertical.png',
'circuitjs103b/gwt/clean/clean_rtl.css',
'circuitjs103b/gwt/clean/clean.css',
'circuitjs103b/circuitjs1.devmode.js',
'circuitjs103b/setuplist.txt',
'circuitjs103b/locale_nb.txt',
'circuitjs103b/x.txt',
'circuitjs103b/img/subcircuits/subcircuit-pins1.png',
'circuitjs103b/img/subcircuits/subcircuit-pins2.png',
'circuitjs103b/locale_csx.txt',
'circuitjs103b/circuitjs1.nocache.js',
'circuitjs103b/locale_zh.txt',
'circuitjs103b/BD1463902CA4134DFE8D8781E686BEFE.cache.js',
'circuitjs103b/style.css',
'circuitjs103b/locale_ru.txt',
'circuitjs103b/locale_zh-tw.txt',
'circuitjs103b/locale_fr.txt',
'circuitjs103b/compilation-mappings.txt',
'circuitjs103b/clear.cache.gif',
'circuitjs103b/locale_pl.txt',
'circuitjs103b/locale_ja.txt',
'circuitjs103b/locale_kr.txt',
];

self.addEventListener('install', event => {
  event.waitUntil(
    caches.open(CACHE_NAME)
      .then(cache => cache.addAll(urlsToCache))
  );
});

self.addEventListener('fetch', (event) => {
    // Normalize the request URL by stripping the ?startCircuit parameter
    let req = event.request;
    const url = new URL(req.url);

    if (url.searchParams.has('startCircuit')) {
        // Remove the 'startCircuit' query parameter
        url.searchParams.delete('startCircuit');
        const request = event.request;
        req = new Request(url, {
                    method: request.method,
                    headers: request.headers,
                    mode: request.mode === 'navigate' ? 'same-origin' : request.mode,
                    credentials: request.credentials,
                    redirect: request.redirect,
                    referrer: request.referrer,
                    integrity: request.integrity,
                    cache: request.caches });
    }

    event.respondWith(
        caches.match(req).then((cachedResponse) => {
            if (cachedResponse) {
                // If the resource is already cached, return it
                return cachedResponse;
            }

            // Otherwise, fetch it from the network and add it to the cache
            return fetch(req).then((networkResponse) => {
                if (
                    event.request.method === 'GET' &&
                    networkResponse.status === 200
                ) {
                    const responseClone = networkResponse.clone();
                    caches.open(CACHE_NAME).then((cache) => {
                        cache.put(req, responseClone);
                    });
                }

                return networkResponse;
            });
        })
    );
});

// Activate event: cleans up old caches
self.addEventListener('activate', (event) => {
    const cacheWhitelist = [CACHE_NAME];  // List of cache versions you want to keep

    event.waitUntil(
        caches.keys().then((cacheNames) => {
            return Promise.all(
                cacheNames.map((cacheName) => {
                    if (!cacheWhitelist.includes(cacheName)) {
                        return caches.delete(cacheName);  // Delete old caches that aren't in whitelist
                    }
                })
            );
        })
    );
});
