ProjectEvergreen / greenwood

Greenwood is your workbench for the web, embracing web standards from the ground up to empower your stack from front to back.
https://www.greenwoodjs.io
MIT License
96 stars 9 forks source link

imported modules in API routes not reloading changes in development mode #1104

Closed thescientist13 closed 1 year ago

thescientist13 commented 1 year ago

Type of Change

Bug

Summary

While editing an API route and changing its contents works, if there is a module loaded in that API route that gets changed, the contents won't change.

https://user-images.githubusercontent.com/895923/235518201-bac16056-f156-4a62-88c7-70451d60191c.mov

Details

This seems to be an ESM related caching issue, similar to the one we ran into when implementing the same feature for SSR pages, since we are using import programmatically for API routes too

async serve(url, request) {
  const api = this.compilation.manifest.apis.get(url.pathname);
  const apiUrl = new URL(`.${api.path}`, this.compilation.context.userWorkspace);
  // https://github.com/nodejs/node/issues/49442
  const href = process.env.__GWD_COMMAND__ === 'develop' // eslint-disable-line no-underscore-dangle
    ? `${apiUrl.href}?t=${Date.now()}`
    : apiUrl.href;

  const { handler } = await import(href);
  const req = new Request(new URL(`${request.url.origin}${url}`), {
    ...request
  });

It seems like we'll have to do some sort of Workers related work around here as well, at least until a more robust ESM cache busting solution is available in NodeJS.