saml-dev/obsidian-custom-js

CustomJS not loading on startup

rossoj85 opened this issue · 6 comments

This seems to only happen at startup. If I flip to a different note and then back again, everything loads. Is there any way to force the load before startup across all code fences?

Thanks!

image

This is what I use to load my dataview script on startup and it gets rid of the same error you are getting.
It's a dataviewjs block saved in startup.md

// required by dataview for getWeather functions
let tpl = this.app.plugins.plugins['templater-obsidian'].templater;

I load it with templater 'Startup Templates' option.
Without defining tpl (a variable used in my script) on startup I was getting the same error even though it is already defined in my script? I had the same effect where changing from this note to another and back fixed it but with this startup script that is no longer necessary. Hopefully this will help you.

Check out the Asynchronous Usage section of the README. It might fix the problem for both of you.

I tried adding await forceLoadCustomJS(); as described in the README but then Obsidian renders the page before Dataview finishes indexing, resulting Dataview: No results to show for table query.

Works fine without CustomJs if the code is added to the file directly.


Workaround is enabling Automatic View Refreshing for Dataview. I had that off because of another issue: blacksmithgu/obsidian-dataview#1075

@charleshan Hello! Thank you for your insights! May I know what exactly you have added into the JS file?
I tried to just add await forceLoadCustomJS() but the code add syntax error.

I have the same issue

await forceLoadCustomJS();
const {getSome} = commonJS;

Evaluation Error: ReferenceError: commonJS is not defined
at eval (eval at (plugin:dataview), :2:20)
at async DataviewJSRenderer.render (plugin:dataview:18402:13)

@yustinrd you typed commonJS instead of customJS.