neutralinojs-cli icon indicating copy to clipboard operation
neutralinojs-cli copied to clipboard

Conflict Hot Reload with Neutralino native API

Open mp3846 opened this issue 2 years ago • 2 comments

After enabling Hot Reload and running neu run --frontend-lib-dev Hot Reloading works fine but I can not access global variables like NL_OS !

Here is the result of running Demo project:

Neutralino Hot Reload Conflict With Native API

After running neu run --frontend-lib-dev --window-enable-inspector I get this error in the console:

Uncaught DOMException: Failed to construct 'WebSocket': The URL 'ws://localhost:undefined' is invalid.
at a (http://localhost:3000/neutralino.js:1:580)
at e.init (http://localhost:3000/neutralino.js:1:11526)
at ./src/index.js (http://localhost:3000/static/js/bundle.js:105:19)
at options.factory (http://localhost:3000/static/js/bundle.js:45618:31)
at __webpack_require__ (http://localhost:3000/static/js/bundle.js:45063:33)
at http://localhost:3000/static/js/bundle.js:46200:37
at http://localhost:3000/static/js/bundle.js:46202:12

Every thing is OK when I just run neu run (native API is accessible)

mp3846 avatar Jul 15 '22 00:07 mp3846

As discussed in this bug #909 some variables should be initialized first (for react it should be inside index.js)

import { BrowserRouter } from 'react-router-dom'
import { StrictMode } from 'react'
import App from './App'
import authInfo from '<AppName>/.tmp/auth_info.json' // using a symlink to access folders outside src
import ReactDOM from 'react-dom/client'

const root = ReactDOM.createRoot(document.getElementById('root'))
root.render(
	<StrictMode>
		<BrowserRouter>
			<App />
		</BrowserRouter>
	</StrictMode>
)

if (process.env.NODE_ENV === 'development') {
	window.NL_PORT = authInfo.port
	window.NL_TOKEN = authInfo.accessToken
	window.NL_ARGS = []
}

window.Neutralino.init()

Now we can access Neutralino API but still predefined global variables like NL_OS or NL_CWD are undefined!

mp3846 avatar Jul 30 '22 08:07 mp3846

Now we can access Neutralino API but still predefined global variables like NL_OS or NL_CWD are undefined!

I found a fix for Vite but you can edit my fix according to the build tool you're using, https://github.com/neutralinojs/neutralinojs/issues/909#issuecomment-1374735839

Vexcited avatar Jan 07 '23 21:01 Vexcited

I think you can solve the issue by using the window. prefix while accessing global variables. Please use the latest neu CLI version which doesn't ask you to manually use the --frontend-lib-dev flag. Thanks :tada:

shalithasuranga avatar May 06 '24 12:05 shalithasuranga