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.

How to get content and background scripts to hot reload as well?

See original GitHub issue

I’m having trouble figuring out what to change in webpack config in order to get my content and background scripts to hot reload with the rest of my app.

This is important for me because my extension is injecting a react component into a user’s active tab via the content script. For this reason I need the content script to hot reload.

Here is my webpack config, or at least what I think are the relevant parts.

var options = {
  mode: process.env.NODE_ENV || 'development',
  entry: {
    newtab: path.join(__dirname, 'src', 'pages', 'Newtab', 'index.jsx'),
    content: path.join(__dirname, 'src', 'pages', 'Content', 'index.js'),
    options: path.join(__dirname, 'src', 'pages', 'Options', 'index.jsx'),
    popup: path.join(__dirname, 'src', 'pages', 'Popup', 'index.jsx'),
    pageInject: path.join(__dirname, 'src', 'pages', 'PageInject', 'index.jsx'),
    background: path.join(__dirname, 'src', 'pages', 'Background', 'index.js'),
    contentScript: path.join(__dirname, 'src', 'pages', 'Content', 'index.js'),
    devtools: path.join(__dirname, 'src', 'pages', 'Devtools', 'index.js'),
    panel: path.join(__dirname, 'src', 'pages', 'Panel', 'index.jsx'),
  },
  chromeExtensionBoilerplate: {
    notHotReload: ['devtools'],
  },

As you can see I removed contentScript from chromeExtensionBoilerPlate.notHotReload.

Honestly I’m having trouble even understanding how this hot reloading is working, any help here is appreciated!

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:1
  • Comments:16

github_iconTop GitHub Comments

4reactions
tyn1998commented, Feb 8, 2022

Hi guys, I have just added background & content-scripts auto-reload feature to this boilerplate (with some other not-related modification out of personal taste).

checkout this: https://github.com/tyn1998/chrome-extension-boilerplate-react/tree/background-contentScripts-auto-reload

IMPORTANT: you should keep service worker devtools page open to keep EventSource connection to devServer! Since service worker will be killed automactically in mv3. image

I will explain how it works later. please tell me if it works.

4reactions
leemun1commented, May 2, 2021

Can confirm that the change made to content script is only reflected on refreshing the page WITH the extension, not just the extension.

Interestingly, refreshing the page by itself did not change anything, nor did refreshing the extension by itself. The changes took effect only when the extension was refreshed first, THEN the page was refreshed.

I would also love to find a fix or workaround for this issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Hot Reloading for Chrome Extensions | by Vit Gordon - Medium
Drop hot-reload.js to your extension's directory. Add it to your background scripts section in the manifest.json file: "background": { "scripts": ...
Read more >
Inject content script on extension reload programmatically ...
I am creating a chrome extension that I want to be able to enable/disable. I have successfully made a popup that does just...
Read more >
11.4: Chrome Extensions: Background Scripts - YouTube
In this video, I discuss " background scripts " for chrome extensions. The background script (or " background page") contains JavaScript code ...
Read more >
Edit running code with Hot Reload - JetBrains Rider
Make changes when debugging ... You can edit the code of a suspended program and then continue debugging without the need to restart...
Read more >
chrome-extension-hot-reload - npm package - Snyk
You should NOT call connect in options page or content scripts, they should already work automatically if you have called start in background...
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