How to preload file with `application` object

Is there a built-in way to preload the content of a file using the application object in the Dev Console?

Explanation

I need a way to get the content of a specific file while loading up the Glitch Editor.
application.fileByPath(FILE_PATH).content() just gives me a blank string if the file hasn’t been opened yet.

I know that I could just force-open the file using some event shenanigans, but that would result in slightly slower loading times (for actually calling it).

I would appreciate help of any kind. Thanks!

1 Like

I think application.fileByPath(FILE_PATH).content() is mostly to write to a file by providing parameter to the content(CONTENTS) function. Try using application.fileByPath(FILE_PATH).I.contents

That gives the same empty string.

Replication Steps

  1. Select any file that ISN’T the one you are wanting to read data from.
  2. Reload your page.
  3. Call application.fileByPath(FILE_PATH).content() or application.fileByPath(FILE_PATH).I.content in the dev console (NOT the glitch terminal).
1 Like

yeah I just tested it, I think Glitch changed it. application.fileByPath(FILE_PATH).I.contents is what I have always been using…

1 Like

This seems to work:

let selectedFile;
application.files().map((file) => {
if (file.I.path === "FILE PATH") {
selectedFile = file
}});
console.log(selectedFile.I.content)

(I’m sure there’s a better way instead of using map btw)

That results in the same thing (An empty string).

It’s probably because the selectedFile variable is the same object that application.fileByPath returns…

Did you do the replication steps that I had posted above?

Reloading the page seems to make it go back to an empty string again. Probably file contents are cached when opening a file and that’s what is accessed by application.fileByPath("path").I.content

That makes sense…

The problem is that I need to know the contents of the file before any user opens it (I may even make the file hidden)

1 Like

Figured it out!

The session can be loaded for the file with:

application.ensureSession(application.fileByPath(FILE_PATH));

Then you can read the data from it like normal!

Just keep in mind that application.ensureSession returns a promise.

1 Like

that’s really neat! is it ok if I also use that for glitch addons? :saluting_face:

Sure! Go ahead!

I’m using it for Monarch anyways!

1 Like

whoa you’re adding asset folders and more themes?

I’m 90% done with the folders already, and themes have technically already been added.

To get the themes, you just need click the extensions button in the top right, then click on the brownish-yellow icon for Monarch.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.