feat(ssr): add import.meta.pathname as url in ssr#5252
feat(ssr): add import.meta.pathname as url in ssr#5252matthewp wants to merge 1 commit intovitejs:mainfrom
Conversation
Based on discord discussion, opening this PR to spark a discussion on `import.meta.url`. This change does 2 things: 1. Makes `import.meta.url` be the file URL, matching what Node.js does in native ESM. 2. Creates a separate `import.meta.pathname` which is the server pathname (what is currently import.meta.url in main). Can bikeshed on the name, but having the server pathname is still useful for doing things like creating URLs for dynamic resources.
|
Thanks @matthewp. I think we could divide this PR in two. Maybe we could manage to merge the fix for A Vite SSR server could run in the edge (ie. cloudflare workers), so I don't know if this should be applied generally. We have a We would also need a test case that is failing without this fix in one of the ssr playgrounds. |
|
Thanks @patak-js, happy to split if that's what the team would like. I haven't tested Cloudflare Workers but in Deno it's the file URL as well. I would expect that to be the case in CW, going to ask their team about that. |
|
Not sure about Cloudflare Workers, but Fran probably knows about this. Otherwise LGTM. |
|
We are not using |
|
I deployed a test project in Cloudflare and |
|
New PR with just the |
|
@patak-js going to leave this PR open for now, will open a separate one for the |
|
Thanks @matthewp, it would be useful if you could give a concrete example of what |
|
@patak-js In Astro we have an API: const thisURL = new URL('http://example.com' + import.meta.pathname)
export default function({ animal }) {
return <img src={new URL(`../images/${animal}.png`, thisURL)} />
}This would create an image that output something like Having this outside of SSR-only makes sense to me. I'd need some help on the best way to implement that. |
|
I've been able to workaround this without needing a pathname, due to the fact that Vite is able to resolve full pathnames, so actually having I'm a little torn on this, as having the front-facing pathname is better in some ways, but adding a non-standard import.meta property means the code is less portable. Closing for now, but happy to continue to discuss. |
Description
Based on discord discussion, opening this PR to spark a discussion on
import.meta.url. The main thing this does is fiximport.meta.urlto match Node ESM.Additional context
This PR does 2 things:
import.meta.urlbe the file URL, matching what Node.js does in native ESM.import.meta.pathnamewhich is the server pathname (what is currently import.meta.url in main). Can bikeshed on the name, but having the server pathname is still useful for doing things like creating URLs for dynamic resources.What is the purpose of this pull request?
Before submitting the PR, please make sure you do the following
fixes #123).