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

Use .server.js and .client.js to assert the JavaScript environment #744

Closed
brillout opened this issue Mar 27, 2023 · 12 comments · Fixed by #1296
Closed

Use .server.js and .client.js to assert the JavaScript environment #744

brillout opened this issue Mar 27, 2023 · 12 comments · Fixed by #1296

Comments

@brillout
Copy link
Member

brillout commented Mar 27, 2023

Description

Now that the V1 design removes the filename suffixes such as .page.server.js and .page.client.js, we can use .server.js and .client.js for another purpose.

I suggest we use these to assert where the file is loaded.

// /some/where/SomeClientOnlyComponent.client.js

// The file ends with `.client.js` and is therefore guaranteed to never load on the server-side
export function SomeClientOnlyComponent() {
   // ...
}
// /some/where/secrets.server.js

// The file ends with `.server.js` and is therefore guaranteed to never load on the client-side
export const databaseConnection = {
  password: '4H34CXA3cMa'
}
@brillout brillout added the enhancement 🚀 New feature or request label Mar 27, 2023
@brillout brillout self-assigned this Mar 27, 2023
@brillout brillout removed their assignment Jun 29, 2023
@dajinchu
Copy link
Contributor

Is there a way to do this manually in the meantime? Does checking typeof window at the top of a file cover it? Or is it more nuanced. Thanks

@brillout
Copy link
Member Author

Yes you can try to implement a Vite plugin that does this. AFAICT it should be relatively straigthforward to implement. Keep me updated if you do, as I'm curious about what you end up with.

@dajinchu
Copy link
Contributor

Wrote a simple plugin. It's pretty dumb but gets the job done for us. It just checks for substring ".server" in import statements.

https://gist.github.com/dajinchu/2c03ec791ad135e481dbb96bd3538121

Wrote another version using resolveId instead of transform but transform gives a nice error message showing where the import happened.

@brillout
Copy link
Member Author

a nice error message showing where the import happened.

Good idea 👌. (And the dependency on es-module-lexer should be fine.)

I'll additionally implement the "dumber" version with an internal assertion, increasing the safety of this feature.

@usk94
Copy link
Member

usk94 commented Nov 28, 2023

Hi @brillout. I am going to start working on this issue, and I plan to follow this approach for both the server and the client side, is this correct?

It just checks for substring ".server" in import statements.

@brillout
Copy link
Member Author

brillout commented Nov 28, 2023 via email

@igolkotek

This comment was marked as off-topic.

@brillout

This comment was marked as off-topic.

@igolkotek

This comment was marked as off-topic.

@brillout

This comment was marked as off-topic.

@igolkotek

This comment was marked as off-topic.

@brillout

This comment was marked as off-topic.

brillout added a commit to usk94/vike that referenced this issue Dec 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants