简体   繁体   中英

precaching a generated asset in Service Worker

I have some resources which are getting generated on runtime or on build for example scripts, CSS, etc.
The file format is something like this main.min.63716273681.js and similarly for other resources as well.

Unfortunately, I cannot use sw-precache library or anything integrated with my build.

How can I precache those resources, Is it possible to do it using regex?

Note: The question is about precaching the resource and resources are getting generated by AEM(Adobe experience manager)

install webpack-manifest-plugin and import at the top of your webpack config file

const ManifestPlugin = require('webpack-manifest-plugin');

use the below code in your webpack plugin section this will generate a file name asset-manifest.json with all the assets that are build by webpack

new ManifestPlugin({
        fileName: 'asset-manifest.json',
        publicPath: your public path here,
 }),

it will generate file having content like below

// asset-manifest.json

{
  "files": {
    "about.js": "/static/js/about.bc816d03.chunk.js",
    "about.js.map": "/static/js/about.bc816d03.chunk.js.map",
    "dashboard.js": "/static/js/dashboard.f180c270.chunk.js",
    "dashboard.js.map": "/static/js/dashboard.f180c270.chunk.js.map",
    "homepage.js": "/static/js/homepage.4dd0316c.chunk.js",
    "homepage.js.map": "/static/js/homepage.4dd0316c.chunk.js.map",
    "login.js": "/static/js/login.1b8cf466.chunk.js",
    "login.js.map": "/static/js/login.1b8cf466.chunk.js.map",
    "logout.js": "/static/js/logout.ac3c5758.chunk.js",
    "logout.js.map": "/static/js/logout.ac3c5758.chunk.js.map",
    "main.css": "/static/css/main.977b6895.chunk.css",
    "main.js": "/static/js/main.a65a1d5d.chunk.js",
    "main.js.map": "/static/js/main.a65a1d5d.chunk.js.map",
    "profile.js": "/static/js/profile.20ae3dae.chunk.js",
    "profile.js.map": "/static/js/profile.20ae3dae.chunk.js.map",
    "runtime-main.js": "/static/js/runtime-main.ad8b0a50.js",
    "runtime-main.js.map": "/static/js/runtime-main.ad8b0a50.js.map",
    "static/js/8.796ce7e3.chunk.js": "/static/js/8.796ce7e3.chunk.js",
    "static/js/8.796ce7e3.chunk.js.map": "/static/js/8.796ce7e3.chunk.js.map",
    "index.html": "/index.html",
    "precache-manifest.e770a629726af82e25b547dd941bae89.js": "/precache-manifest.e770a629726af82e25b547dd941bae89.js",
    "service-worker.js": "/service-worker.js",
    "static/css/main.977b6895.chunk.css.map": "/static/css/main.977b6895.chunk.css.map",
    "static/js/8.796ce7e3.chunk.js.LICENSE.txt": "/static/js/8.796ce7e3.chunk.js.LICENSE.txt",
    "static/media/arvidsson.jpg": "/static/media/arvidsson.4d6f8e0d.jpg",
    "static/media/logo.jpg": "/static/media/logo.8caa15b8.jpg",
    "static/media/pekka.jpg": "/static/media/pekka.1eab475c.jpg"
  },
  "entrypoints": [
    "static/js/runtime-main.ad8b0a50.js",
    "static/js/8.796ce7e3.chunk.js",
    "static/css/main.977b6895.chunk.css",
    "static/js/main.a65a1d5d.chunk.js"
  ]
}

you can read asset-manifest.json file and take the files object and iterate and check the key having.js in the end.

Hope this will answer your question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM