简体   繁体   English

在 Service Worker 中预缓存生成的资产

[英]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.我有一些在运行时或构建时生成的资源,例如脚本、CSS 等。
The file format is something like this main.min.63716273681.js and similarly for other resources as well.文件格式类似于main.min.63716273681.js ,其他资源也类似。

Unfortunately, I cannot use sw-precache library or anything integrated with my build.不幸的是,我不能使用sw-precache库或与我的构建集成的任何东西。

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)注意:问题是关于预缓存资源和资源由 AEM(Adobe 体验管理器)生成

install webpack-manifest-plugin and import at the top of your webpack config file安装 webpack-manifest-plugin 并在 webpack 配置文件顶部导入

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在您的 webpack 插件部分中使用以下代码,这将生成一个文件名asset-manifest.json,其中包含由 webpack 构建的所有资产

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

it will generate file having content like below它将生成具有如下内容的文件

// asset-manifest.json // 资产清单.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.您可以阅读asset-manifest.json文件并获取文件object并迭代并检查最后的密钥have.js。

Hope this will answer your question.希望这能回答你的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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