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

Solution to userscript not refreshing on every page load #63

Open
testuser3158 opened this issue Oct 9, 2021 · 3 comments
Open

Solution to userscript not refreshing on every page load #63

testuser3158 opened this issue Oct 9, 2021 · 3 comments
Labels
good first issue Good for newcomers

Comments

@testuser3158
Copy link

testuser3158 commented Oct 9, 2021

Hi,

Even with the use of proxy script and setting Tampermonkey's externals update interval to always, I noticed the old version of script would remain active for one or two page reloads. It seems like common problem so I'll leave my solution here, in case it's of use for someone.

  1. In Tampermonkey's extension details page over at chrome://extensions/?id=dhdgffkkebhmkfjojejmpbldmpobfkfo, enable "Allow access to file URLs"

  2. Use this webpack configuration:

    devServer: {
      static: path.resolve(__dirname, 'dist'),
      devMiddleware: { writeToDisk: true }
    },
    plugins: [
      new WebpackUserscript({
        proxyScript: {
          baseUrl: `file://${path.resolve(__dirname, dist)}`,
          filename: '[basename].proxy.user.js',
          enable: !isProductionBuild
        }
      })
    ]

The important bits are that we make webpack dev server write updated script to disk, so that Tampermonkey can access it with the @require header that now points to file:// instead of the webpack dev server.

Full configuration example here: https://github.com/testuser3158/ylis-suodatin/blob/master/webpack.config.js

Related:

Lastly, thanks @momocow for this great plugin.

@momocow
Copy link
Owner

momocow commented Oct 20, 2021

Thanks for providing the information!

Maybe I should put this into the README for anyone who has been bothered in this caching and refreshing issues.

@momocow momocow added the good first issue Good for newcomers label Oct 20, 2021
@momocow momocow pinned this issue Oct 20, 2021
@Trinovantes
Copy link

I found after upgrading to webpack-dev-server@4, I also need to set hot: false

    devServer: {
        hot: false,
        devMiddleware: {
            writeToDisk: true,
        },
    },
    plugins: [
        new WebpackUserscript({
            proxyScript: {
                enable: isDev,
                baseUrl: url.pathToFileURL(path.resolve(__dirname, 'dist')).href,
                // filename doesn't need to be set since it already defaults to '[basename].proxy.user.js'
            },
        }),
    ],

@mjpieters
Copy link
Contributor

For what it's worth, ViolentMonkey has a far better reload story. I just open the .user.js file from page served by the webpack devserver, and VM then auto-reloads the script for me. I've not touched the .proxy.js version since switching.

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

No branches or pull requests

4 participants