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

Split client & serverside imports #183

Open
DavidWells opened this issue Jun 30, 2021 · 2 comments
Open

Split client & serverside imports #183

DavidWells opened this issue Jun 30, 2021 · 2 comments
Labels
enhancement New feature or request

Comments

@DavidWells
Copy link
Owner

Currently imports work with a single simple import statement, e.g.

import googleAnalytics from '@analytics/google-analytics'

This is great for dev ergonomics but causes issues with typescript because the serverside code can have different types than the client code 😅. Bummer! Reference issue in TS repo


So it appears we need a different approach to this.

It will most likely look something like this with client and server import paths

// import client side code for browser
import googleAnalytics from '@analytics/google-analytics/client'
// import server side code for node enviroment
import googleAnalytics from '@analytics/google-analytics/server'

Not as pretty but this should solve typescripts inability to resolve the correct type files.


Open to suggestions if anyone has ideas on this 😃

@DavidWells DavidWells added the enhancement New feature or request label Sep 14, 2021
@dobesv
Copy link
Contributor

dobesv commented Aug 5, 2022

I guess when you run typescript to compile your code, it can't really tell which code is client code and which is server code, and so it doesn't really have a way to choose types based on that. It works in JS only because the file to import is resolved at runtime so it knows whether it's in a browser or in node.

I'm not sure there's a good way to define the types without separating the imports, or at least giving the option to do so.

The only way it would work is to define types that allow client and server operations both, or else some kind of lowest common denominator.

Probably better to just have separated imports, though. Even in JS it'll help people avoid accidentally importing the wrong version of the API.

@dobesv
Copy link
Contributor

dobesv commented Aug 5, 2022

Since "server" analytics are not that common, maybe the top-level default import would be for client code, and you could use /server on the end to import the server stuff?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants