-
Notifications
You must be signed in to change notification settings - Fork 5
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
Suggestions: Ability to clear and re-initialise, or expose interals #13
Comments
Hello! Thank you so much for your attention and for the suggestion :) |
@coxmi Could you please clarify which version of Node.js you're currently using? Also, when you refer to 'watch mode', are you talking about the default '--watch' option or external tools like nodemon or tsnd? It will help me to reproduce the issue. |
Hi Dan, thank you! I'm using esbuild's watch mode at the moment. I was hoping that I would be able to manually refresh the handler after each build, so i'm doing something like this currently: import esbuild from 'esbuild'
import glob from 'fast-glob'
let server
const routes = glob.sync('./routes/**.{ts,js}')
const ctx = await esbuild.context({
entryPoints: [
'server.ts',
...routes
],
outdir: '.build',
bundle: true,
platform: 'node',
plugins: [
{
name: 'onwatchend',
setup: build => {
build.onEnd(async result => {
if (server) {
await server.setupRouter()
} else {
server = await import('./.build/server.cjs')
}
})
}
}
]
})
await Promise.all([
ctx.watch(), server.serve()
]) I can get a slimmed down example repo over to you if that would be easier? I'm not 100% certain this is a good way to use Thanks again :) |
I've created a test repo here: ( |
@coxmi wow, thank you! I've already sat down to figure it out |
Let me know if I can help in any way, I'd be very happy to work on a pull request if there's a rough plan for the API. |
@coxmi I've implemented the watch mode in a slightly different way: coxmi/node-file-router-test#1 Does this solve your case? Or would you prefer to keep the server state without restarting it completely and just re-import the updated files? |
Hi Dan, thanks! That does work, but I'm running another service in the same express instance (with a long boot time), so I can't just start/restart the thread, annoyingly. Is it feasible to expose the Then I may be able to get it working with the SSR APIs of various bundlers too (e.g Vite's |
@coxmi Alright, sure! I need to determine how the API might look. Besides import/require cache invalidation, are there any other use cases you have? |
Amazing, thank you so much! |
@coxmi Hi! Would this approach work for you? (it's just api design, not released)
This will clear the cache each time before importing. You'll simply need to reinitialize the file router. I think this is the simplest method to accomplish it without altering the existing API. |
@coxmi Hi! :) Is it still relevant for you? Could the approach above help you? |
Hello! and sorry for the delay in getting back to you. Yes, that would be fantastic :) |
@coxmi Hi! I released a new version with this feature :) To see how to use it please take a look at a new section on the website. But it's actually the same as I described. Please let me know if everything is okay or if there are any areas that need improvement. I've check in your test repo, it works. |
Thanks so much for this! |
Hello,
Thanks for node-file-router!
I'm trying to get this working in watch mode while developing. Ideally after a route file has been edited/rebuilt, the next request should reflect the updates, rather than the cached module in node.
I've tried re-running the
initFileRouter
function after an update and passing the new instance into the app middleware, which doesn't work:The node module cache needs to be cleared first, with:
Which allows it to load the most recent version.
With all that said, is there a possibility to either:
clearCache
orrefresh
function which clears the node module cache for any previously imported modules, and re-imports them?(url: string): string => filePathToModule
?Thanks again :)
The text was updated successfully, but these errors were encountered: