[SSR - Quasar CLI Vite] How to properly separate server and client dependencies? #14595
Answered
by
TobyMosque
gustavotoyota
asked this question in
CLI - SSR mode
-
|
I want to add Objection only to the server, but it's also appearing in the client code. |
Beta Was this translation helpful? Give feedback.
Answered by
TobyMosque
Oct 11, 2022
Replies: 2 comments 3 replies
-
|
do u're doing that in a ssr middleware? |
Beta Was this translation helpful? Give feedback.
2 replies
-
|
IMHO: preFetch isn't the right place for that, but if u wanna some piece of code to run only at the side side (or only at the client side), u can do that: instead of import { doSomethingOnServer } from 'server-only-package'
import { doSomethingOnClient } from 'client-only-package'
export default {
async preFetch ({ ssrContext }) {
if (ssrContext) {
const res = await doSomethingOnServer()
} else {
const res = await doSomethingOnClient()
}
}
}you would do: export default {
preFetch () {
if (process.env.SERVER) {
const { doSomethingOnServer } = require('server-only-package').default
const res = await doSomethingOnServer ()
}
if (process.env.CLIENT) {
const { doSomethingOnClient } = require('client-only-package').default
const res = await doSomethingOnClient()
}
}
}but i still think that would be done in a SSR middleware import { doSomethingOnServer } from 'server-only-package'
export default async ({ app }) => {
const apiRoute = app.route('/api')
apiRoute.get('/do-something', (req, res) => {
const res = doSomethingOnServer()
res.send(res)
})
}so, in the preFetch import axios from 'axios'
export default {
async preFetch () {
const res = await axios.get('/api/do-something')
}
} |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
gustavotoyota
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
IMHO: preFetch isn't the right place for that, but if u wanna some piece of code to run only at the side side (or only at the client side), u can do that:
instead of
you would do: