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

feat(ssr): add import.meta.pathname as url in ssr #5252

Closed
wants to merge 1 commit into from

Conversation

matthewp
Copy link
Contributor

@matthewp matthewp commented Oct 11, 2021

Description

Based on discord discussion, opening this PR to spark a discussion on import.meta.url. The main thing this does is fix import.meta.url to match Node ESM.

Additional context

This PR 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.

What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the Pull Request Guidelines and follow the Commit Convention.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Ideally, include relevant tests that fail without this PR but pass with it.

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.
@patak-dev patak-dev added the p3-downstream-blocker Blocking the downstream ecosystem to work properly (priority) label Oct 11, 2021
@patak-dev
Copy link
Member

Thanks @matthewp. I think we could divide this PR in two. Maybe we could manage to merge the fix for import.meta.url in node, and leave pathname as a feat PR to be discussed in the next team meeting.

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 ssr.target config option that can be used to differentiate both cases. @frandiox @brillout could you also help us review this PR?

We would also need a test case that is failing without this fix in one of the ssr playgrounds.

@matthewp
Copy link
Contributor Author

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.

@aleclarson aleclarson changed the title fix: Align import.meta.url with Node.js fix(ssr): align import.meta.url with Node.js Oct 11, 2021
@matthewp
Copy link
Contributor Author

Going to wait for @frandiox @brillout chime in before splitting and adding tests.

@brillout
Copy link
Contributor

Not sure about Cloudflare Workers, but Fran probably knows about this.

Otherwise LGTM.

@frandiox
Copy link
Contributor

We are not using import.meta.url at runtime in Vitedge so I'm not sure how this change behaves in CFW to be honest 🤔
Afaik CFW doesn't have a filesystem. Also, they are working on ES Modules support (currently in beta) so this might even behave differently depending on the way the worker is deployed.

@matthewp
Copy link
Contributor Author

I deployed a test project in Cloudflare and import.meta.url is undefined. If you use their webpack template then it's the local filesystem URL as expected (because webpack is building to commonjs).

@matthewp
Copy link
Contributor Author

New PR with just the import.meta.url part: #5268

@matthewp
Copy link
Contributor Author

@patak-js going to leave this PR open for now, will open a separate one for the pathname feature after #5268 is merged in. I hope this issue is sufficient to discuss the pathname idea.

@patak-dev patak-dev changed the title fix(ssr): align import.meta.url with Node.js feat(ssr): add import.meta.pathname as url in ssr Oct 12, 2021
@patak-dev
Copy link
Member

Thanks @matthewp, it would be useful if you could give a concrete example of what import.meta.pathname would enable to help while discussing this new feature. Also, maybe it should be added even out of ssr so it could be used in a generic way?

@matthewp
Copy link
Contributor Author

matthewp commented Oct 12, 2021

@patak-js In Astro we have an API: Astro.resolve('./penguin.png') that would use this under the hood. It allows you to create URLs that are relative to the current module. This needs to be dynamic rather than using ?url so you can do stuff like this (this is a React component):

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 /src/images/penguin.png.


Having this outside of SSR-only makes sense to me. I'd need some help on the best way to implement that.

@matthewp
Copy link
Contributor Author

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 import.meta.url as the file URL is enough.

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.

@matthewp matthewp closed this Oct 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat: ssr p3-downstream-blocker Blocking the downstream ecosystem to work properly (priority)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants