Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

self.__WB_MANIFEST is replaced only on the first occurrence #2681

Closed
ItalyPaleAle opened this issue Nov 27, 2020 · 8 comments · Fixed by #2683
Closed

self.__WB_MANIFEST is replaced only on the first occurrence #2681

ItalyPaleAle opened this issue Nov 27, 2020 · 8 comments · Fixed by #2683
Assignees
Milestone

Comments

@ItalyPaleAle
Copy link

Library Affected:

workbox-webpack-plugin

Browser & Platform:

Webpack 5
Workbox 6.0.0-rc.0

Issue Description:

I have this code in my service worker:

if (self.__WB_MANIFEST) {
    console.log('precached', self.__WB_MANIFEST)
    precacheController.addToCacheList(self.__WB_MANIFEST)
}

And this in the webpack config:

        new InjectManifest({
            swSrc: './src/sw/sw.js',
            include: undefined,
        }),

After building this (without minification), the output files has self.__WB_MANIFEST replaced with the manifest only on the first occurrence:

// Manifest omitted
if ([{...}]) {
    console.log('precached', self.__WB_MANIFEST)
    sw_precacheController.addToCacheList(self.__WB_MANIFEST)
}

Instead, the manifest should have been replaced in all occurrences.

@jeffposnick
Copy link
Contributor

This is a difference in behavior between workbox-build's version of injectManifest, and workbox-webpack-plugin's InjectManifest. In workbox-build we treat multiple instance of self.__WB_MANIFEST as a fatal error:

assert(injectionResults.length === 1, errors['multiple-injection-points'] +
options.injectionPoint);

I am going to standardize things so that workbox-webpack-plugin does the same instead of just silently replacing the first instance.

The rationale behind this behavior is that the precache manifest might be lengthy, and storing multiple copies of it in the same service worker file is a waste of bytes. You can do the following if you'd like to, e.g., log it and also pass it to workbox-precaching:

const manifest = self.__WB_MANIFEST;
if (manifest) {
    console.log('precached', manifest);
    precacheController.addToCacheList(manifest);
}

@jeffposnick jeffposnick self-assigned this Nov 30, 2020
@jeffposnick jeffposnick added this to the v6 milestone Nov 30, 2020
@ItalyPaleAle
Copy link
Author

Thanks. That's what I ended up doing indeed and I understand the rationale.

However, can I recommend showing an error in the console if people try to replace it more than once? Right now, it silently fails and that made me spend quite a bit of time trying to debug this.

@jeffposnick
Copy link
Contributor

Yup, I'm going to treat this as a compilation error in v6.

@tangdexin
Copy link

i have the same error

@jeffposnick
Copy link
Contributor

@tangdexin, please only include the symbol self.__WB_MANIFEST in your swSrc file once. If you need to refer to the manifest multiple times, assign self.__WB_MANIFEST to a variable first:

const manifest = self.__WB_MANIFEST;

// Do whatever you want with `manifest`:
if (manifest) {
    console.log('precached', manifest);
    precacheController.addToCacheList(manifest);
}

@tangdexin
Copy link

@tangdexin, please only include the symbol self.__WB_MANIFEST in your swSrc file once. If you need to refer to the manifest multiple times, assign self.__WB_MANIFEST to a variable first:

const manifest = self.__WB_MANIFEST;

// Do whatever you want with `manifest`:
if (manifest) {
    console.log('precached', manifest);
    precacheController.addToCacheList(manifest);
}

It works , thanks very much

@Alkaidd
Copy link

Alkaidd commented Oct 11, 2024

version: "workbox-webpack-plugin": "6.6.0"
In this version, i have the same error even if i commented out "self.__WB_MANIFEST" while run dev.
For example:

const ignored = self.__WB_MANIFEST;
// precacheAndRoute(self.__WB_MANIFEST)

This will cause same error in dev, but work well in build.
After deleting the comment, i run dev successfully.

@FleetAdmiralJakob
Copy link

Same issue here. Commented out __SW_MANIFEST counts as it is not commented out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants