Unable to override scripts

Solved Plugin Development
  • Migrating to NodeBB v2 I came across an issue that I cannot resolve. In my theme/plugin I override two scripts because I want to write my own logic for two pages:

    • register
    • email

    I get this error:

    uncaughtException: EEXIST: file already exists, symlink '../../../../node_modules/[PLUGIN/THEME NAME]/lib/client/register.js'

    Any ideas on how to get around this?

  • @baris Instead of overriding the file, you can name the file whatever you want, and use filter:script.load on the client-side to remove the call to the built-in module, and insert your own.

    hooks.on('filter:script.load', (data) => {
      console.log(data.tpl_url, data.scripts);
    });
    
  • Do you want to completely replace the logic on register page? Can you post a sample from your plugin.json?

    I think the suggested way to do this is to use modules section.

  • @baris Instead of overriding the file, you can name the file whatever you want, and use filter:script.load on the client-side to remove the call to the built-in module, and insert your own.

    hooks.on('filter:script.load', (data) => {
      console.log(data.tpl_url, data.scripts);
    });
    
  • magnusvhendinM magnusvhendin has marked this topic as solved on
  • @julian This worked great. I did this in my main client file (like persona.js).

    require(['hooks'], (hooks) => {
    	hooks.on('filter:script.load', (data) => {
    		const replaceMap = {
    			register: {
    				target: 'forum/register',
    				with: 'replacement/register',
    			},
    		};
    
    		const replace = replaceMap[data.tpl_url];
    
    		if (replace) {
    			const index = data.scripts.indexOf(replace.target);
    			if (index > -1) data.scripts[index] = replace.with;
    			else data.scripts.push(replace.with);
    		}
    
    		return data;
    	});
    });
    

    Then I can just add my scripts that I want to replace to the replaceMap object.


Suggested Topics


  • 0 Votes
    6 Posts
    427 Views

    Thanks for the clarification @PitaJ! I did a lot of experimenting but I guess I didn't get the right combination. I think I never tried naming the module with its extension. That might have been it. For anyone who might be interested in the actual solution, here it is:

    In file-explorer.js:

    define('azure/file-explorer', ['components'], function (components) { const FileExplorer = {}; FileExplorer.init = function () { // Init stuff here }); return FileExplorer; });

    In plugin.json:

    "modules": { "azure/file-explorer.js": "static/lib/file-explorer.js" },

    In admin.js:

    define('admin/plugins/azure', ['settings', 'azure/file-explorer'], function (settings, fileExplorer) { fileExplorer.init(); });

    In main.js:

    $(document).ready(function () { require(['azure/file-explorer'], (fileExplorer) => { fileExplorer.init(); }); });
  • Cached scripts

    Plugin Development
    1
    1 Votes
    1 Posts
    230 Views

    Hi all!
    I've run into an issue that when I deploy a new version of my theme/plugin, most users don't get any changes done until they do a hard refresh. This only seems to affect the client side Javascript, so everything looks updated, but in reality functionality gets lost.

    Has anyone stumbled upon something similar? Is there a better way to release code?

    The background in this is that I have forked Persona and built upon it, so all changes to the platform is basically in the theme. Is there a way to make sure all clients receive all updates?

    Thanks!

  • 0 Votes
    5 Posts
    2k Views
  • 0 Votes
    3 Posts
    2k Views

    Off course, tha'll work!
    Thanks heaps for that!

  • 0 Votes
    2 Posts
    2k Views

    @akumbhare Best bet is to have a client-side file loaded that listens for the action:ajaxify.end hook.

    $(window).on('action:ajaxify.end', function(ev, data) { var url = data.url; });