Skip to content

FileAttachment.lastModified does not exist after file change (in preview mode) #1280

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
timmolendijk opened this issue Apr 24, 2024 · 3 comments · Fixed by #1290
Closed

FileAttachment.lastModified does not exist after file change (in preview mode) #1280

timmolendijk opened this issue Apr 24, 2024 · 3 comments · Fixed by #1290
Assignees
Labels
bug Something isn’t working

Comments

@timmolendijk
Copy link

timmolendijk commented Apr 24, 2024

I’m running the preview server to serve a document that loads a local static JSON file. When changing the JSON file, the data gets reloaded and the document auto-updates. So far so good. The problem is that FileAttachment’s lastModified property goes missing in the process.

The FileAttachment instance before the change:

FileAttachment {name: "my.json", mimeType: "application/json", lastModified: 1713998954446}

And after the change:

FileAttachment {name: "my.json", mimeType: "application/json"}
@timmolendijk timmolendijk added the bug Something isn’t working label Apr 24, 2024
@mbostock
Copy link
Member

This is a known limitation of the preview server. As @Fil mentioned in #1051:

In preview, by contrast, [computing the modification time] happens before the data loader has had a chance to run. So, when the cache is stale, you get stale information — but the situation solves itself on the next page load. (It is not possible to solve this in any other way as we can’t predict the future, and don’t want to wait for the loader to have finished running before we send the page to the browser.)

In other words, when you edit a referenced file, the preview server immediately detects the change and notifies the client over the websocket so that the client can download the latest version of the file. However, when this referenced file is generated by a data loader, we know that the data loader has been changed, but we don’t know yet what the contents of the file are because the data loader is still running.

Perhaps there are two ways we could fix this:

  1. We could wait until the data loader finishes running before reporting it to the client. This would be quite a disruptive change to implement because currently data loaders are triggered lazily by the client requesting a file. It would also mean that page rendering would have to stall until all referenced data loaders have finished, which I expect is a much worse developer experience. (You’d never see any loading indicators and instead you’d see the browser waiting for the page to load with nothing visible.)
  2. During preview, or perhaps just when the data loader has been invalidated and is newer than its previous output, we could report the modification time as the modification time of the data loader source rather than the cached output. This is a “lie” in a sense, but it’s probably a convenient lie from the perspective of the client, since the client now wouldn’t have to handle the case where the lastModified property is undefined.

I think we can do the latter pretty easily. I’ll try that.

@mbostock mbostock self-assigned this Apr 27, 2024
@mbostock
Copy link
Member

Edit: this applies to all file updates, not just data loaders! We were forgetting to populate lastModified on the diff. Will fix.

@timmolendijk
Copy link
Author

Edit: this applies to all file updates, not just data loaders! We were forgetting to populate lastModified on the diff. Will fix.

Exactly :)

Wonderful, thanks for the fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn’t working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants