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

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

Open
williamlmao opened this issue Apr 21, 2021 · 17 comments
Open

Comments

@williamlmao
Copy link

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!

@twigs67
Copy link

twigs67 commented Apr 22, 2021

Also curious about this. It seems like v3 with the use of service workers, makes this difficult.

@twigs67
Copy link

twigs67 commented Apr 23, 2021

I was at this most of yesterday and I could not figure out how to hot reload with manifest v3. There has to be a way, because removing the extension and loading it again is just not feasible for development.

@andy-rsa
Copy link

andy-rsa commented Apr 24, 2021

Change your 'notHotReload' to include 'contentScript' and 'background'

chromeExtensionBoilerplate: {
notHotReload: ['devtools' , 'contentScript', 'background' ],
},

it appears the 'notHotReload' entry is not intuitively named (at least from the behavior i see - not having to unload and reload my extension).

@williamlmao
Copy link
Author

Huh @andy-rsa, just tried that but it's not working for me.

@andy-rsa
Copy link

andy-rsa commented Apr 25, 2021

weird, this is what I'm doing in addition.

  1. run 'npm start' in the project folder (so changes are detected and built).
  2. click 'reload' on the extension in browser's extensions tab
  3. reload the page that is using the extension

I had a problem where things stopped working for a bit so i did this

  1. stop content.styles.css @import doesn't work #1 from above
  2. run 'npm run-script build' once and verify that the expected files were built in the build directory
  3. continue with 1-3 above

Note: I ran myself in circles for 5 minutes very frustrated that it didn't appear that it was working until I realized I was hitting refresh on the wrong extension in the browser's extension page. Make sure you're clicking refresh on the extension you're developing and hit reload on the page that you're using to test the extension.

just to continue the rubber-ducking, remove and reload the extension once just to make sure you're getting the right folder (verify you see the files you're expecting).
good luck

for reference this is what that part of my webpack.config.js looks like

entry: {
    contentScript: path.join(__dirname, 'src', 'pages', 'Content', 'index.js'),
    background: path.join(__dirname, 'src', 'background.js'),
  },
  chromeExtensionBoilerplate: {
    notHotReload: [ 'contentScript', 'background' ],
  },
  output: {
    path: path.resolve(__dirname, 'build'),
    filename: '[name].bundle.js',
    publicPath: ASSET_PATH,
  },

this is what gets created in my build folder. When you add the extension to your browser, verify you see the files you're expecting.
image

this is what my extension's manifest.json looks like
image

@williamlmao
Copy link
Author

hey @andy-rsa, ahhh! thanks for the detail here. So you're right, it is hot reloading into the build folder now BUT for some reason the changes don't make it to chrome unless I hit refresh on the chrome extension. When I make changes in popup, the changes propogate into the extension with only a page refresh and not an extension refresh.

Anyways, thanks for the help here! Not having to run npm run build every time is the biggest thing anyways.

@mikemklee
Copy link

mikemklee commented 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.

@pacexy
Copy link

pacexy commented Jul 10, 2021

Hi, I wrote a package mv3-hot-reload for solving this problem, it is easy to use.

@adventurini
Copy link

Hi, I wrote a package mv3-hot-reload for solving this problem, it is easy to use.

I cannot seem to get this working with this boilerplate. I am getting a process is not defined error.

@pacexy
Copy link

pacexy commented Aug 29, 2021

@adventurini You should ensure webpack process your dependency code correctly.

@nxvinh222
Copy link

nxvinh222 commented Nov 19, 2021

@willliuwillliu how do you inject a react component into an user active tab, i currently inject plain HTML using $(data).appendTo('body') and it's not very efficiently

@tyn1998
Copy link

tyn1998 commented 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.

@raed667
Copy link

raed667 commented Mar 2, 2022

Any update on this issue?

@jktzes
Copy link

jktzes commented Mar 19, 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.

Sorry if this is asking too much but is there any chance you could cherrypick the change to support hot reload and create a PR?

@octohedron
Copy link

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.

Sorry if this is asking too much but is there any chance you could cherrypick the change to support hot reload and create a PR?

Here you go: #110

@amehito
Copy link

amehito commented Apr 10, 2022

thanks

@pigpigever
Copy link

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.

Sorry if this is asking too much but is there any chance you could cherrypick the change to support hot reload and create a PR?

Here you go: #110

Looks like it hasn't been merged yet :(

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

No branches or pull requests