question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ERROR in Can't find self.__WB_MANIFEST in your SW source in TypeScript project

See original GitHub issue

Library Affected: Current version: 5.1.3

Issue or Feature Request Description: When trying to build my webpack project, I run into the error ERROR in Can't find self.__WB_MANIFEST in your SW source.

I’ve tried some searching, but all the related issues don’t point me in the right direction and the docs are no help either.

webpack.config.js

const webpack = require('webpack');
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { InjectManifest } = require('workbox-webpack-plugin');

module.exports = (env = {}) => {
  const mode = env.production ? 'production' : 'development';
  const apiUrl = env.production
    ? 'https://xxx.cloudfunctions.net'
    : 'http://localhost:5001/xxx/us-central1';
  return {
    mode,
    entry: './src/index.tsx',
    output: {
      filename: `main${env.production ? '-[contenthash]' : ''}.js`,
      path: path.resolve(__dirname, 'dist'),
      publicPath: '/',
    },
    resolve: {
      extensions: ['.tsx', '.ts', '.js'],
      alias: { 'react-dom': '@hot-loader/react-dom' },
    },
    module: {
      rules: [
        {
          test: /\.(j|t)sx?$/,
          exclude: /node_modules/,
          use: {
            loader: 'babel-loader',
          },
        },
        {
          test: /\.(png|svg|jpg|gif)$/,
          use: ['file-loader'],
        },
      ],
    },
    plugins: [
      new HtmlWebpackPlugin({
        template: 'src/index.html',
      }),
      new webpack.DefinePlugin({
        mode,
        API_URL: JSON.stringify(apiUrl),
      }),
      new InjectManifest({
        swSrc: path.resolve(__dirname, './service-worker.ts'),
      }),
    ],
    devServer: {
      historyApiFallback: true,
    },
  };
};

service-worker.ts

// Listen for install event, set callback
self.addEventListener('install', function (event) {
  console.log('installed');
});

index.ts

...
if ('serviceWorker' in navigator) {
  window.addEventListener('load', () => {
    navigator.serviceWorker
      .register('/service-worker.js')
      .then((registration) => {
        console.log('SW registered: ', registration);
      })
      .catch((registrationError) => {
        console.log('SW registration failed: ', registrationError);
      });
  });
}
...

However, the process seems to work correctly. When I serve my page, I see the installed console log, and all other project functionality works as well.

Any idea what the root cause of this issue is?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

43reactions
jeffposnickcommented, May 26, 2020

If you’d like to use the InjectManifest plugin, you need to include self.__WB_MANIFEST somewhere in your swSrc file. The InjectManfiest plugin will look for that symbol and replace it with the manifest created as part of the build process—i.e. literally “injecting the manifest” into your swSrc.

You’d normally do something in your swSrc like the following to make use of the manifest:

import {precacheAndRoute} from 'workbox-precaching';

precacheAndRoute(self.__WB_MANIFEST);

// Additional code goes here.

If you don’t want to use workbox-precaching, then you don’t need to use the InjectManifest plugin at all. You can use any of the other Workbox libraries that you want, and just compile your service-worker.ts as another entry alongside your main entry in webpack. The InjectManifest plugin is only needed if you want to inject the precache manifest.

4reactions
markgong-gdcommented, Apr 24, 2022

i set import {precacheAndRoute} from ‘workbox-precaching’;

precacheAndRoute(self.__WB_MANIFEST);

error show : Can’t find self.__WB_MANIFEST in your SW source

Read more comments on GitHub >

github_iconTop Results From Across the Web

Webpack workbox Can't find self.__WB_MANIFEST in your ...
It was my webpack config for injectManifest bug, I fixed it like this : new WorkboxPlugin.InjectManifest({ swSrc: path.join(process.cwd(), ...
Read more >
Building a Service Worker with Workbox 5, TypeScript ...
In this post, I'll concentrate on one of the first building blocks that we've integrated into our Angular application: a service worker. The...
Read more >
Ultimate Guide to PWAs with Workbox
This post is a complete guide to building a Progressive Web App (PWA) from the beginning using Google's Workbox. By the end of...
Read more >
How to build a progressive web app (PWA) with Node.js
OpenSSL to generate a self-signed certificate (more on that later) ... In the browser, access http://localhost to see the page with just ...
Read more >
Add a web app manifest
The web app manifest is a simple JSON file that tells the browser about your web application and how it should behave when...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found