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

Investigate how much we can mix server and client code #1864

Open
sodic opened this issue Mar 7, 2024 · 0 comments
Open

Investigate how much we can mix server and client code #1864

sodic opened this issue Mar 7, 2024 · 0 comments

Comments

@sodic
Copy link
Contributor

sodic commented Mar 7, 2024

As of Wasp 0.12.0-ish, we can mix client and server code in the same file.

For example, the main.wasp can have this:

route SomeRoute { path: "/something", to: SomePage }
page SomePage {
  component: import { SomeComponent } from "@src/api"
}

// ...

api fooBar {
  fn: import { fooBar } from "@src/api",
  httpRoute: (GET, "/foo/bar")
} 

The src/api.tsx file can contain this:

import type { FooBar } from 'wasp/server/api'

export const fooBar: FooBar = (req, res, context) => {
  res.json({ msg: 'Hello friend' })
}

export const SomeComponent = () => {
  return <div>Some gmali</div>
}

And everything works (even when built and deployed).

Gotchas:

  • Hot reloading on the server doesn't work for the tsx file (i.e,. the server ignores changes).
    This probably happens because we're not watching .tsx files.
  • It's even possible to use React on the server:
    import type { FooBar } from 'wasp/server/api'
    import React from 'react' // necessary
    
    export const fooBar: FooBar = (req, res, context) => {
      console.log(SomeComponent())
      res.json({ msg: 'Hello friend' })
    }
    
    export const SomeComponent = () => {
     return <div>Some gmali</div>
    }
    If you forget to import react, you get a runtime error (neither the IDE or Wasp complain in compile time). We should investigate this as well.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant