Skip to content
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

Bug with resolving imports when using viteServer.ssrLoadModule in astro adapter #11048

Closed
1 task done
patheticGeek opened this issue May 15, 2024 · 6 comments
Closed
1 task done
Labels
needs triage Issue needs to be triaged

Comments

@patheticGeek
Copy link

patheticGeek commented May 15, 2024

Astro Info

Astro                    v4.8.3
Node                     v18.18.0
System                   Linux (x64)
Package Manager          npm
Output                   server
Adapter                  @patheticGeek/astro-express
Integrations             none

If this issue only occurs in one browser, which browser is a problem?

No response

Describe the Bug

I am migrating from nextjs to astro at work and need to run astro server under a express server as we have integrations on express level want to be using them.

I created a express adapter based on https://github.com/matthewp/astro-fastify but both of these have the same issue which is in the vite plugin's configureServer function. we get the vite server and have to load our server file with server.ssrLoadModule.

This server.ssrLoadModule function is not resolving relative imports correctly in astro projects, it works fine when used in a vanilla vite project (Example).

Both projects have the same file structure for server folder

├── package.json
├── server
│   ├── index.js
│   └── utils
│       └── index.js
├── astro-express
│   ├── index.js // when using astro adapter, ssrloadmodule is called in this file
│   ├── response-iterator.js
│   └── server.js
└── src
    └── server.js // when using vite, ssrloadmodule is called in this file

The following error is thrown when trying to import ./utils/index.js in server/index.js

Failed to load url ./utils (resolved id: ./utils). Does the file exist?
  Stack trace:
    at loadAndTransform (/home/projects/withastro-astro-kfswv5/node_modules/vite/dist/node/chunks/dep-cNe07EU9.js:53933:21)

Related

matthewp/astro-fastify#21

What's the expected result?

The relative imports should work as expected

Link to Minimal Reproducible Example

https://stackblitz.com/edit/withastro-astro-kfswv5?file=astro-express%2Findex.js

Participation

  • I am willing to submit a pull request for this issue.
@github-actions github-actions bot added the needs triage Issue needs to be triaged label May 15, 2024
@bluwy
Copy link
Member

bluwy commented May 15, 2024

The bug is that as you're calling server.ssrLoadModule in configureServer, Vite's internal plugin that converts the ./utils to something valid for itself, e.g. /server/utils/index.js, is not working because of this line where transform is should be converting the path, but its configureServer hook has not reached yet.

I'm not really sure how we can fix this on the Vite side, perhaps it's not safe to call ssrLoadModule before the server is finished configuring? You may need to workaround this by delay calling ssrLoadModule, e.g. returning a function from configureServer. Or maybe not rely on ssrLoadModule for importing the code.

@patheticGeek
Copy link
Author

Thanks a lot ! That fixed the issue.
Here's the fixed version for reference.

Is there any place for discussion around better support for a dev server in adapter?
I would like to contribute to that.

There was this stream where they explored it but nothing came out of this
https://www.youtube.com/watch?v=GgyGjCF-pVk
mcollina/fastify-astro#1

@patheticGeek
Copy link
Author

Also a question is, why does it happen only with astro and not in a vite project
and i have also found out that no aliases are being applied when using this function, some of the config is being ignored i think

@bluwy
Copy link
Member

bluwy commented May 15, 2024

Is there any place for discussion around better support for a dev server in adapter?

For this specific case, I don't think there's a discussion about this yet. For adapter influencing dev in general, there's the Vite 6 Environment API, but it wouldn't help this case with a custom express/fastify server. I've not seen the pattern you showed a lot in the wild, and it feels a bit hacky to me though.

Also a question is, why does it happen only with astro and not in a vite project

It also happens with a Vite project if you do the ssrLoadModule in a configureServer hook. Vite also has a specific middlewareMode to prevent creating two http servers.


Also closing this for now since it's not a bug in Astro. I agree that the error is confusing and perhaps you can submit an issue to Vite about it, but I'm not sure how to fix this in Vite yet in the meantime.

@bluwy bluwy closed this as not planned Won't fix, can't repro, duplicate, stale May 15, 2024
@patheticGeek
Copy link
Author

It also happens with a Vite project if you do the ssrLoadModule in a configureServer hook. Vite also has a specific middlewareMode to prevent creating two http servers.

Ah, understood what I was missing and why this worked in the vite example I gave above.

I've not seen the pattern you showed a lot in the wild, and it feels a bit hacky to me though.

I admit this is a rare use case, but super important when you want APIs/business logic separated from your rendering framework. This has helped that we have APIs fully separated from next and can move to astro without rethinking our that whole layer.
Also, just super useful in general to have the same adapter serving things in dev & prod, not having different behaviours there.

Anyway, thanks for the help!

@bluwy
Copy link
Member

bluwy commented May 16, 2024

Ah I missed the Vite example you gave. Yeah the difference is that the example calls ssrLoadModule (or executeEntrypoint) after the server is created, while the Astro case is calling it while the server is being created, hence the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs triage Issue needs to be triaged
Projects
None yet
Development

No branches or pull requests

2 participants