luigi
luigi copied to clipboard
onNodeActivation does not work when using deepLinks
Description
When the onNodeActivation
function is used on a node, then it will only run the function, when we navigate manually to the node.
When the node is opened via a deeplink (e.g. just reload the page when on the node), then the function is not executed.
Browser: Chrome OS: OSX Version: 1.21.0 Expected result
the onNodeActivation callback is executed when the node is accessed through a deeplink
the onNodeActivation callback is not executed when the node is accessed through a deeplink
Steps to reproduce
- add onNodeActivation callback to a node, e.g. basicExample:
//you can now use ES6 goodies here
Luigi.setConfig({
navigation: {
nodes: () => [
{
pathSegment: 'home',
label: 'Home',
children: [
{
pathSegment: 'hw',
label: 'Hello World!',
viewUrl: '/assets/basicexternal.html'
},
{
pathSegment: 'one',
label: 'Section one',
viewUrl: '/assets/basicexternal.html#one',
onNodeActivation: function () {
alert('hello world');
}
},
{
pathSegment: 'two',
label: 'Section two',
viewUrl: '/assets/basicexternal.html#two'
}
]
}
]
},
routing: {
/**
* Development:
* For path routing, set to false
* For hash routing, set to true
*/
useHashRouting: true
},
settings: {
appLoadingIndicator: {
hideAutomatically: true
}
}
});
- navigate to Section one (the alert is shown)
- reload the page (the alert is not shown)
Troubleshooting
I could hack around the issue by doing the following:
- add the following lifecycle hooks:
lifecycleHooks: {
luigiAfterInit: () => {
const {pathname} = window.location
if (pathname != '/' && Luigi.navigation().pathExists(pathname)) {
Luigi.navigation().navigate(pathname)
}
}
}
Hi @paschdan , sorry for the late response. You are right, the onNodeActivation behaves like you have described and it is by design, although I agree with you that it is not very intuitive. Problem is that a change here would be a breaking one, which we usually try to avoid at all costs ;)
I guess a good alternative could be https://docs.luigi-project.io/docs/navigation-parameters-reference/?section=nodechangehook, which is pretty flexible. With that, you could even introduce your own prop at node level with a function that is executed, e.g.
...
navigation: {
nodes: [{
...,
label: 'SomeNode',
functionThatAlwaysGetsExecuted: () => { alert('Hello World'); }
...
}],
nodeChangeHook: (previous, current) => {
if(current.functionThatAlwaysGetsExecuted instanceof Function) {
current.functionThatAlwaysGetsExecuted();
}
},
...