Renamed `sketch.js` still executes when referenced in `index.html` (caching issue?)
p5.js version
2.1.1
What is your operating system?
Mac OS
Web browser and version
Chrome Version 142.0.7444.176 (Official Build) (arm64)
Actual Behavior
In the p5 web editor (p5.js 2.1.1) I am seeing cases where an old version of sketch.js keeps being executed or loaded even after the file has been renamed (for example to not.js).
In those cases:
- Changing
<script src="not.js">to<script src="sketch.js">makes the preview run an older version of the code, even thoughsketch.jsno longer exists in the Sketch Files panel. - This still happens when loading the same project in an incognito window, so it does not look like a simple browser cache issue.
Expected Behavior
- If
sketch.jshas been renamed or removed, referencing it inindex.htmlor vialoadStrings("sketch.js")should fail, not load a stale copy. - The preview should only execute and load files that exist in the Sketch Files panel.
Steps to reproduce
I have not yet found a reliable series of steps to reproduce from a fresh sketch, but the problem started in a sketch where await loadStrings("sketch.js") was used and the file was later renamed. I'm not sure whether that is significant.
Here is a sketch exhibiting that behavior: https://editor.p5js.org/SableRaf/sketches/wuZkibCGu
Try changing <script src="sketch.js"></script> to <script src="not.js"></script> and see the color change.
Additionally, when calling loadStrings("sketch.js") from not.js, the console output shows the old contents of sketch.js from before the file was renamed to not.js (see screenshot below). The file sketch.js no longer exists in the Sketch Files panel, yet loadStrings("sketch.js") still returns the previous version instead of failing.
@SableRaf i would like to work on this issue could you please assign to me ?
Hi! I’d like to work on this issue.
I’ve read through the problem and I’m interested in investigating the stale sketch.js loading behavior. If no one is currently assigned, could you please assign this issue to me? Happy to dig into the caching / file-loading logic and work on a fix.
Thanks!
Thanks for opening this @SableRaf and for your interest in this issue @Pavan-Kumar-KN and @SinhaS12!
@Pavan-Kumar-KN @SinhaS12 Before we move ahead with assigning this issue, would you mind giving a few details about how you plan to approach the issue or investigation process? Just want to make sure that you're going in the right direction!
@SableRaf I tried to reproduce this issue, but everything is working fine both locally and on the site as well. Could you please provide more information about the issue?
@Pavan-Kumar-KN I am not sure what else I can provide beyond what is already in the issue, since I also have not been able to reproduce this from a fresh sketch.
Just to recap, the only place where I consistently see the problem is in this specific project: https://editor.p5js.org/SableRaf/sketches/wuZkibCGu
The issue does not appear when duplicating that project, though it does when loading it in a different browser, which makes me think the project may have been left in a weird internal state where two versions of the sketch file are stored. It must have something to do with how file renaming is handled in the editor's virtual file system.
If there is any specific kind of extra information that would help, I am happy to try to collect it.
@SableRaf @raclim Hi! How I am going to approach I can fix this issue. The problem happens because the code currently hardcodes "sketch.js" when loading the file, which causes old versions to run or breaks execution if the file is renamed. I plan to resolve it by dynamically loading the active file using the selector (e.g., activeFile.name) and, if needed, adding a cache-busting query to always fetch the latest version. This will make the sketch run correctly regardless of the filename
Hi @SableRaf , If you're able to consistently reproduce the issue by following the steps you mentioned, or if it occurs suddenly could you please share a short screen recording of the behavior? This will help us replicate the issue more accurately and understand what might be happening with the virtual file system.
Hi @raclim and @SableRaf 👋,
I opened the shared sketch link and tested it as-is. On my end, calling:
await loadStrings("sketch.js");
from not.js consistently results in an HTTP loading error (as expected), since sketch.js does not exist in the Sketch Files panel. I’m not seeing any stale contents being returned in this case.
This seems to reinforce the idea that the bug is project-state–specific rather than something that can be reproduced reliably from a fresh or “clean” load. In other words, when the internal state is intact, the editor behaves correctly and fails fast.
Given that duplicating the sketch also resolves the issue (fresh internal state), my current suspicion is that certain rename flows leave behind stale entries in the editor’s virtual file system or asset cache, which are only triggered under very specific conditions (e.g. rename after loadStrings, undo history, or long-lived project state).
Happy to keep digging or try instrumenting the rename / file-resolution path if that would be useful. Thanks!