-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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
Dynamic imports get cached and there is no way to get the fresh version of it #25742
Comments
And Yes this is a important use case and I'll tell why Imagine you build Next.js in Deno Imagien you have a file named _app.tsx and you import it dynamically and render, then something changes inside while the server is still running, now on next serverSideRendering request it will give you the same result even if the jsx of some child import changed Or is there some alternative to Tested it you can't even import a npm module which uses require) Basically as I understand it means you have to terminate the process and restart it maybe a script that terminates another script s needed something like pm2( |
This is a regression and definitely something that should work. |
I don't believe this ever worked. From testing, this behavior is consistent all the way back to As noted:
To highlight this, the crucial bit here is that we would have to reload all children of a given module. While we do reload |
Ohh right, I missed that bit. Just checked all the other runtimes and they only refresh the module with the query param, not any further children of said module. This works in Deno as expected when I change something in |
So is there a change that it will be fixed soon? What I had to do for the moment is to implement something similar to PM2, a process that controll other process so that I lose the cache just by killing the child process and restarting This is obviously a workaround and I would want the dynamic import to work correctly, for a better developer experience Also this blocks migration of some tools to deno which relly on dynamic require |
Can we move this to BUG label |
If I add specially but still others will have the possibility to import uncached modules In Node there are NPM packages that can do that but uses require.cache. but in deno there is no solution from what I can see |
This is working as intended as per spec. Cache busting with I think your problem can be solved by exposing a module loader API. See #8327 . For now, you have to manually cache bust all modules in the graph to achieve. |
Closing in favor of #8327 |
Lol) Working as designed dones't mean that the design is correct In tools like esbuild this works correct because every import is suffixed, and they can do that because is a transpiler You apply a JS mindset where you can compile from TS to js and have power over the resulted code....... to Deno, is that a correct design? |
The behaviour in Deno matches the behaviour of all other runtimes. What you're asking for is either a loader API, or what is more likely the proper solution is some form of transpilation like nextjs, vite, esbuild and all others are doing. Since Deno sees itself as a runtime, not a bundler, moving the cache busting logic into a bundler is more appropriate. That said there is also a |
What we need is a implimentation of Or some new method like |
I've tried --watch-hmr Also if you have to notify a browser for example once something changed in _app.tsx you can't do that since the program is restarted and you cant send any event to browser to reload it as well And yes Browser code is imported in Deno because you need to do server side rendering and once something changes in client code deno also will restart it's whole process |
And yes to your point the behavior indeed matches, but in other runtimes you have other tools that can remove the cache in Deno NO that's the difference |
This is available in Deno:
You can clear the cache if you want. I believe part of the confusion stems from the fact that |
Ok let me try this then |
Actually no it is not workig)
So aparently you can't uncache a ES module once loaded Which means once a Deno TS file is loaded then tehre is no way to uncache it's children imports And this is a real use case, I can give another example |
hmmmm https://bun.sh/docs/runtime/modules#:~:text=In%20Bun's%20JavaScript%20runtime%2C%20require,exports%20object%20(as%20in%20Node. |
|
In RC this import throws a error
|
The result is kinda the same as in 1.46) but a different error, Require doesn't seem to be able to import a ES module |
From what I can see in Bun they can import TS with require
So maybe the Deno impliemntation in RC is buggy, Or I do something incorrect? |
Can you share the code that causes this error? |
This works correctly for me: // bar.ts
export function greet() {
return "Hello!"
} // some_file.ts
import { greet } from "./bar.ts";
export default {
a: "b",
foobar: "bar",
greet,
} // foo.js
import { createRequire } from "node:module";
const require = createRequire(import.meta.url);
const a = require("./some_file.ts");
console.log(a)
I'm using Deno v2.0.0-rc.4 |
|
So I basically try to build a util that uses require instead of import so that I can delete the cache for a path |
And then the files that this utility try to load are absolute paths to TSX files and it doesn't work |
It basically says that I can't use import outside a module, but how do I convert it to a module)) if it is already a TS file and is a module
Or maybe I should split this util in 2 files one for web and one for server? maybe when it see both require and import in same file it complains? but why I would expect it to work anyway |
All the files are btw TS or TSX no JS at all |
Sorry but without a full reproduction I can't really say what's going wrong. If you have a repo that can be tested then I can take a look. |
Ok so I tested all 3 Seems that indeed only a Module Loader can help here |
That being said I can 100% guarantee that a library like Wordpress which allows to install plugins and themes at runtime and change their code at runtime, is imposible to be built in JS world We'll I guess then it becomes a Feature Request, and if Deno would impelemet a @bartlomieju Thanks for your time, I'll open a feature request for this |
Version: Deno 1.46
The bug:
file a.ts
file b.ts
file c.ts
Then run the
deno run ./a.ts
Now this will console log the result of dynamic import, and THIS IS IMPORTANT the dynamic import has a query param which means DENO should import a fresh version each time and not from cache
But if while this script runs you go to c.ts and change the string manually the console log will not change, since C inside B is cached which is supper bad since I have a query param in B and I expect a fresh version of it and it's child imports also should be not cached for this exact import
Well that obsiously sucks, because that literally means you can't load user code since only first file will be Fresh/last version everything inside again is cached
The text was updated successfully, but these errors were encountered: